Discussion Locked

Run time error 91 Object variable or ..

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello,

I am having difficulty resolving the set instruction below.

I get runtime error 91 object variable or with block variable not set

I have tried many ways of handling this.

Public Sub LoadFindCbo()
Dim db As Database
Dim rs As Recordset
Dim sql As String
frmFind.cboCode.Clear
sql = "Select distinct code from PersonnelCommon order by code"

Set rs = dbEFC.OpenRecordset(sql) ' e r r o r

If rs.RecordCount > 0 Then
rs.MoveFirst
Do While rs.EOF = False
frmFind.cboCode.AddItem NoNull(rs!code)
rs.MoveNext
Loop
End If
rs.Close
End Sub

Thank you.

10 replies

by smart_santanu

Set rs = dbEFC.OpenRecordset(sql) ' e r r o r

where from do you get dbEFC? Probably it is a global variable. Check that you have properly initialised it like
set dbEFC = CreateObject(....)

by madestroitsolutions

This error is simply telling you that the object "dbEFC" has bot been set or instantiated.

OpenRecordSet works with either a Connection or a Database. In this case I can fairly say "dbEFC" must be a database (db), but you have to make sure it hasbeen properly instantiated, because the recordset will be set based on the information retrieved from it. For example this must be declared somewhere:

Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
Dim dbEFC As Database
Set dbEFC = wrkJet.OpenDatabase("yourdb.mdb")

==============================================
A little piece of advice:
Why not use ADO instead?
just a thought

Good Luck!

by dsa

add this to your code.

set db = dbengine(0)(0)

Check you sql statement.

set rs = db.OpenRecordset(sql, dbOpenDynaset)
rs.MoveFirst
If rs.Bof and rs.Eof then
' Empty
Else
Add you loop statement here
Endif

by rrv

Straight to answer.
This error occurs most of the time when you try to use any function expected to be in a dll and the dll is not instantiated or in memory. For example in your case the object dbEFC is not opened yet. This can be either your DAO connection is not done properly or incorrect version of DAO or simply the scope of your DB connection is void.

Use ADO connection to your database and check the status of connection before opening any recordset. Sample
set dbEFC=new ADODB.Connection
dbEFC.cursorlocation=aduseclient
dbEFC.connectionstring="dsn=yourdsn"
dbEFC.open

if dbEFC.State=adstateclosed then
msgbox "Connection to database failed !"
else
msgbox "Successfully connected."
set rsEFC=new ADODB.Recordset
rsEFC.open "Select your data",dbEFC,adopenforwardonly,adlockreadonly
if rsEFC.state=adstateclosed then
msgbox "No Data selected
else
do while not rsEFC.EOF
'Do your data processing
rsEFC.movenext
loop
endif
endif

of course you should have a reference to Microsoft ActiveX Data objects [some version above 2.0]

RV

This discussion is closed to new replies.