I want to export the results of my query to an excel file by clicking on a button in a form.
For this I used this code and it works well:
Private Sub Command9_Click()
On Error GoTo ProcError
DoCmd.OutputTo _
ObjectType:=acOutputQuery, _
ObjectName:="Contract Type Billing", _
OutputFormat:=acFormatXLSX, _
Autostart:=True
ExitProc:
Exit Sub
ProcError:
Select Case Err.Number
Case 2501 'User clicked on Cancel
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
"Error in cmdExportQuery_Click event procedure..."
End Select
Resume ExitProc
End Sub
But my query uses 2 parameters sdate and edate, I don't want access to ask me for these value but I want the user to enter them in the form with the appropriate textboxes.
So I added this bit to the code before DoCMD.OutputTo
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("Contract Type Billing")
qdf.Parameters("sdate") = sdate.Value
qdf.Parameters("edate") = edate.Value
But unfortunately it doesn't work. How can put the parameters into my query before I export it ?