View Full Version : Help?


aziz rasul
05-16-2002, 01:51 AM
Can anyone tell me why I get error 91 (i.e. Object variable or With block variable not set) for the following code: -

Public Function Method_Three()
On Error GoTo Err_Method_Three

Dim dbs As Database
Dim tdf As TableDef
Dim rst As Recordset
Dim qdfDelete_1 As QueryDef
Dim qdfDelete_2 As QueryDef
Dim strSQL_1 As String, strSQL_2 As String

strSQL_1 = "DELETE [tblTable - Query Collection].* FROM [tblTable - Query Collection];"

strSQL_2 = "Delete [tblTable - Query Collection].[DAO Name] FROM [tblTable - Query Collection] WHERE "
strSQL_2 = strSQL_2 & "(([tblTable - Query Collection].[DAO Name]) Like ""MSys*""));"

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblTable - Query Collection", dbOpenDynaset)
Set qdfDelete_1 = dbs.CreateQueryDef("", strSQL_1)
Set qdfDelete_2 = dbs.CreateQueryDef("", strSQL_2)

qdfDelete_1.Execute dbFailOnError

With dbs
For Each tdf In .TableDefs
With rst
.AddNew
![DAO Name] = tdf.Name
.Update
End With
Next tdf

End With

qdfDelete_2.Execute dbFailOnError

MsgBox "DAO Collection Updated."

Exit_Method_Three:
Exit Function

Err_Method_Three:
MsgBox Err.Description & "The Error is in Driver Number " & tdf.Name
'Resume Exit_Method_Three

qdfDelete_1.Close
qdfDelete_2.Close
dbs.Close

End Function

The code errors on Set qdfDelete_2

Travis
05-16-2002, 08:06 AM
The Error 91 is caused by this line in your Err_Method_Three:
MsgBox Err.Description & "The Error is in Driver Number " & tdf.Name

tdf is not set at the time your msgbox runs.

I don't know if the error I got to get to this point is what you are getting but I got:

3078

The Microsoft Jet database engine cannot find the input table or query 'tblTable - Query Collection'. Make sure it exists and that its name is spelled correctly.

to debug change the Error Handler to the following:

Err_Method_Three:
Stop
Resume 0
MsgBox Err.Description & "The Error is in Driver Number " & tdf.Name

The "Stop" will stop your code on an Error so you can use F8 to step. "Resume 0" will take you back to the Offending Code.

aziz rasul
05-16-2002, 09:05 AM
The reason you may have received Error 3078 is that you did not have the table "tblTable - Query Collection", which I do have. As far as setting tdf, the whole routine works OK when the line

Set qdfDelete_2 = dbs.CreateQueryDef("",strSQL_2)

is removed. Hence tdf behaves itself at that time. Hence I think the problem lies elsewhere. My own instincts is the SQL statement strSQL_2. The reason that I say that is that I'm not a SQL person.