Access is giving me errors when I try to export a newly created query.
Run-time error '3011':
The Microsoft Jet database engine could not find the object 'example_query'. Make sure the object exists and that you spell its name and the path name correctly.
However, if I go into debug mode, and continue (F5), then it exports without any errors. Sometimes, if the query is large, it will give the error a second or third time, but eventually Access realizes that the query actually does exist, and will proceed to export it.
This means that there must be some refreshing delays in access. I tried to get around this problem by calling a WaitForQuery sub before exporting, but I still get errors.
Here is a simplified example of my current approach:
Even when the immediate window says that the query exists before exporting, there are still errors. So it seems that even if the query detection method reports that the query exists, Jet still doesn't know about it.
What makes this even more frustrating is that sometimes it will export without any errors, so it may seem like the problem is solved, when in reality, it was just lucky refresh timing.
Is there a way to see what queries jet thinks are part of the database, or to force jet to refresh its query list?
Run-time error '3011':
The Microsoft Jet database engine could not find the object 'example_query'. Make sure the object exists and that you spell its name and the path name correctly.
However, if I go into debug mode, and continue (F5), then it exports without any errors. Sometimes, if the query is large, it will give the error a second or third time, but eventually Access realizes that the query actually does exist, and will proceed to export it.
This means that there must be some refreshing delays in access. I tried to get around this problem by calling a WaitForQuery sub before exporting, but I still get errors.
Here is a simplified example of my current approach:
Code:
Option Compare Database
Option Explicit
'sleep function
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'looks though the list of all queries in the database and returns true if it finds one with a matching name
Public Function QueryExists(queryName As String, db As Database) As Boolean
Dim qdf As QueryDef
QueryExists = False
For Each qdf In db.QueryDefs
If qdf.Name = queryName Then
QueryExists = True
Exit Function
End If
Next qdf
End Function
'depending on what preExisting is set to, this sub either waits for a query to show up or be deleted
Public Sub waitForQuery(q As String, preExisting As Boolean)
Dim c As Integer
c = 0
' if preExisting is set to true, then this section loops until the query is no longer in the database
' if preExisting is set to false, then this section loops until the query is detected in the database
Do While (QueryExists(q, CurrentDb) = preExisting)
Sleep 200
c = c + 1
Loop
If preExisting = True Then
Debug.Print ("Waited " & c & " times for " & q & " to be deleted")
Else
Debug.Print ("Waited " & c & " times for " & q & " to show up")
End If
End Sub
Sub exportExample()
Dim exampleQueryDef As QueryDef
'deletes the query if it is already in the database
If QueryExists("example_query", CurrentDb) = True Then
DoCmd.DeleteObject acQuery, "example_query"
'waits until Access realizes that the query has been deleted
waitForQuery "example_query", True
End If
'make the query
Set exampleQueryDef = CurrentDb.CreateQueryDef("example_query")
'give SQL statement to query
exampleQueryDef.SQL = "SELECT * FROM SomeTable"
'wait until Access detects the query
waitForQuery exampleQueryDef.Name, False
'additional confirmation that the query exists
If QueryExists(exampleQueryDef.Name, CurrentDb) = True Then
Debug.Print "For a second time " & exampleQueryDef.Name & " exists"
End If
'export Query
DoCmd.TransferText acExportDelim, "ExampleSpec", exampleQueryDef.Name, "C:\Documents and Settings\Nathan\Desktop\Output\" & exampleQueryDef.Name & ".txt", True
Set exampleQueryDef = Nothing
End Sub
What makes this even more frustrating is that sometimes it will export without any errors, so it may seem like the problem is solved, when in reality, it was just lucky refresh timing.
Is there a way to see what queries jet thinks are part of the database, or to force jet to refresh its query list?