运行时错误' 3061'。参数太少。预计1 Access 2013

时间:2017-09-27 11:57:40

标签: vba ms-access access-vba

我使用这行代码:

Call SendTQ2XLWbSheetData("qryCustExportStyColOnlyDrop", "Data", "C:\Users\" & GetLogonName() & "\FWD Order Customer Export.xlsm")

调用并将参数传递给此函数:

Public Function SendTQ2XLWbSheetData(strTQName As String, strSheetName As String, strFilePath As String)
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
' strFilePath is the name and path of the file you want to send this data into.

    Dim rst As DAO.Recordset
    Dim ApXL As Object
    Dim xlWBk As Object
    Dim xlWSh As Object
    Dim fld As DAO.Field
    Dim strPath As String
    Const xlCenter As Long = -4108
    Const xlBottom As Long = -4107
    On Error GoTo err_handler

    strPath = strFilePath

    Set rst = CurrentDb.OpenRecordset(strTQName)

    Set ApXL = CreateObject("Excel.Application")

    Set xlWBk = ApXL.Workbooks.Open(strPath)

    ApXL.Visible = True

    Set xlWSh = xlWBk.Worksheets(strSheetName)

    xlWSh.Visible = True

    xlWSh.Activate

    'clear any current size ranges
    ApXL.Range("DataRange").Select
    ApXL.Selection.ClearContents

    xlWSh.Range("A1").Select

    For Each fld In rst.Fields
        ApXL.ActiveCell = fld.Name
        ApXL.ActiveCell.Offset(0, 1).Select
    Next

    rst.MoveFirst

    xlWSh.Range("A2").CopyFromRecordset rst

    xlWSh.Visible = False


    rst.Close

    Set rst = Nothing


    xlWBk.Close True

    Set xlWBk = Nothing

    ApXL.Quit

    Set ApXL = Nothing

Exit_SendTQ2XLWbSheet:
    Exit Function

err_handler:
    DoCmd.SetWarnings True
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_SendTQ2XLWbSheet
End Function

然而,当我运行它时,我一直收到错误3061太少参数 - 预期1.当我单步执行时,正是这行代码导致错误:

Set rst = CurrentDb.OpenRecordset(strTQName)

但是,如果我将鼠标悬停在调试的上一行上,它会显示我正在传递的查询名称(qryCustExportStyColOnlyDrop)。

我错过了什么?

感谢。

1 个答案:

答案 0 :(得分:0)

您的查询中很可能是对表单控件的引用。

如果从代码运行查询,则必须将此值作为参数传递。

因此,在函数中创建一个 QueryDef 对象,传递参数,然后从QueryDef对象中打开记录集。

相关问题