In my access database I have TABLE1 which is a linked table to a SQL 2005 server table. I have a query QUERY1 which selects a subset of TABLE1, manipulates / formats some of its data, and places that data into temporary table TMP_TABLE1 (ie. SELECT * INTO [TMPTABLE1] FROM [TABLE1]). I also have a form FORM1 that has TMP_TABLE1 as its recordsource where I can view and manipulate the data.
Here is my code that accomplishes these tasks:
On Error Resume Next
DoCmd.DeleteObject acTable, "TMPTABLE1"
On Error GoTo 0
DoCmd.SetWarnings False
CurrentDb.Execute "QUERY1", dbSeeChanges
DoEvents
DoCmd.SetWarnings True
DoCmd.OpenForm "FORM1", acNormal, , , , acDialog
The problem I am having is that my FORM1 tries to open before my QUERY1 finishes processing and I get an error stating my table does not exist. I have always experienced CurrentDb.Execute as a synchronous query and have only recently run into this behaviour. If I place a short pause or a loop waiting for the table to be created before opening the form, my procedure will work correctly.
Unfortunately, this is just one example of the underlying problem which I can’t remedy. For example, even if I keep the temporary table and simply delete all records and append all new records, the problem persists. This happens for any instances where I manipulate data in code before displaying it on screen such as INSERT statements, UPDATE statements, DAO.Recordset and ADODB.Recordset objects.
I have tested the compiled and uncompiled client on Windows 7 64-bit, Windows Vista 32-bit and Windows XP 32-bit and the all react the same way. The problem is intermittent and occasionally the query will finish quickly and my form will open correctly but 90% of the time it fails to open.
Does anyone have any ideas on what I can do? Maybe a setting got changed to run queries differently? Could it be a SQL Server 2005 option/setting I need to change?
EDIT:
Below is the most verbose code I could think of to try and wait for the table to be ready and it is still failing. Some times, my execute procedure throws an error saying that TMPTABLE1 already exists even though I delete it at the start of the function.:
On Error Resume Next
DoCmd.DeleteObject acTable, "TMPTABLE1"
On Error GoTo 0
Dim wrk As DAO.Workspace
Set wrk = DBEngine.Workspaces(0)
Dim dbs As DAO.Database
Set dbs = CurrentDb
wrk.BeginTrans
On Error GoTo TransErr
dbs.Execute "QUERY1", dbSeeChanges Or dbFailOnError
wrk.CommitTrans
TransResume:
Dim waitLoop As Long
Do While TableDefExists("_working_ReceivedMaterials") = False
waitLoop = GetTickCount
Do While GetTickCount < waitLoop + 100
DoEvents
Loop
Loop
DoCmd.OpenForm "ReceivedMaterials_Entry", acNormal, , , , acDialog
Exit Function
TransErr:
wrk.Rollback
GoTo TransResume
Access can display strange behaviour sometimes when deleting tables and then re-creating them, therefore I dont’ think your code is tripping over itself, the best solution I think is to edit your form and change the recordset to nothing. Then on your forms “On_Load” event, set the recordset there e.g:
Private Sub Form_Load()
Me.Recordset = “SELECT * FROM TMPTABLE1”
End Sub
Alternatively, do not delete the table, just run “DELETE FROM TMPTABLE1” instead, and then instead of a make table query use “INSERT INTO TMPTABLE1 SELECT * FROM TABLE1”