businesshippieRH
Registered User.
- Local time
- Yesterday, 21:06
- Joined
- Aug 8, 2014
- Messages
- 60
I am trying to export a query to excel. I have used this code minus the SQL portion (those queries did not require input from a form) for other queries to do the exact same thing. I have multiple queries that will require input from a form, and believe that if I can solve this first one, the rest should be pretty formulaic.
However, when I run this one, I get a "Run-time error 3061: too few parameters. Expected 3."
I have tried the code split into two sections run by separate buttons, where one creates the query and the other exports to excel. I have also tried running a query built using SQL (no VBA) and exporting it using the method that works for my other exports. The query itself populates using either method. It just will not export (crashes on red line).
My code is as follows:
Thanks in advance for your help!!!
However, when I run this one, I get a "Run-time error 3061: too few parameters. Expected 3."
I have tried the code split into two sections run by separate buttons, where one creates the query and the other exports to excel. I have also tried running a query built using SQL (no VBA) and exporting it using the method that works for my other exports. The query itself populates using either method. It just will not export (crashes on red line).
My code is as follows:
Code:
Private Sub btn_EXPORTCCQry_Click()
'Open the query
'Export to Excel
'Step 1: Declare your variables
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim strSQL As String
Dim i As Integer
'SQL from original "qry_REPORTINGChargeCodeDate" This query will be deleted if I can get this code to work.
strSQL = "PARAMETERS [Forms].[frm_Reporting].[txt_CCStartDate] DateTime, [Forms].[frm_Reporting].[txt_CCEndDate] DateTime;" & _
" SELECT tbl_CompletedScans.ChargeCode, tbl_CompletedScans.CheckOutDate, tbl_EmployeeData.EmpName, tbl_CompletedScans.TrinityID, tbl_CompletedScans.EquipmentType, tbl_CompletedScans.Manufacturer, tbl_CompletedScans.ModelNo, tbl_CompletedScans.SerialNo" & _
" FROM tbl_EmployeeData INNER JOIN tbl_CompletedScans ON tbl_EmployeeData.EmpID = tbl_CompletedScans.CheckOutEmployee" & _
" WHERE (((tbl_CompletedScans.ChargeCode)=Forms!frm_Reporting!txt_CCChargeCode) And ((tbl_CompletedScans.CheckOutDate) Between (Forms!frm_Reporting!txt_CCStartDate) And (Forms!frm_Reporting!txt_CCEndDate)));"
'Step 2: Identify the database and query
Set MyDatabase = CurrentDb
On Error Resume Next
With MyDatabase
.QueryDefs.Delete ("qry_ExportChargeCodeDate")
Set MyQueryDef = .CreateQueryDef("qry_ExportChargeCodeDate", strSQL)
.Close
End With
'Step 3: Open the query
[COLOR="Red"]Set MyRecordset = MyQueryDef.OpenRecordset[/COLOR]
'Step 4: Clear previous contents
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.Workbooks.Add
.Sheets("Sheet1").SELECT
'Step 5: Copy the recordset to Excel
.ActiveSheet.Range("A1").CopyFromRecordset MyRecordset
'Step 6: Add column heading names to the spreadsheet
For i = 1 To MyRecordset.Fields.Count
xlApp.ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
Next i
xlApp.Cells.EntireColumn.AutoFit
End With
'Open message box
MsgBox "Please use MS Excel to modify data further.", vbInformation, "Data Exported"
Thanks in advance for your help!!!
Last edited: