title: MS Access - Query Definition Disappears After VBA Executes
body: <p>I have an Access 2010 database that I set up to keep track of time at work. I have a button on a form with the following VBA:</p>
  
  <pre><code>Private Sub cmdExport_Click()
  Dim myUser As String
  
  myUser = Environ("Username")
  
  DoCmd.OutputTo acOutputQuery, "ExportTime", "ExcelWorkbook(*.xlsx)", "C:\Users\" &amp; myUser &amp; "\Desktop\Time" &amp; Format(Me.txtStartDate, "yyyymmdd") &amp; "-" &amp; Format(Me.txtEndDate, "yyyymmdd") &amp; ".xlsx", False, "", , acExportQualityPrint
  
  End Sub
  </code></pre>
  
  <p>The button exports a saved query named ExportTime with the following definition:</p>
  
  <pre><code>SELECT MYTIME.MYDATE, MYTIME.ID, MYTIME.CM, MYTIME.MYDESCRIP, MYTIME.MYHOURS
  FROM CM RIGHT JOIN MYTIME ON CM.ID = MYTIME.CM
  WHERE (((MYTIME.MYDATE)&gt;=[Forms]![ExportTime]![txtStartDate] And (MYTIME.MYDATE)&lt;=[Forms]![ExportTime]![txtEndDate]))
  ORDER BY MYTIME.MYDATE, MYTIME.CM;
  </code></pre>
  
  <p>Pressing the button successfully exports the time to an Excel spreadsheet, exactly as I intend it to.</p>
  
  <p>The problem is that after running this operation, the query definition disappears.  The named query is still in the list of saved queries, but its definition is changed to just</p>
  
  <pre><code>SELECT;
  </code></pre>
  
  <p>This seems to happen regularly. I’ve copied the query definition to a text file, so I can paste it back in when I want to export my time again. I’m stumped as to why the definition keeps disappearing, though.</p>
  
  <p>Any thoughts or advice will be greatly appreciated.</p>