title: Exporting a parameterized query to excel using vba
body: <p>I want to export the results of my query to an excel file by clicking on a button in a form.</p>
  
  <p>For this I used this code and it works well:</p>
  
  <pre><code>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 " &amp; Err.Number &amp; ": " &amp; Err.Description, vbCritical, _
  "Error in cmdExportQuery_Click event procedure..."
  End Select
  Resume ExitProc
  
  End Sub
  </code></pre>
  
  <p>But my query uses 2 parameters <strong>sdate</strong> and <strong>edate</strong>, 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.</p>
  
  <p>So I added this bit to the code before DoCMD.OutputTo</p>
  
  <pre><code>Dim qdf As DAO.QueryDef
  
  Set qdf = CurrentDb.QueryDefs("Contract Type Billing")
  
  qdf.Parameters("sdate") = sdate.Value
  qdf.Parameters("edate") = edate.Value
  </code></pre>
  
  <p>But unfortunately it doesn't work. How can put the parameters into my query before I export it ?</p>