I am attempting to relink a handful of pass through queries. I have found the following codes to relink the queries, though I am unsure of how to use them. Could somemone provide some insight as to how I would get these queries updated using either of the codes below? More specifically, where do I place these codes? Into the query themselves?
Code:
[INDENT]Dim qdf As dao.QueryDef
Dim dbs As dao.Database
Set dbs = CurrentDb
create connection string
Dim strConnect As String
strConnect = "ODBC;DRIVER={SQL Native Client}" _
& ";SERVER=server_name" _
& ";DATABASE=database_name" _
& ";UID=my_user" _
& ";Trusted_Connection=Yes" & ";"
For Each qdf In dbs.QueryDefs
If qdf.Type = dbQSQLPassThrough Then
qdf.Connect = strConnect
End If
Next qdf
dbs.Close [/INDENT]
Code:
[INDENT]Sub RelinkSQLQueries()
Dim db As Database
Set db = CurrentDb
Dim qdef As QueryDef
Dim constr As Variant
constr = DLookup("ConnectionString", "tblConnections", "Active = True")
For Each qdef In db.QueryDefs
If InStr(qdef.Connect, "ODBC") Then
qdef.Connect = constr
End If
Next
MsgBox "Re link completed"
End Sub[/INDENT]