G
Guest
Hello, I'm trying to have my Access application that has linked tables use
SQL Server Application Role feature. I provided my current code below,
basically it drops all the existing linked tables and then relinks them.
I know that their is a stored procedure called sp_SetAppRole.
Do I have to call this procedure for each table I'm linking?
Thanks
Public Sub DBConnect(sServerName As String, sDBName As String)
Dim sSQL As String
Dim TableName As String
Dim locDB As localDB
Dim rs As Recordset
Dim MyDB As Database
Dim td As TableDef
DoCmd.Hourglass True
SQLConnectSERVER = sServerName
SQLConnectDatabase = sDBName
sServerName = "SERVER=" & sServerName & ";"
sDBName = "DATABASE=" & sDBName & ";"
Set MyDB = DBEngine.Workspaces(0).Databases(0)
sSQL = "Select * From qryDBConnections WHERE Connect Like '*" &
sServerName & "*' AND Connect Like '*" & sDBName & "*'"
Set rs = MyDB.OpenRecordset(sSQL)
If Not rs.EOF Then
SQLCONNECTSTRING = "ODBC" & _
";DRIVER={SQL SERVER}" & _
";SERVER=" & SQLConnectSERVER & _
";AppName=" & gsAppName & _
";DATABASE=" & SQLConnectDatabase & _
";TRUSTED_CONNECTION=Yes;"
SQLParamQryString = "DRIVER={SQL SERVER}" & _
";SERVER=" & SQLConnectSERVER & _
";AppName=" & gsAppName & _
";DATABASE=" & SQLConnectDatabase & _
";TRUSTED_CONNECTION=Yes;"
RelinkAllPassThroughQueries
'Cycle through any and all linked tables and remove connection
sSQL = "Select * From qryDBConnections"
Set rs = MyDB.OpenRecordset(sSQL)
Do Until rs.EOF
Debug.Print "Delete " & rs!TableName
On Error Resume Next
DoCmd.DeleteObject acTable, rs!TableName
On Error GoTo Err_Routine
rs.MoveNext
Loop
''''''''''''''''''''
'Cycle through all tables that should be attached and create linked tables
sSQL = "select * from lstTables ORDER BY TableName"
Set rs = MyDB.OpenRecordset(sSQL)
Do Until rs.EOF
Set td = MyDB.CreateTableDef(rs!TableName)
td.Connect = SQLCONNECTSTRING
td.SourceTableName = rs!TableName
MyDB.TableDefs.Append td
If Not IsNull(rs!UniqueIdentifiers) Then
'debug.print rs!TableName
sSQL = "CREATE UNIQUE INDEX PK_" & rs!TableName & " on " &
rs!TableName & "(" & rs!UniqueIdentifiers & ")"
MyDB.Execute sSQL
End If
rs.MoveNext
Loop
End Sub
SQL Server Application Role feature. I provided my current code below,
basically it drops all the existing linked tables and then relinks them.
I know that their is a stored procedure called sp_SetAppRole.
Do I have to call this procedure for each table I'm linking?
Thanks
Public Sub DBConnect(sServerName As String, sDBName As String)
Dim sSQL As String
Dim TableName As String
Dim locDB As localDB
Dim rs As Recordset
Dim MyDB As Database
Dim td As TableDef
DoCmd.Hourglass True
SQLConnectSERVER = sServerName
SQLConnectDatabase = sDBName
sServerName = "SERVER=" & sServerName & ";"
sDBName = "DATABASE=" & sDBName & ";"
Set MyDB = DBEngine.Workspaces(0).Databases(0)
sSQL = "Select * From qryDBConnections WHERE Connect Like '*" &
sServerName & "*' AND Connect Like '*" & sDBName & "*'"
Set rs = MyDB.OpenRecordset(sSQL)
If Not rs.EOF Then
SQLCONNECTSTRING = "ODBC" & _
";DRIVER={SQL SERVER}" & _
";SERVER=" & SQLConnectSERVER & _
";AppName=" & gsAppName & _
";DATABASE=" & SQLConnectDatabase & _
";TRUSTED_CONNECTION=Yes;"
SQLParamQryString = "DRIVER={SQL SERVER}" & _
";SERVER=" & SQLConnectSERVER & _
";AppName=" & gsAppName & _
";DATABASE=" & SQLConnectDatabase & _
";TRUSTED_CONNECTION=Yes;"
RelinkAllPassThroughQueries
'Cycle through any and all linked tables and remove connection
sSQL = "Select * From qryDBConnections"
Set rs = MyDB.OpenRecordset(sSQL)
Do Until rs.EOF
Debug.Print "Delete " & rs!TableName
On Error Resume Next
DoCmd.DeleteObject acTable, rs!TableName
On Error GoTo Err_Routine
rs.MoveNext
Loop
''''''''''''''''''''
'Cycle through all tables that should be attached and create linked tables
sSQL = "select * from lstTables ORDER BY TableName"
Set rs = MyDB.OpenRecordset(sSQL)
Do Until rs.EOF
Set td = MyDB.CreateTableDef(rs!TableName)
td.Connect = SQLCONNECTSTRING
td.SourceTableName = rs!TableName
MyDB.TableDefs.Append td
If Not IsNull(rs!UniqueIdentifiers) Then
'debug.print rs!TableName
sSQL = "CREATE UNIQUE INDEX PK_" & rs!TableName & " on " &
rs!TableName & "(" & rs!UniqueIdentifiers & ")"
MyDB.Execute sSQL
End If
rs.MoveNext
Loop
End Sub