title: Access VBA: Recordset should be NOT nothing
body: <p>I have a Front-End database setup for users to extract data regarding a list of information that they upload.  The export function worked fine except they want the results to go to the open workbook add a sheet with the data without saving.  The problem is that the created query has data when I run the query before or after the macro is not running.  However as the macro is running the query returns nothing. The latest VBA I'm using is below.  Please review and advise what I'm missing.
  Thank you,</p>
  
  <p>MS Office - Access: 2010</p>
  
  <p>Active Reference Library:</p>
  
  <ul>
  <li>Visual Basic for applications</li>
  <li>Microsoft Access 14.0 Object Library   </li>
  <li>OLE Automation</li>
  <li>Microsoft Excel 14.0 Object Library</li>
  <li>Microsoft Office</li>
  <li>14.0 Access database engine Object Library</li>
  </ul>
  
  <hr>
  
  <p>Macro:</p>
  
  <pre><code>Private Sub ExpFile_Click()
  Dim sql2export, s As String, blnExcel, blnWhere As Boolean, qdf As QueryDef, xlApp As Object, ws As Excel.Worksheet
  Dim MyDatabase As DAO.Database, MyQueryDef As DAO.QueryDef, MyRecordset As DAO.Recordset
  blnWhere = False
  
  If Me. QueryASubform.Visible = True Then 'exceptions
      sql2export = "QueryA"
      blnWhere = True
  ElseIf Me. QueryBSubform.Visible.Visible = True Then 'no Program Group for Build ID
      sql2export = " QueryB"
  ElseIf Me. QueryCSubform.Visible = True Then 'Bill to and Type report.
      sql2export = " QueryC"
  Else: Exit Sub
  End If
  
  If blnWhere = False Then
      s = "select * from " &amp; sql2export &amp; " Where (((" &amp; sql2export &amp; ". GPID)=[Forms]![frmFEFindQA]![GPID]));"
  Else: s = "select * from " &amp; sql2export
  End If
  
  On Error Resume Next
  CurrentDb.QueryDefs.Delete "xlsExport"
  Set qdf = CurrentDb.CreateQueryDef("xlsExport", s)
  
  Set xlApp = GetObject(, "excel.application")
  If (Err.Number = 0) Then
      Set xlApp = GetObject("Excel.Application")
      xlApp.Visible = True
      Set ws = xlApp.Sheets.Add
      Set MyDatabase = CurrentDb
     MyDatabase.QueryDefs.Delete ("xlsExport")
     Set MyQueryDef = MyDatabase.CreateQueryDef("xlsExport", s)
     Set MyRecordset = MyDatabase.OpenRecordset("xlsExport")    ‘&lt;------ empty
  
      With xlApp
          .ws.Select
          .ActiveSheet.Range("a2").CopyFromRecordset MyRecordset
          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
  Else:
      Set xlApp = CreateObject("Excel.Application")
      xlApp.Visible = True
      DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "xlsExport", "C:\Users\" &amp; Environ("USERNAME") &amp; "\Documents\VehInfoExp", True
      xlApp.Workbooks.Open "C:\Users\" &amp; Environ("USERNAME") &amp; "\Documents\InfoExp.xls", True, False
  End If
  Err.Clear
  On Error GoTo 0
  Set xlApp = Nothing
  End Sub
  </code></pre>