Error 91: Object variable or With block variable not set (1 Viewer)

sachin.modak

New member
Local time
Today, 21:03
Joined
Oct 29, 2007
Messages
3
Hi,

I'm having 2 functions in my application in two different modules .
They are as follows.

Public Function GetNextNumber() As Variant
Dim strSQL As String
Dim rs As Recordset


GetNextNumber= Null
// creating sql to execute MYSP stored procedures with 2 parameteres

strSQL = "EXEC MySP '" & param 1 & "','" & param 2 & "'"

Set rs = ExecProc(strSQL)

If (rs Is Nothing) Or (rs.EOF And rs.BOF) Then
Call ShowErrors
Else
GetNextNumber= rs!Value
End If

ExitPoint:

On Error Resume Next
rs.Close
Set rs = Nothing
Exit Function

ErrHandler:

MsgBox err.Number & " - " & err.Description
Resume ExitPoint

End Function


Function ExecProc(strCommandText As String) As Recordset

Dim db As Database
Dim qdef As QueryDef
Dim rst As Recordset


On Error GoTo ErrHandler

Set db = CurrentDb()
Set qdef = db.CreateQueryDef("")

qdef.Connect = CONNECTSTRING
qdef.ODBCTimeout = 600
qdef.ReturnsRecords = True
qdef.Sql = strCommandText



Set rst = qdef.OpenRecordset()
Debug.Print rst.Fields(0).Value
Set ExecProc = rst

rst.Close
Set rst = Nothing
Set qdef = Nothing


ExitPoint:

On Error Resume Next
qdef.Close
Set qdef = Nothing

Exit Function

ErrHandler:

Set ExecProc = Nothing
Resume ExitPoint

End Function

When I'm trying to execute stored procedure thru MS Access.I'm getting error at line indicated by red color.
the error is (Error 91:Object variable or With block variable not set.)

So can anyone help me out ?

Thanks in advance.

Regards,
Sachin
 

ajetrumpet

Banned
Local time
Today, 10:33
Joined
Jun 22, 2007
Messages
5,638
Dim db As Database
Dim qdef As QueryDef
Dim rst As Recordset


On Error GoTo ErrHandler

Set db = CurrentDb()
Set qdef = db.CreateQueryDef("") <---create a definition with the name BLANK??

qdef.Connect = CONNECTSTRING
qdef.ODBCTimeout = 600
qdef.ReturnsRecords = True
qdef.Sql = strCommandText



Set rst = qdef.OpenRecordset() <---need to specify the name of the set to open... " " ????
Debug.Print rst.Fields(0).Value
Set ExecProc = rst

rst.Close
Set rst = Nothing
Set qdef = Nothing
I think the way you have it now, you are trying to ask Access to open a set with the name NULL. That's the way it reads to me.
You have to specify the name of the set to open with that syntax. That is the only required part of the line.
 

sachin.modak

New member
Local time
Today, 21:03
Joined
Oct 29, 2007
Messages
3
Thanks

Hi,
Can u please explain with a small snippet of example?
Coz I've tried lot thru' many ways to resolve this issue but still I'm not getting any solution.


Regards,

Sachin
 

unclejoe

Registered User.
Local time
Today, 23:33
Joined
Dec 27, 2004
Messages
190
Hi,

Here's from Help. You could do something like this.

Code:
CreateQueryDef Method, OpenRecordset Method, and SQL 
Property Example (Client/Server)

This example uses the CreateQueryDef and OpenRecordset methods and 
the SQL property to query the table of titles in the Microsoft SQL 
Server sample database Pubs and return the title and title identifier of 
the best-selling book. The example then queries the table of authors 
and instructs the user to send a bonus check to each author based on his 
or her royalty share (the total bonus is $1,000 and each author 
should receive a percentage of that amount).

Sub ClientServerX2()

    Dim dbsCurrent As Database
    Dim qdfBestSellers As QueryDef
    Dim qdfBonusEarners As QueryDef
    Dim rstTopSeller As Recordset
    Dim rstBonusRecipients As Recordset
    Dim strAuthorList As String

    ' Open a database from which QueryDef objects can be 
    ' created.
    Set dbsCurrent = OpenDatabase("DB1.mdb")

    ' Create a temporary QueryDef object to retrieve
    ' data from a Microsoft SQL Server database.
    Set qdfBestSellers = dbsCurrent.CreateQueryDef("")
    With qdfBestSellers
        .Connect = "ODBC;DATABASE=pubs;UID=sa;PWD=;" & _
             "DSN=Publishers"
        .SQL = "SELECT title, title_id FROM titles " & _
            "ORDER BY ytd_sales DESC"
        Set rstTopSeller = .OpenRecordset()
        rstTopSeller.MoveFirst
    End With

    ' Create a temporary QueryDef to retrieve data from
    ' a Microsoft SQL Server database based on the results from
    ' the first query.
    Set qdfBonusEarners = dbsCurrent.CreateQueryDef("")
    With qdfBonusEarners
        .Connect = "ODBC;DATABASE=pubs;UID=sa;PWD=;" & _
            "DSN=Publishers"
        .SQL = "SELECT * FROM titleauthor " & _
            "WHERE title_id = '" & _
            rstTopSeller!title_id & "'"
        Set rstBonusRecipients = .OpenRecordset()
    End With

    ' Build the output string.
    With rstBonusRecipients
        Do While Not .EOF
            strAuthorList = strAuthorList & "  " & _
                !au_id & ":  $" & (10 * !royaltyper) & vbCr
            .MoveNext
        Loop
    End With

    ' Display results.
    MsgBox "Please send a check to the following " & _
        "authors in the amounts shown:" & vbCr & _
        strAuthorList & "for outstanding sales of " & _
        rstTopSeller!Title & "."

    rstTopSeller.Close
    dbsCurrent.Close

End Sub

Hi,
Can u please explain with a small snippet of example?
Coz I've tried lot thru' many ways to resolve this issue but still I'm not getting any solution.
Regards,
Sachin
 

ajetrumpet

Banned
Local time
Today, 10:33
Joined
Jun 22, 2007
Messages
5,638
Hi,
Can u please explain with a small snippet of example?
An example of what??? "CreateQueryDef" syntax???

From looking at your module, I am guessing that you are trying to access data from an outside source using the QueryDef. If that is the case, before you do any of that, you have to name the object. The syntax is...
Code:
Db.CreateQueryDef("NAME", "SQL, if appropriate")
Isn't that what you are trying to do?? You have a 600 second delay for the SQL pass-through, correct???

I guess what I should say now is...what are you trying to do with a QueryDef???
 
Last edited:

sachin.modak

New member
Local time
Today, 21:03
Joined
Oct 29, 2007
Messages
3
Yes, U r absolutely right.
My store procedure is in SQL server 2000.
And I'm executing the same from MS Access.
While setting the recordset object.i'm getting an error.
 

ajetrumpet

Banned
Local time
Today, 10:33
Joined
Jun 22, 2007
Messages
5,638
Take the ( ) off the end of the OpenRecordSet Command. I bet you it's looking for an object name because those are present. Looks like you already created the temporary Def, so that shouldn't be the problem (I wouldn't think).

I am not that familiar with SQL server, but have you tried posting this question in that forum???
 

RoyVidar

Registered User.
Local time
Today, 17:33
Joined
Sep 25, 2000
Messages
805
I wasn't aware you could do stuff like that with DAO, but with ADO, it should be quite easy. You need to establish a connection to the db - see for instance http://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForSQLServer, then
Code:
dim rs               as adodb.recordset
dim cn               as adodb.connection

set cn = new adodb.connection
cn.open <check out the above link>

' for readonly forwardonly recordset
set rs = cn.execute("EXEC MySP '" & param1 & "','" & param2 & "'", ,adCmdText)

' for other cursor/locktype
dim cmd              as adodb.command
set cmd = new adodb.command
with cmd
    set .activeconnection = cn
    .commandtype = admcdtext
    .commandtext = "EXEC MySP '" & param1 & "','" & param2 & "'"
end with
set rs = new adodb.recordset
with rs
    .locktype = adlockreadonly
    .cursortype = adopenstatic
    .open cmd
end with

' check it
debug.print rs.getstring
(air code)
 

Users who are viewing this thread

Top Bottom