an example of my ODBC usage:
Public Const cnProvider As String = "MSDataShape" '"SQLOLEDB"
Public Const cnSecurity As String = "SSPI"
Public Const cnSrvName As String = "XXXXXXXX"
Public Const cnTimeOut As Integer = 900
Public DBCnn As New ADODB.Connection
Public cnDBName As String
Public Function fSetADOConnection()
On Error GoTo cnADOErr
'This is for startup initialization - set the connection database
If Len(cnDBName) < 1 Then
Dim strDbName As String
'format the string
strDbName = CurrentDb.TableDefs(1).Connect
strDbName = Right(strDbName, (Len(strDbName)) - InStr(1, strDbName, "Database", vbTextCompare) - Len("Database"))
strDbName = Left(strDbName, Len(DatabaseNameFormat))
'set the Public connection name to the formated string
cnDBName = strDbName
End If
With DBCnn
.Provider = cnProvider
.ConnectionString = "DATA PROVIDER=SQLOLEDB;"
.CommandTimeout = cnTimeOut
.Properties("Data Source") = cnSrvName
.Properties("Initial Catalog") = cnDBName
.Properties("Integrated Security") = cnSecurity
.Open
End With
Exit Function
cnADOErr:
DBCnn.Close
Resume Next
End Function
When I'm changing between Databases, I always change where my tables are pointing or I change a global named cnDBName.
Essentially, you need to close your ODBC connection and reopen it. the .Close Method of your connection and then reconnect.
I hope this helps.
Be safe,
Randall Vollen
National City Bank Corp.