Method 1: Use the CreateTableDef method
The CreateTableDef method creates a linked table. To use this method, create a new module, and then add the following AttachDSNLessTable function to the new module.
Copy the code The code is as follows:
'// Name: AttachDSNLessTable
'// Purpose: Create a linked table to SQL Server without using a DSN
'// Parameters
'// stLocalTableName: Name of the table that you are creating in the current database
'// stRemoteTableName: Name of the table that you are linking to on the SQL Server database
'// stServer: Name of the SQL Server that you are linking to
'// stDatabase: Name of the SQL Server database that you are linking to
'// stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
'// stPassword: SQL Server user password
Function AttachDSNLessTable (stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
On Error GoTo AttachDSNLessTable_Err
Dim td As TableDef
Dim stConnect As String
For Each td In CurrentDb.TableDefs
If td.Name = stLocalTableName Then
CurrentDb.TableDefs.Delete stLocalTableName
End If
Next
If Len (stUsername) = 0 Then
'// Use trusted authentication if stUsername is not supplied.
stConnect = "ODBC; DRIVER = SQL Server; SERVER =" & stServer & "; DATABASE =" & stDatabase & "; Trusted_Connection = Yes"
Else
'// WARNING: This will save the username and the password with the linked table information.
stConnect = "ODBC; DRIVER = SQL Server; SERVER =" & stServer & "; DATABASE =" & stDatabase & "; UID =" & stUsername & "; PWD =" & stPassword
End If
Set td = CurrentDb.CreateTableDef (stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
CurrentDb.TableDefs.Append td
AttachDSNLessTable = True
Exit Function
AttachDSNLessTable_Err:
AttachDSNLessTable = False
MsgBox "AttachDSNLessTable encountered an unexpected error:" & Err.Description
End Function
To call the AttachDSNLessTable function, call the code, which is similar to one of the following code examples in the Autoexec macro or in the Form_Open event of the startup form:
• When you use Autoexec, call the AttachDSNLessTable function, and then pass the parameters as shown below from the RunCode operation.
AttachDSNLessTable ("authors", "authors", "(local)", "pubs", "", "")
• When you use the startup form, the code will be similar to the following with the Form_Open event.
Private Sub Form_Open (Cancel As Integer)
If AttachDSNLessTable ("authors", "authors", "(local)", "pubs", "", "") Then
'// All is okay.
Else
'// Not okay.
End If
End Sub
When adding multiple linked tables to an Access database, you must adjust the programming logic.
Method 2: Use the DAO.RegisterDatabase method
The DAO.RegisterDatabase method creates a DSN connection in the Autoexec macro or in the startup form. Although this method does not delete the connection to the DSN, it does not help you solve the problem by creating the DSN connection in code. To use this method, create a new module, and then add the following CreateDSNConnection function to the new module.
'// Name: CreateDSNConnection
'// Purpose: Create a DSN to link tables to SQL Server
'// Parameters
'// stServer: Name of SQL Server that you are linking to
'// stDatabase: Name of the SQL Server database that you are linking to
'// stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
'// stPassword: SQL Server user password
Function CreateDSNConnection (stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String) As Boolean
On Error GoTo CreateDSNConnection_Err
Dim stConnect As String
If Len (stUsername) = 0 Then
'// Use trusted authentication if stUsername is not supplied.
stConnect = "Description = myDSN" & vbCr & "SERVER =" & stServer & vbCr & "DATABASE =" & stDatabase & vbCr & "Trusted_Connection = Yes"
Else
stConnect = "Description = myDSN" & vbCr & "SERVER =" & stServer & vbCr & "DATABASE =" & stDatabase & vbCr
End If
DBEngine.RegisterDatabase "myDSN", "SQL Server", True, stConnect
'// Add error checking.
CreateDSNConnection = True
Exit Function
CreateDSNConnection_Err:
CreateDSNConnection = False
MsgBox "CreateDSNConnection encountered an unexpected error:" & Err.Description
End Function
Note If you call the RegisterDatabase method again, the DSN is updated.
To call the CreateDSNConnection function, please code it similar to one of the following code examples in the Autoexec macro or in the Form_Open event of the startup form:
• When you use Autoexec, call the CreateDSNConnection function, and then pass the parameters, as shown below, from the RunCode operation.
CreateDSNConnection ("(local)", "pubs", "", "")
• When you use the startup form, the code will be similar to the following with the Form_Open event.
Private Sub Form_Open (Cancel As Integer)
If CreateDSNConnection ("(local)", "pubs", "", "") Then
'// All is okay.
Else
'// Not okay.
End If
End Sub
Note This method assumes that by using "myDSN" as the DSN name, you have created a linked SQLServer table in the Access database.
For more information about the CreateTableDef method, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A289.asp (http://msdn.microsoft.com/library /default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A289.asp)
For the RegisterDatabase method, please visit the following MSDN Web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A2EA.asp (http://msdn.microsoft.com/library /default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A2EA.asp)