I’m fighting with a VBA non-interactive problem that really bugs me: I have a pass-through query to a mysql database, which works well if double-clicked by the user. But it fails if called from VBA automation session (called from excel) if no interaction was done first. Most strange: it works from VBA after clicking it on the GUI for a while (odbc mysql connection timeout possibly).
The passthrough-query has it’s password in the DSN and in the connection string (to sort out problems with the store). The behavior is the same with a linked table.
Problematic VBA code called looks like this:
CurrentProject.Connection.Execute "INSERT INTO [SomeLocalTable] (id) SELECT id FROM [somePassThroughOrLinkedMySQLTable]"
The error is a generic odbc connection failure 80004005.
While this type of query works all the time:
Dim cnn As New ADODB.Connection
cnn.Open ("Driver=MySQL ODBC 5.2w Driver;SERVER=myserver;UID=user;DATABASE={db};PORT=3306;DFLT_BIGINT_BIND_STR=1;PWD=secret")
Can I “initialize” the passthrough query like the UI does to make it work? Or can I use the second type of query to insert into a local MS Access table?
Environment: Win8-64bit, Office2013, mysql-odbc-5.2w
I think the problem is happening because you are referring to the Access database as an external ADODB data source, instead I would use an instance of Access in order to run the query via DAO. The sample sub below creates an invisible instance of Access and executes the query, and then quits once finished. I’ve used late binding in this example with DAO version 3.6 so you may need to amend that bit slightly or use early binding:
This was coded using Excel 2003 but should be easy enough to translate to later versions of office.