Share via

Need Help with Runtime Error 91

Anonymous
2012-06-05T01:33:13+00:00

I get a Runtime Error 91 "object variable or with block variable not set" message when I try to run the following code.  I have not been able to solve this issue.  Any help would be appreciated.

Option Compare Database

Option Explicit

Private Sub LadiesTIndex_Click()

On Error GoTo ErrorHandler

Dim dbs As DAO.Database

Dim rsSQL As DAO.Recordset

Dim qdf As DAO.QueryDef

Dim MemberNum As Integer

Dim Differential As Currency

Dim TournDate As Date

Dim mySQL As String

Set dbs = CurrentDb

mySQL = ""

While Not rsSQL.EOF

mySQL = mySQL & "SELECT TOP 50 PERCENT" & _

"qryLTIndexOrder.Differential" & _

"qryLTIndexOrder.MemberNum, qryLTIndexOrder.TournDate," & _

"FROM qryLTIndexOrder," & _

"WHERE qryLTIndexOrder.memberNum = 92127, 93509"

rsSQL.MoveNext

If Not rsSQL.EOF Then mySQL = mySQL

Wend

Set rsSQL = dbs.OpenRecordset(mySQL, dbOpenSnapshot)

With dbs

Set qdf = .CreateQueryDef("tmpMemberNum", mySQL)

DoCmd.OpenQuery "tmpMemberNum"

.QueryDefs.Delete "tmpMemberNum"

End With

dbs.Close

qdf.Close

Exit Sub

ErrorHandler:

MsgBox Err.Description

End Sub

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2012-06-06T04:46:39+00:00

    What is the exact text that it printed? This will be a clue as to why you're getting the error you quoted.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-06-06T04:02:26+00:00

    I placed debug.print mySQL after mySQL and it printed the results of mySQL in the intermediate window.  I found this recommendation in a number of this forums answers.

    Was this answer helpful?

    0 comments No comments
  3. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2012-06-06T03:53:27+00:00

    My crystal ball is broken. What does the debug.print print?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2012-06-05T21:54:10+00:00

    Thank you, I missed that.  Now I get an error message that the "select statement includes a reserved word or an argument that is misspelled or missing, or the punctuation is incorrect". When I add "debug.print mySQL", the result looks correct.  Here is my corrected code.

    Option Compare Database

    Option Explicit

    Private Sub LadiesTIndex_Click()

    On Error GoTo ErrorHandler

    Dim dbs As DAO.Database

    Dim rsSQL As DAO.Recordset

    Dim qdf As DAO.QueryDef

    Dim MemberNum As Integer

    Dim Differential As Currency

    Dim TournDate As Date

    Dim mySQL As String

    Set dbs = CurrentDb

    Set rsSQL = dbs.OpenRecordset("qryLTIndexOrder", dbOpenSnapshot)

    mySQL = ""

    While Not rsSQL.EOF

    mySQL = mySQL & "SELECT TOP 50 PERCENT " & _

    "qryLTIndexOrder.Differential, " & _

    "qryLTIndexOrder.MemberNum, qryLTIndexOrder.TournDate, " & _

    "FROM qryLTIndexOrder, " & _

    "WHERE ((qryLTIndexOrder.MemberNum) = 92127) "

    rsSQL.MoveNext

    If Not rsSQL.EOF Then mySQL = mySQL

    Wend

    With dbs

    Set qdf = .CreateQueryDef("tmpMemberNum", mySQL)

    DoCmd.OpenQuery "tmpMemberNum"

    .QueryDefs.Delete "tmpMemberNum"

    End With

    dbs.Close

    qdf.Close

    Exit Sub

    ErrorHandler:

    MsgBox Err.Description

    End Sub

    Was this answer helpful?

    0 comments No comments
  5. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2012-06-05T03:47:36+00:00

    This line: While Not rsSQL.EOF

    will fail, because rsSQL has not been assigned yet. IOW, there should be a "Set rsSQL = ..." line before it.

    Was this answer helpful?

    0 comments No comments