I have a sub that exports tables from my Query in Access to Excel. In Access, I have a form that runs the Query and allows user to update the Query as needed. If I run the sub with set parameters (not using the form), for instance, "Austria" the sub works fine. But when I use the form, and write in "Austria" I get the area message:
3601 Too few Parameters. Expected 1.
The issue appears to be with the HAVING ((([Game Categories].Lottery)=[Forms]![TransferToExcel]![listLotteryName]) (If replaced with Austria, the sub works fine)
Sub Mysub()
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Dim qdfQUERY2014sales As QueryDef
Dim rsQUERY2014sales As Recordset
Set qdfQUERY2014sales = CurrentDb.QueryDefs("QUERY2014sales")
Set rsQUERY2014sales = qdfQUERY2014sales.OpenRecordset()
Set objexcel = CreateObject("excel.Application")
objexcel.Visible = True
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\Users\MORTBANKER\Documents\test.xlsm")
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
Set wbexcel = objexcel.Workbooks.Add()
End If
CopyToWorkbook wbexcel, rsQUERY2014sales
'need to save the workbook, make it visible or something.
End Sub
Private Sub CopyToWorkbook(objWorkbook As Excel.Workbook, rsQRY As Recordset)
Dim newWorksheet As Excel.Worksheet
Set newWorksheet = objWorkbook.Worksheets.Add()
With newWorksheet
.Range("A1").CopyFromRecordset rsQRY '<-magic happens here!
End With
'Copy stuff to the worksheet here'
End Sub
Anyone have any thoughts how I can fix this?