Excel VBA ADO查询到microsoft访问返回的记录超出预期

时间:2014-06-19 20:09:55

标签: sql excel vba excel-vba ado

我的excel / VBA ADO查询返回的记录超出了我的预期。

当我在ms访问中手动运行查询时,它显示59条正确的记录。当我使用某个VBA从excel运行导入时,它会导入234条记录。我可能做错了什么?

这是我使用的代码:

Function DataPath() As String

    DataPath = ThisWorkbook.Path & "\" & "SpaceOpt.accdb"

End Function

Function GetAccessConnection() As ADODB.Connection

    Set GetAccessConnection = New ADODB.Connection
    GetAccessConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DataPath & ";"

End Function

Sub GetDataFromAccess()

    Dim adoConn As ADODB.Connection, adoComm As ADODB.Command, adoRS As ADODB.Recordset

    Set adoConn = GetAccessConnection
    Set adoComm = New ADODB.Command

    With adoComm
        Set .ActiveConnection = adoConn
        .CommandText = "SELECT * FROM [Queryname]"
    End With

    Set adoRS = adoComm.Execute

    sheet2.range("A1").CopyFromRecordset adoRS

    adoConn.Close

End Sub

0 个答案:

没有答案