A2003, but this behaviour also occurred during the same DAO process I'm
about to describe in A97.
I have a sub procedure which takes two arguments: a querydef name; and
an SQL statement constructed from the calling procedure and constructs
or modifies a querydef. The sub runs through the querydefs collection -
if it finds the querydef name argument, it takes the existing query and
modifies the SQL. If not found, a new querydef is created.
The problem is this. It works fine when the mdb is first opened and the
querydef in question has been previously deleted - it finds that there
is no querydef by the argument name and creates a new one. However, if
during a session I delete a querydef through the database window, the
function still finds the querydef exists! I've tried using
querydefs.refresh at the beginning of the function to no avail.
The error is (in this case, the query name being passed is
"qryCriteriaUserList":
Error 3011 The Microsoft Jet database engine could not find the object
'qryCriteriaUserList'. Make sure the object exists and that you spell
its name and the path name correctly.
When I close the app and reopen it, things go as you would expect, with
no error (the query gets created).
Here's the code I use (this is for pass through queries, but I don't
think this matters).
Thanks very much in advance for any suggestions.
Sub sCreatePT(strSql As String, strQryName As String)
'Takes an SQL string (strSql) and a query name (strQryName)
'and either creates a new PT query or, if
'it exists already, changes the SQL string
Dim qdf1 As DAO.QueryDef
Dim booFound As Boolean
On Error GoTo Err_Proc
'I just added the following, it still coughs up
Access.CurrentDb.QueryDefs.Refresh
booFound = False
For Each qdf1 In dbLocal.QueryDefs
If qdf1.Name = strQryName Then
booFound = True
Exit For
End If
Next
If booFound = False Then 'querydef does not exist, so create it
Set qdf1 = dbLocal.CreateQueryDef(strQryName)
Else 'already exists
Set qdf1 = dbLocal.QueryDefs(strQryName)
End If
'Now assign pass through characteristics
With qdf1
.Connect = cTmarConnect
.SQL = strSql
.ReturnsRecords = True
End With
Exit_Proc:
On Error Resume Next 'use brute force to close
qdf1.Close
Set qdf1 = Nothing
On Error GoTo 0
Exit Sub
Err_Proc:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & " " & Err.Description,
vbCritical, "sCreatePT", Err.HelpFile, Err.HelpContext
Resume Exit_Proc
End Select
End Sub
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me