How to execute and query in VBA in MS Access using the ODBC Connector for MySQl?

592 Views Asked by At

The following VBA code will allow you to run a sql query in the current access database

I tried this in a MS Access file that is connected to a MySQL database through the ODBC Connector. However this code does not work, presumably because it's not a normal access DB.

How can I query the database using the existing ODBC connection?

EDIT: I always get a "Runtime error 3024 cannot find file"

Try 1

Dim mydb As Database
Dim myrs As DAO.Recordset


Set mydb = CurrentDb

Set myrs = mydb.OpenRecordset("select from blah blah...")

Try 2

Dim dbMine As DAO.Database
Set dbMine = CurrentDb

Dim qr As String
qr = "select count(*) as `result` from floatinp_appdb2.clientes where cliente_email = '[email protected]';"
'debug.print qr
dbMine.Execute qr, dbFailOnError
2

There are 2 best solutions below

4
Albert D. Kallal On

Requirements are: Linked table has a PK defined.

So, this should work:

Set myrs = mydb.OpenRecordset("select from blah blah...",dbOpenDynaset, dbSeeChanges)

Also: any true/false columns (bit fields) should have default value of 0 set. (don't allow nulls for the true/false columns).

You can and should also consider adding a rowversion column, but above should suffice.

0
Calvin On

Though the error was "runtime error 3024 cannot find file", my mistake was including the database name before the table

e.g. Select * From DB2.Table1