Last active
December 15, 2015 06:39
-
-
Save mrowles/5218259 to your computer and use it in GitHub Desktop.
Microsoft Access: The RefreshODBCLinks will refresh your ODBC connections each time you wish to make a query/connection to the backend database. In this example, the RefreshODBC function is called when opening the Access Database. It can also be called before using queries in your code to ensure the data is always correct and up to date and the …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'RefreshODBCLinks() | |
'Refreshes the ODBC links to ensure the latest data is being pulled through and is correct | |
Function RefreshODBCLinks() As Boolean | |
Dim sConnStr As String | |
sConnStr = "ODBC;" _ | |
& "DATABASE={Database Name e.g., theDatabase};" _ | |
& "DSN={ODBC Connection Name e.g., theDB};" _ | |
& "LANGUAGE={Lanauge of Database e.g., British};" _ | |
& "DRIVER={SQL Version e.g., SQL Server Native Client 10.0};" _ | |
& "APP={Version of App e.g., 2007 Microsoft Office system};" | |
Dim db As DAO.Database | |
Dim tb As DAO.TableDef | |
Set db = CurrentDb | |
For Each tb In db.TableDefs | |
If Left(tb.Connect, 4) = "ODBC" Then | |
tb.Connect = sConnStr | |
tb.RefreshLink | |
Debug.Print "Refreshed ODBC table " & tb.Name | |
End If | |
Next tb | |
Set db = Nothing | |
RefreshODBCLinks = True | |
End Function | |
'auto_exec() - Executes on Database open | |
Public Function auto_exec() | |
'Call a refresh on the ODBC links when database opens initially | |
If Not (RefreshODBCLinks) Then | |
MsgBox "Could not connect to the database. Please restart the application and try again.", vbCritical | |
Exit Function | |
End If | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment