0
votes

I use this line of code:

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

To call and pass parameters to this Function:

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

However, when I run it I keep receiving error 3061 Too Few Parameters - Expected 1. When I step through, it is this line of code causing the error:

Set rst = CurrentDb.OpenRecordset(strTQName)

However if I hover over the above line in debug, it is showing the name of the query (qryCustExportStyColOnlyDrop) I am passing.

What am I missing?

Thanks.

1
Can you run that query without the code? What happens then?braX

1 Answers

0
votes

Most likely you have in your query a reference to a form control.

The value from this you must pass as a parameter if you run the query from code.

So, create a QueryDef object in your function, pass the parameter, and open your recordset from the QueryDef object.