Results 1 to 11 of 11
  1. #1
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2008
    Posts
    58

    VBA to export query to Excel

    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


  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    What is "IVR Call Needed" and why the double quotes?

    Have you tried debug.print for your SQL string?

    DEbug.Print strSQL

    then view it in the immediate window.

  3. #3
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2008
    Posts
    58
    IVR Call Needed is the value found in the field Status field.

    I added double quotes because when I added single quotes I get a Compile Error: Expected end of statement.

  4. #4
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2008
    Posts
    58
    The immediate window shows the following
    SELECT Letters.[Sponsor SSN], Letters.[Beneficiary name], Letters.[Phone Number] FROM Letters WHERE Letters.[Status]= "IVR Call Needed";

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If you are looking for the literal text "IVR Call Needed" try..

    WHERE Letters.[Status]= 'IVR Call Needed';"

    Your previous code was probably looking for quatations in the field "Status"

  6. #6
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2008
    Posts
    58
    I tried WHERE Letters.[Status]= 'IVR Call Needed';" and the Excel sheet is empty. I do see that a Query was created and stored with the data, but the data was not copied to the Excel sheet.

  7. #7
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2008
    Posts
    58
    It looks like Step 3...opening the query is not occuring.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I am not familiar with opening a recordset this way.
    Set MyRecordset = MyQueryDef.OpenRecordset

    Maybe
    Set MyRecordset = MyDatabase.OpenRecordset("MyQueryDef", dbOpenSnapshot)

    I am going to do a little research to try and understand the approach you have here. It could also be something with how you are writing to the cells....

  9. #9
    Monterey_Manzer is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2012
    Posts
    164
    I'm not sure why you need all the QueryDefs code. I manage to get by with just a DAO.Recordset. I would define the active workbook and worksheet and just copy from the recordset. Something like this:
    Code:
    Dim xl As Excel.Application, xlWb as Excel.Workbook, xlWs as Excel.Worksheet
    Dim strSQL as String, rst as DAO.Recordset
    
    Set xl = CreateObject("Excel.Application")
    Set xlWb = xl.ActiveWorkbook
    Set xlWs = xlActiveWorksheet
    
    strSQL = "SELECT Letters.[Sponsor SSN], Letters.[Beneficiary name], Letters.[Phone Number] FROM Letters WHERE Letters.[Status]= 'IVR Call Needed';"
    
    Set rst = CurrentDb.OpenRecordset(strSQL)
    
    xlWs.Range("A2").CopyFromRecordset rst
    Give that a shot and see what you get.

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I was able to get this to work


    Code:
    'Step 1: Declare your variables
        Dim MyDatabase As DAO.Database
        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
        Set MyRecordset = MyDatabase.OpenRecordset(strSQL, dbOpenSnapshot)
    '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
    The way you were opening the recordset seemed to be the issue. No need to create a qryDef either

    edit: I see I was a little late with a solution. But the same idea... Seems like you started with some code you copied from somewhere. Why don't you tell us what you are trying to acompllish. Maybe there is yet another way.

  11. #11
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2008
    Posts
    58
    ItsMe that solution worked perfectly. Thank you everyone fot their assistance with this.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Export Query into Excel??
    By drjr in forum Import/Export Data
    Replies: 1
    Last Post: 07-10-2013, 05:45 PM
  2. Replies: 7
    Last Post: 04-25-2013, 03:47 PM
  3. Export query to excel
    By Liam87 in forum Queries
    Replies: 1
    Last Post: 12-05-2012, 12:00 AM
  4. Export Query into Excel
    By system243trd in forum Programming
    Replies: 1
    Last Post: 11-24-2012, 08:51 AM
  5. Replies: 3
    Last Post: 10-07-2011, 07:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums