Dim adorst As New ADODB.Recordset
Dim adoconn As New ADODB.Connection
Private Sub Command1_Click()
Dim strConn As String
adoconn.Open "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=master;User Id=<User ID>;Password=<Strong Password>;"
adoconn.Execute "EXEC sp_addlinkedserver 'SecuredJetLS', 'Jet 4.0', 'Microsoft.Jet.OLEDB.4.0', 'c:\.....\MyDatabase.mdb'"
adoconn.Execute "EXEC sp_addlinkedsrvlogin 'SecuredJetLS', FALSE, NULL, 'UserName', 'Password'"
adoconn.Close
MsgBox "Successful Setup"
End Sub
Private Sub Command2_Click()
Dim SQL As String
' Using OpenQuery syntax.
SQL = " Select a.* from OPENQUERY(SecuredJetLS, 'Select * from MyTable') a"
' Using OpenRowset syntax.
' SQL = "SELECT * From OpenRowset('Microsoft.Jet.OLEDB.4.0','c:\....\MyDatabase.mdb'; 'UserName';'Password', MyTable)"
' Using four-part name syntax.
' SQL = "Select * from SecuredJetLS...MyTable"
adoconn.CursorLocation = adUseClient
adoconn.Open "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=master;User Id=<User ID>;Password=<Strong Password>;"
adorst.Open SQL, adoconn, adOpenStatic, adLockReadOnly
Set DataGrid1.DataSource = adorst
End Sub
Private Sub Form_Load()
Command1.Caption = "Setup Linked Server"
Command2.Caption = "Query Linked Server"
End Sub
Private Sub Form_Unload(Cancel As Integer)
adorst.Close
Set adorst = Nothing
adoconn.Close
Set adoconn = Nothing
End Sub