lightinthedark
Technical User
Hi.
I'm setting up a system using MS Access 2003 as a front end to a MySQL database using linked tables. The tables would show all data when I first linked them, but after closing and re-openning Access, I was presented with "ODBC connection failed". Using the Linked table manager provided a fix until I next closed Access... clearly not ideal.
I wrote the following code to refresh the link with the intention of having it run automatically every time I started Access; I found that having run it once, the linked table behaves itself now (as of an hour ago). Not sure why this fixes the failed connection, or why the connection failed in the first place (any insight would be welcome), but I know it works now.
---------------------------------------
Sub Link_Refresh()
MsgBox ("Refreshing link")
' This procedure is to get around Access
' failing to keep it's connection
' to a MySQL database once closed
Dim curDb As Database
Dim tdfLinked As TableDef
' Create a reference to the linked table
Set curDb = CurrentDb
Set tdfLinked = curDb.TableDefs("yourtablename")
' Create the connection to the external DSN
tdfLinked.Connect = "ODBC;DATABASE=mysqldatabasename; _
DSN=mysqlDSNname"
tdfLinked.RefreshLink
MsgBox ("Link refreshed")
End Sub
---------------------------------------
I don't claim that this is the best way of doing this, just that it worked for me.
Hope this helps someone.
Hello
-Lightinthedark
I'm setting up a system using MS Access 2003 as a front end to a MySQL database using linked tables. The tables would show all data when I first linked them, but after closing and re-openning Access, I was presented with "ODBC connection failed". Using the Linked table manager provided a fix until I next closed Access... clearly not ideal.
I wrote the following code to refresh the link with the intention of having it run automatically every time I started Access; I found that having run it once, the linked table behaves itself now (as of an hour ago). Not sure why this fixes the failed connection, or why the connection failed in the first place (any insight would be welcome), but I know it works now.
---------------------------------------
Sub Link_Refresh()
MsgBox ("Refreshing link")
' This procedure is to get around Access
' failing to keep it's connection
' to a MySQL database once closed
Dim curDb As Database
Dim tdfLinked As TableDef
' Create a reference to the linked table
Set curDb = CurrentDb
Set tdfLinked = curDb.TableDefs("yourtablename")
' Create the connection to the external DSN
tdfLinked.Connect = "ODBC;DATABASE=mysqldatabasename; _
DSN=mysqlDSNname"
tdfLinked.RefreshLink
MsgBox ("Link refreshed")
End Sub
---------------------------------------
I don't claim that this is the best way of doing this, just that it worked for me.
Hope this helps someone.
Hello
-Lightinthedark