I have this code written that lets me relink my SQL server links to a
specific SQL server (via a different DSN). The problem I have is that I
have to enter the password when I run it. Can any one show me how to
code the password in so the user doesn't have to deal wtih that?
Thanks. Here's the code:
---- BEGIN CODE
Public Sub ReConnectTables ()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim td2 As DAO.TableDef
Dim strTable As String
Dim strMsg As String
On Error GoTo lblErr
strMsg = "Tables were relinked successfully."
' Get rid of any old links.
'Call DeleteLinks
' Create a recordset to obtain server object names.
Set db = CurrentDb
' Walk through list of tables and create the links.
For Each tdf In CurrentDb.Table Defs
With tdf
' Delete only SQL Server tables.
If (.Attributes And dbAttachedODBC) = dbAttachedODBC And
Not tdf.Name = "tblTableNa mes" Then
DoCmd.Rename "bak_" & tdf.Name, acTable, tdf.Name
strTable = tdf.Name '"dbo." ' rst!SQLTable
' Create a new TableDef object.
Set td2 = db.CreateTableD ef(strTable)
' Set the Connect property to establish the link.
td2.Connect = "ODBC;DSN=MyDSN ;" & _
"Descriptio n=My Target Server;" & _
"UID=MyUserID;A PP=Microsoft Office 2003;" &
_
"WSID=MyWorkSta tionID;DATABASE =TargetDB;" &
_
"TABLE=dbo. " & strTable
td2.SourceTable Name = strTable
' Append to the database's TableDefs collection.
db.TableDefs.Ap pend td2
'DELETE old table
DBEngine(0)(0). Execute "DROP TABLE [" & "bak_" &
tdf.Name & "]"
End If
End With
Next tdf
lblExit:
Set tdf = Nothing
Set db = Nothing
MsgBox strMsg, , "Link SQL Tables"
Exit Sub
lblErr:
Select Case Err
Case Else
ErrMsgFunction( )
Resume lblExit
End Select
Resume
End Sub
---- END CODE
specific SQL server (via a different DSN). The problem I have is that I
have to enter the password when I run it. Can any one show me how to
code the password in so the user doesn't have to deal wtih that?
Thanks. Here's the code:
---- BEGIN CODE
Public Sub ReConnectTables ()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim td2 As DAO.TableDef
Dim strTable As String
Dim strMsg As String
On Error GoTo lblErr
strMsg = "Tables were relinked successfully."
' Get rid of any old links.
'Call DeleteLinks
' Create a recordset to obtain server object names.
Set db = CurrentDb
' Walk through list of tables and create the links.
For Each tdf In CurrentDb.Table Defs
With tdf
' Delete only SQL Server tables.
If (.Attributes And dbAttachedODBC) = dbAttachedODBC And
Not tdf.Name = "tblTableNa mes" Then
DoCmd.Rename "bak_" & tdf.Name, acTable, tdf.Name
strTable = tdf.Name '"dbo." ' rst!SQLTable
' Create a new TableDef object.
Set td2 = db.CreateTableD ef(strTable)
' Set the Connect property to establish the link.
td2.Connect = "ODBC;DSN=MyDSN ;" & _
"Descriptio n=My Target Server;" & _
"UID=MyUserID;A PP=Microsoft Office 2003;" &
_
"WSID=MyWorkSta tionID;DATABASE =TargetDB;" &
_
"TABLE=dbo. " & strTable
td2.SourceTable Name = strTable
' Append to the database's TableDefs collection.
db.TableDefs.Ap pend td2
'DELETE old table
DBEngine(0)(0). Execute "DROP TABLE [" & "bak_" &
tdf.Name & "]"
End If
End With
Next tdf
lblExit:
Set tdf = Nothing
Set db = Nothing
MsgBox strMsg, , "Link SQL Tables"
Exit Sub
lblErr:
Select Case Err
Case Else
ErrMsgFunction( )
Resume lblExit
End Select
Resume
End Sub
---- END CODE
Comment