无法将ADODB.Recordset作为参数传递给函数调用

时间:2016-08-01 07:14:10

标签: excel vba macros access byref

以下是我的记录集声明

    Dim expectedRs, actualRs As ADODB.Recordset

    Set expectedRs = accessDatabse.getResultSetForSqlQuery(tempArr(1))
    Set actualRs = accessDatabse.getResultSetForSqlQuery(tempArr(2))

以下工作正常,

    excelFunc.writeQueryResultsToExcel( tempArr(0),   accessDatabse.getResultSetForSqlQuery(tempArr(1)),    accessDatabse.getResultSetForSqlQuery(tempArr(2)))

但是,我无法在方法调用中传递变量

    excelFunc.writeQueryResultsToExcel CStr(tempArr(0)), expectedRs,actualRs 

我收到编译时错误" ByRef Argument Type Mismatch"

以下是我的函数被调用

    Public Function writeQueryResultsToExcel(workbookName As String, expectedRs As Object, actualRs As Object)

        Dim wkb As Workbook
        Dim strPath As String

        strPath = globalObj.getDefaultRunInstancePath()

        Set wkb = Workbooks.Open(strPath + workbookName + ".xlsx")

        'rs.Open strSQL
        'Sheet4.Range("A1").CopyFromRecordset rs
    '***********************************UntestedCode******************************
        wkb.Sheets("Expected").Range("A1").CopyFromRecordset expectedRs
        wkb.Sheets("Actual").Range("A1").CopyFromRecordset actualRs
        wkb.Save
        wkb.Close
    '***********************************UntestedCode******************************

    End Function

1 个答案:

答案 0 :(得分:0)

Dim expectedRs, actualRs As ADODB.Recordset

实际上意味着:

Dim expectedRs As Variant, actualRs As ADODB.Recordset

您的函数预计expectedRs ObjectactualRs也是Object。因此,请做以下更改:

Dim expectedRs, actualRs As ADODB.Recordset

更改为:

Dim expectedRs As ADODB.Recordset, actualRs As ADODB.Recordset

Public Function writeQueryResultsToExcel(workbookName As String, expectedRs As Object, actualRs As Object)

更改为:

Public Function writeQueryResultsToExcel(workbookName As String, expectedRs As ADODB.Recordset, actualRs As ADODB.Recordset)