views:

1216

answers:

4

I'm at a loss to explain this one:

I'm getting an error "91" (Object or With block not set) on the second line below:

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("SELECT * FROM employees")

The following also causes it: Set rs = CurrentDb.OpenRecordset("employees")

Executing ?CurrentDb.Name alone in the immediate window causes the error as well.

Now, clearly the database is open since I'm editing the form within it, so what can cause this error here?

+3  A: 

you should assign your .openRecordset method to a dao.recordset object or a generic object ('late binding' technique). try something like this:

dim rs as dao.recordset
set rs = currentDb.openRecordset(your SELECT instruction,...)
Philippe Grondier
+4  A: 

If you are working with an ADP project, you should use CurrentProject instead of CurrentDB.

birger
+1  A: 

Try creating a database object and using that instead of the CurrentDb reference. For example:

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM Employees")
Forester93
A: 

The reason l suspect the code will not work is that according to M/Soft link text 'CurrentDb' is a method which will return an 'object variable of type Database'. So the code shown by Forester93 should work. Its what l would use and have been using for many years now.

BobF