title: Access VBA Multiple Sorting Criteria
body: <p><strong>The Case:</strong> I have a form that has several fields of entry. A button contained within this form runs a query and generates a report based on the form entry fields. </p>
  
  <p>I have created 3 comboboxes that will allow the user to sort the report on various criteria (ie. sort by Analyst name, then by Meeting Date, then by Ticker).</p>
  
  <pre><code>Combo Box 1 = Sort_By;
  Combo Box 2 = Sort_By_2;
  Combo Box 3 = Sort_By_3
  </code></pre>
  
  <p><strong>The Code:</strong></p>
  
  <pre><code>Private Sub Run_Query_Button_Click()
  If Revisit_Check.Value = False Then
  DoCmd.OpenQuery "Important Information Extracted"
  DoCmd.Close
  DoCmd.OpenReport "Important Information Extracted", acViewReport
  DoCmd.SetOrderBy Sort_By Sort_By_2 Sort_By_3
  Else
  DoCmd.OpenQuery "Revisit"
  DoCmd.Close
  DoCmd.OpenReport "Revisit_Report", acViewReport
  DoCmd.SetOrderBy Sort_By Sort_By_2 Sort_By_3
  End If
  End Sub
  </code></pre>
  
  <p>This code returns a syntax error. It does not sort on the three "sort by" criteria. If I use only on of the criteria in the following fashion:</p>
  
  <pre><code>Private Sub Run_Query_Button_Click()
  If Revisit_Check.Value = False Then
  DoCmd.OpenQuery "Important Information Extracted"
  DoCmd.Close
  DoCmd.OpenReport "Important Information Extracted", acViewReport
  DoCmd.SetOrderBy Sort_By
  </code></pre>
  
  <p>The code runs properly and sorts on the given "sort by" value. If instead of using the form field comboboxes to sort, I use the actual field names, for instance:</p>
  
  <pre><code>Private Sub Run_Query_Button_Click()
  If Revisit_Check.Value = False Then
  DoCmd.OpenQuery "Important Information Extracted"
  DoCmd.Close
  DoCmd.OpenReport "Important Information Extracted", acViewReport
  DoCmd.SetOrderBy "Analyst, Meeting Date, Ticker"
     ...
  </code></pre>
  
  <p>Everything works fine as well. Why, when I use the three sort criteria, do I get a syntax error? How can I sort on these three criteria?</p>