可以使用VBA / ADO连接两个记录集吗?

时间:2013-01-27 16:52:24

标签: ms-access ado

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

所以我一直在尝试创建两个记录集并将它们加入到VBA / ADO中。连接的左侧是具有Season和WeekNum的Access表,而连接的右侧是具有Season和Weeknum的SQL Server表以及其他数据。这工作正常,直到我尝试创建第三个记录集,这是连接的结果(在这个例子中,我没有尝试进行连接,只是通过从Access记录集中选择连接的第一部分)。当我设置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获取有关帐号列表的数据。意识到我可以在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

1 个答案:

答案 0 :(得分:3)

您报告此行会触发错误:

Set ObjRecordset3 = "SELECT * FROM " & Access_Recordset

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

我认为你应该寻找一种更简单的方法。在以下查询中,dbo_BigIntTable是指向SQL Server表的ODBC链接。它包含一个字段bigint_fld,其SQL Server数据类型为BigInt。但是,Access将该字段视为文本类型。因此,我可以使用相当于Long Integer字段(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 View创建连接并且它工作正常。