2017-09-27 215 views
0

我使用这行代码:运行时错误“3061”。参数太少。预计1访问2013

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)的名称。

我错过了什么?

谢谢。

+0

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

回答

0

您最有可能在查询中引用了表单控件

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

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

+0

你是对的 - 我在查询中引用了表单控件! – Michael

+0

您的建议是修复 - 谢谢。 – Michael