0

我创建了下面的代码来测试我是否可以运行查询并从 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
4

1 回答 1

0

您应该能够使用与此非常相似的代码:

Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.Execute _
        "DELETE FROM LocalTable", _
        dbFailOnError
cdb.Execute _
        "INSERT INTO LocalTable (abc123) " & _
        "SELECT DISTINCT abc123 " & _
        "FROM " & _
            "[" & _
                "ODBC;" & _
                "Driver={SQL Server};" & _
                "Server=.\SQLEXPRESS;" & _
                "Database=DataSet;" & _
                "Trusted_Connection=yes;" & _
            "].[dbo.abc]", _
        dbFailOnError
Set cdb = Nothing

您只需将组合框绑定到 [LocalTable],SQL Server 表中的更新值就会出现。

于 2013-06-17T14:26:17.593 回答