Error 91 help required

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Help!

I am getting an Error 91 code "Object variable or With
block variable not set"

I get it on
Set rs = CurrentDb.OpenRecordset("qryFirst")
from the following code:

Private Sub cmdSubmit_click()

Dim rs As DAO.Recordset
Dim CurrentDb As Database

Set rs = CurrentDb.OpenRecordset("qryFirst")

rs.MoveLast 'I put this in because I've found
sometimes it doesn't count properly
otherwise...

If rs.RecordCount = 0 Then
MsgBox "blah"
Else
DoCmd.OpenQuery "qryFirst", acViewNormal
End If

End Sub

Any ideas what's causing this error?
 
Sanda, You need to instantiate the database object variable by using the set
statement as you did for the recordset object. I also changed the name of
your variable. Not sure if CurrentDB is a reserved word, but why take
chances!

Dim db As DAO.Database
Set db = CurrentDb()
Set rs = db.OpenRecordset("qryFirst")
 
Thanks Reggie!

But now I am getting 3061 error - "Too few parameters.
Expected 2"

Any ideas?
 
This error indicates either that you have a misspelling in your query, or
else that you have a parameter that is to be supplied at runtime.

If you run the query through the interface, it can call the Expression
Service to resolve references such as:
Forms!MyForm!MyTextBox
You cannot do that in DAO code.
 
Sandra, What Allen is telling you is that the query you're passing to the rs
object (qryFirst) has some parameters(criteria) that is required and/or if
that query is based on other queries that those queries require parameters.
When you are using DAO to open the recordset those parameters are not
available. Just thought I'd try to clarify it a little for you. What is it
you are actually trying to accomplish? Maybe we can point you in the right
direction.
 
Back
Top