Function to copy table and create link fails with Error 3011

Viewed 105

I have a procedure to copy a table to the backend and then create a link in the front end. If I step through the code in debug mode it works just fine. When it's running full speed it throws Error 3011 "The Microsoft Office Access database engine could not find the object"

The object clearly must exist because I use the same variables for the copy and link operations.

DoCmd.CopyObject vPathname, vTableName, acTable, ubeTable       
DoCmd.TransferDatabase acLink, "Microsoft Access", vPathname, acTable, vTableName, vTableName
1 Answers

The failure is due to some sort of internal race condition with Access. You need to give the backend time to finish creating the new table object before you can link to it. The CopyObject seems to run asynchronously. I tried a whole bunch of different combinations of refreshing the tables and DoEvents but what ended up fixing it was adding DBEngine.Idle dbRefreshCache

DoCmd.CopyObject vPathname, vTableName, acTable, ubeTable       
DBEngine.Idle dbRefreshCache
DoCmd.TransferDatabase acLink, "Microsoft Access", vPathname, acTable, vTableName, vTableName
Related