【问题标题】:Run-time error '3061'. Too few parameters. Expected 1 Access 2013运行时错误“3061”。参数太少。预计 1 访问 2013
【发布时间】:2017-09-27 20:07:24
【问题描述】:

我使用这行代码:

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 Too Few Parameters - Expected 1. 当我单步执行时,正是这行代码导致了错误:

Set rst = CurrentDb.OpenRecordset(strTQName)

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

我错过了什么?

谢谢。

【问题讨论】:

  • 您可以在没有代码的情况下运行该查询吗?然后会发生什么?

标签: ms-access vba


【解决方案1】:

您的查询中很可能引用了一个表单控件

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

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

【讨论】:

  • 你是对的 - 我确实在查询中引用了表单控件!
猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多