4

目标是将 Access 表与 SQL Server 表中的匹配数据连接起来。我会在 Access 中使用链接表来执行此操作,但我遇到了 Access 的 BigInt 问题(我会创建一个视图以将 BigInt 转换为 Int,但现在这不是一个选项)。

所以我一直在尝试创建两个记录集并将它们加入 VBA/ADO。连接的左侧是带有 Season 和 WeekNum 的 Access 表,而连接的右侧是带有 Season 和 Weeknum 以及其他数据的 SQL Server 表。这工作正常,直到我尝试创建作为连接结果的第三个记录集(在此示例中,我没有尝试进行连接,只是通过从 Access 记录集中选择连接的第一部分)。当我执行 Set ObjRecordset3 = "SELECT * FROM " & Access_Recordset ' 时,出现类型不匹配错误。

甚至可以加入两个记录集吗?如果是这样,这是如何完成的?

Function Join()

    Dim SQL_Server_Connection As ADODB.Connection
    Set SQL_Server_Connection = New ADODB.Connection
    Dim SQL_Server_Query As String
    Dim SQL_Server_Recordset As New ADODB.Recordset

    Dim Access_Recordset As New ADODB.Recordset

    Dim ObjConnection As ADODB.Connection
    Set ObjConnection = CreateObject("ADODB.Connection")
    Dim ObjRecordset3 As New ADODB.Recordset

    ' Get data from Bump Table 3:
    Access_Recordset.Open "SELECT * FROM [Bump Table 3]", CurrentProject.Connection

    ' Open connection to SQL Server:
    SQL_Server_Connection_String = "DSN=MySQLServer"
    SQL_Server_Connection.Open SQL_Server_Connection_String

    ' Define the SQL Server query:
    SQL_Server_Query = "SELECT Season, WeekNum FROM TE"

    ' Populate the SQL_Server_Recordset:
    SQL_Server_Recordset.Open SQL_Server_Query, SQL_Server_Connection, adOpenDynamic, adLockOptimistic

    'Join Access_Recordset (Table: Bump Table 3) to SQL_Server_Recordset (Table: TE)
    Set ObjRecordset3 = "SELECT * FROM " & Access_Recordset ' Type Mismatch error on this line

    Access_Recordset.Close
    Set Access_Recordset = Nothing

    SQL_Server_Recordset.Close
    Set SQL_Server_Recordset = Nothing

    SQL_Server_Connection.Close

End Function

* 更新 *

我想出了如何实现我的最终目标,即根据两个表共有的帐号字段从 SQL Server 获取有关 Access 表中帐号列表的数据。意识到我可以在 SQL Server 上创建一个持久临时表,我结合使用 DAO 和 ADO 从 Access 表中获取值并创建一个临时表。我所要做的就是运行引用临时表的传递查询。唯一奇怪的事情(此时这不是问题)是如果我创建临时表并在 VBA 中运行传递查询,则此设置有效。但是如果我在 VBA 中创建临时表并双击传递查询,Access 会说找不到临时表。无论如何,这是代码:

Public Sub Insert_Into_Access_From_ADO_Recordset_Using_PTQ_Simpler()

    Dim dbs As DAO.Database
    Set dbs = CurrentDb()

    Dim cnn As ADODB.Connection
    Set cnn = New ADODB.Connection

    Dim rst As ADODB.Recordset

    'Open SQL Server
    Dim str_cnn As String
    str_cnn = "MYDSN"

    cnn.Open str_cnn

    ' Drop the temp table:

        Dim str_SQL_Drop_Temp_Table As String

        str_SQL_Drop_Temp_Table = "IF OBJECT_ID('tempdb..##BumpData','U') IS NOT NULL "
        str_SQL_Drop_Temp_Table = str_SQL_Drop_Temp_Table & " DROP TABLE ##BumpData "

        cnn.Execute str_SQL_Drop_Temp_Table


    ' Create the temp table:

        Dim str_SQL_Create_Temp_Table As String

        str_SQL_Create_Temp_Table = " CREATE TABLE ##BumpData "
        str_SQL_Create_Temp_Table = str_SQL_Create_Temp_Table & " " & "("
        str_SQL_Create_Temp_Table = str_SQL_Create_Temp_Table & " " & " ID INT "
        str_SQL_Create_Temp_Table = str_SQL_Create_Temp_Table & " " & " ,   AccountNumber VARCHAR(Max)"
        str_SQL_Create_Temp_Table = str_SQL_Create_Temp_Table & " " & ")"

        cnn.Execute str_SQL_Create_Temp_Table


    ' Insert values from the Access table into the temp table
    ' by looping through the Access table as a recordset:

        Dim rst_DAO As DAO.Recordset
        Set rst_DAO = dbs.OpenRecordset("Bump Data")

        Dim str_SQL_Insert As String

        rst_DAO.MoveFirst

        With rst_DAO
            Do While Not rst_DAO.EOF
                'str_Loan_Number_List = str_Loan_Number_List & "'" & Trim(rst![Loan Number]) & "'" & ","
                str_SQL_Insert = " INSERT INTO ##BumpData VALUES (" & rst_DAO![ID] & ",'" & Trim(rst_DAO![Loan Number]) & "') "
                cnn.Execute str_SQL_Insert
                .MoveNext
            Loop
        End With

    ' Run the pass-thru query which joins to the temp table:
    DoCmd.SetWarnings False
    DoCmd.RunSQL "SELECT * INTO [Bump Results] FROM [Bump PTQ]"
    DoCmd.SetWarnings True

End Sub
4

1 回答 1

3

您报告此行触发错误:

Set ObjRecordset3 = "SELECT * FROM " & Access_Recordset

符号的右侧=尝试将字符串与ADODB.Recordset对象连接起来。这可能是编译错误的直接原因。但是,这不是该行的唯一问题。Set <recordset object> = "any string value"不管用。最后,Access SQL 不支持FROM <recordset object>任何类型的记录集对象(ADO 或 DAO)。

我认为您应该寻找一种更简单的方法。在以下查询中,dbo_BigIntTable是指向 SQL Server 表的 ODBC 链接。它包括一个字段 ,bigint_fld其 SQL Server 数据类型为 BigInt。但是,Access 将该字段视为文本类型。因此,我可以将它与长整数字段 ( tblFoo.id) 的字符串等价物连接起来。

SELECT tblFoo.id, dbo_BigIntTable.bigint_fld
FROM
    tblFoo
    INNER JOIN dbo_BigIntTable
    ON CStr(tblFoo.id) = dbo_BigIntTable.bigint_fld;

Access 查询设计器抱怨它无法在设计视图中显示该连接,但我能够从 SQL 视图创建连接并且工作正常。

于 2013-01-27T20:12:15.960 回答