I am trying to export data from Access to an Excel file without saving the file. I have been unable to get my macro to work. Right now when the macro runs it only opens a new Excel workbook but does copy the data into the workbook.
I believe the issue lies in the generated strSQL. I suspect that it has to do with the Status field value equal to IVR Call Needed. Maybe the spaces in the value?
Here is my code.
Code:Private Sub Export() 'Step 1: Declare your variables Dim MyDatabase As DAO.Database Dim MyQueryDef As DAO.QueryDef Dim MyRecordset As DAO.Recordset Dim strSQL As String Dim i As Integer strSQL = "SELECT Letters.[Sponsor SSN], Letters.[Beneficiary name], Letters.[Phone Number] FROM Letters WHERE Letters.[Status]= ""IVR Call Needed"";" 'Step 2: Identify the database and query Set MyDatabase = CurrentDb On Error Resume Next With MyDatabase .QueryDefs.Delete ("tmpOutQry") Set MyQueryDef = .CreateQueryDef("tmpOutQry", strSQL) .Close End With 'Step 3: Open the query Set MyRecordset = MyQueryDef.OpenRecordset 'Step 4: Clear previous contents Dim xlApp As Object Set xlApp = CreateObject("Excel.Application") With xlApp .Visible = True .Workbooks.Add .Sheets("Sheet1").Select 'Step 5: Copy the recordset to Excel .ActiveSheet.Range("A2").CopyFromRecordset MyRecordset 'Step 6: Add column heading names to the spreadsheet 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 End Sub