CurrentDb.OpenRecordset error (error 91 - Object variable or With block variable not set)

E

E. Kwong

I'm trying to extract the field names of a one-row table but keep gettning
"Error 91 : Object variable or With block variable not set". The code
snippet:

Dim rst As DAO.Recordset
Dim f As DAO.Field

On Error GoTo GetFieldNames_Err
Set rst = CurrentDb.OpenRecordset("table1")
For Each f In Rst.Fields
do somthing
Next
rst.Close

The error keeps hitting at the "set rst..." line. Really puzzling. I've
googled about the OpenRecordset method and have tried suggestions on some of
the posts but still to no avail.

I'm using Access 2003, and SQL Server 2000 tables. The codes are in a
Project (.adp). The References include DAO 3.6 object library and ADO 2.1
library.

Any insight greatly appreciated.
 
D

Dirk Goldgar

E. Kwong said:
I'm trying to extract the field names of a one-row table but keep gettning
"Error 91 : Object variable or With block variable not set". The code
snippet:

Dim rst As DAO.Recordset
Dim f As DAO.Field

On Error GoTo GetFieldNames_Err
Set rst = CurrentDb.OpenRecordset("table1")
For Each f In Rst.Fields
do somthing
Next
rst.Close

The error keeps hitting at the "set rst..." line. Really puzzling. I've
googled about the OpenRecordset method and have tried suggestions on some
of the posts but still to no avail.

I'm using Access 2003, and SQL Server 2000 tables. The codes are in a
Project (.adp). The References include DAO 3.6 object library and ADO
2.1 library.

Any insight greatly appreciated.


I'm pretty sure that in an ADP, there is no "current database", only
CurrentProject.Connection CurrentDb, so CurrentDb returns Nothing. IIRC, in
an ADP you have to use ADO, and write code along these lines:

Dim rst As ADODB.Recordset
Dim f As ADODB.Field

On Error GoTo GetFieldNames_Err

Set rst = New ADODB.Recordset
rst.Open "Table1", CurrentProject.Connection

For Each f In Rst.Fields
' do something
Next
rst.Close
 
D

Dirk Goldgar

Dirk Goldgar said:
E. Kwong said:
I'm trying to extract the field names of a one-row table but keep
gettning "Error 91 : Object variable or With block variable not set".
The code snippet:

Dim rst As DAO.Recordset
Dim f As DAO.Field

On Error GoTo GetFieldNames_Err
Set rst = CurrentDb.OpenRecordset("table1")
For Each f In Rst.Fields
do somthing
Next
rst.Close

The error keeps hitting at the "set rst..." line. Really puzzling.
I've googled about the OpenRecordset method and have tried suggestions on
some of the posts but still to no avail.

I'm using Access 2003, and SQL Server 2000 tables. The codes are in a
Project (.adp). The References include DAO 3.6 object library and ADO
2.1 library.

Any insight greatly appreciated.


I'm pretty sure that in an ADP, there is no "current database", only
CurrentProject.Connection CurrentDb, [...]

To correct my typo, that should read:

I'm pretty sure that in an ADP, there is no "current database", only
CurrentProject.Connection, [...]
 
E

E. Kwong

Thanks. Will try.


Dirk Goldgar said:
I'm pretty sure that in an ADP, there is no "current database", only
CurrentProject.Connection CurrentDb, so CurrentDb returns Nothing. IIRC,
in an ADP you have to use ADO, and write code along these lines:

Dim rst As ADODB.Recordset
Dim f As ADODB.Field

On Error GoTo GetFieldNames_Err

Set rst = New ADODB.Recordset
rst.Open "Table1", CurrentProject.Connection

For Each f In Rst.Fields
' do something
Next
rst.Close

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top