I have an MS Access database that is connected to SQL server via linked servers.
The linked tables are added using a modified AttachDSNLessTable
procedure:
stConnect = "ODBC;DRIVER=" & stDriverName & ";SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
Set td = CurrentDb.CreateTableDef(stLocalTableName)
td.SourceTableName = stRemoteTableName
td.Connect = stConnect
CurrentDb.TableDefs.Append td
I have a facility within the application to change the logged in user, this will remove all the tabledefs:
For Each td In CurrentDb.TableDefs
If td.Name = stLocalTableName Then
CurrentDb.TableDefs.Delete stLocalTableName
End If
Next
Then it will re-add using the procedure above.
Now this appears to work however if I log in as user1
then change user to user2
without closing Access the connection is made using the user1
credentials, running a view that includes SUSER_NAME()
shows user1
is the logged in user.
Is there any way to force the connection to be reset or to force the user to be changed?
Edit
My entire login function:
Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stDriverName 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
Exit For
End If
Next
If Len(stUsername) = 0 Then
'//Use trusted authentication if stUsername is not supplied.
stConnect = "ODBC;DRIVER=" & stDriverName & ";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=" & stDriverName & ";SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
End If
Set td = CurrentDb.CreateTableDef(stLocalTableName)
td.SourceTableName = stRemoteTableName
td.Connect = stConnect
CurrentDb.TableDefs.Append td
AttachDSNLessTable = ""
Exit Function
AttachDSNLessTable_Err:
AttachDSNLessTable = err.Description
End Function