我创建了下面的代码来测试我是否可以运行查询并从 SQL 服务器表中检索数据。到目前为止,我可以使用 a 返回结果MessageBox
,但不知何故,我只是不知道如何使用此连接来更新此 Access 文件中的表。基本上我想用这个作为前端文件。然后,当表单打开时,它会自动更新此访问文件中的表,并将数据作为列表加载到组合框中。我尝试在这里搜索它并在这里和谷歌中阅读了许多讨论,但目前我找不到正确的解决方案。
Option Compare Database
Sub LocalServerConn_Test()
Set conn = New adodb.Connection
Set rst = New adodb.Recordset
strDBName = "DataSet"
strConnectString = "Provider = SQLOLEDB.1; Integrated Security = SSPI; " & _
"Initial Catalog = " & strDBName & "; Persist Security Info = True; " & _
"Worksation ID = abc123;"
conn.ConnectionString = strConnectString
conn.Open
strSQL = "SELECT DISTINCT dbo.abc.abc123 FROM dbo.abc"
rst.Open Source:=strSQL, ActiveConnection:=strConnectString, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic
If rst.RecordCount = 0 Then
MsgBox "No records returned"
Else
rst.MoveFirst
Do While Not rst.EOF
MsgBox rst.Fields("abc123").Value
rst.MoveNext
Loop
End If
conn.Close
rst.Close
End Sub