I have a query based on a combobox that works fine, and a function that I 'borrowed' from a message board that exports a query to Excel. That also works fine except when I use it with a query that is based on a combobox.
I get an error that says "Error 3061 too Few Parameters. Expected 1."
Here is my query:
Code:
SELECT tblItem.Pallet, tblItem.PO, tblItem.Dept, tblItem.Class, tblItem.ItemNumber, tblItem.VesselVoyage, tblItem.Container, tblItem.Cartons, tblItem.UnitsPerCarton, tblItem.EA, tblItem.ItemDescription, tblItem.DamageDescriptionFROM tblItemWHERE (((tblItem.SalvagePickupName)=[Forms]![frmMain]![cboSalvagePickup]));
And here is my code:
Code:
Private Sub btnSalvageReport_Click() Call SalvageToExcel("qrySalvageReport", "Sheet1", "J:\MyFolder\qrySalvageReport.xlsx")End Sub
Which calls this function:
Code:
Code:
Public Function SalvageToExcel(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.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.Range("A1:L1").Select
With ApXL.Selection
.Font.Name = "Arial"
.Font.Size = 12
.Borders.LineStyle = xlContinuous
.Borders.ColorIndex = xlAutomatic
End With
With ApXL.Selection
.Interior.Color = RGB(153, 204, 51)
.HorizontalAlignment = xlCenter
.RowHeight = 30
.VerticalAlignment = xlCenter
End With
ApXL.ActiveSheet.Cells.Select
ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
ApXL.ActiveSheet.Cells.HorizontalAlignment = xlCenter
xlWSh.Range("A1").Select
rst.Close
Set rst = Nothing
Exit_SendSalvageToExcel:
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Resume Exit_SendSalvageToExcel
End Function
Any ideas?