Results 1 to 4 of 4
  1. #1
    Kluaoha is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2017
    Posts
    20

    Can't Write to Excel File When Recordset is Open

    I am working towards exporting Access data into Excel by means of a Recordset using VBA. For a while I was struggling to figure out why nothing was being written, until I started commenting out sections and ultimately got down to the situation here:


    Code:
    Private Sub ExportButton_Click()
        Dim XLapp As Excel.Application
        Dim XLwbk As Workbook
        Dim XLsht As Worksheet
        Dim rs As Recordset
        
        On Error GoTo ExportError
        Set XLapp = New Excel.Application
        Set XLwbk = XLapp.Workbooks.Open(CurrentProject.Path & "\MyExcelFile.xlsx")
        Set XLsht = XLwbk.Worksheets(1)
        Set rs = CurrentDb.OpenRecordset("MyQuery", dbOpenSnapshot) 'THIS LINE
        With XLsht
            .range("A5").value = 2
        End With
    
        If MsgBox("Export finished successfully. Open file?", vbDefaultButton2 + vbQuestion + vbYesNo) = vbYes Then
            XLapp.Visible = True
        End If    
    
    ExportError:
        Set XLapp = Nothing: Set XLwbk = Nothing: Set XLsht = Nothing: Set rs = Nothing
    End Sub
    Nothing is written to the Excel file with the above code, but commenting out THIS LINE makes it work. So it seems like the recordset is messing something up somehow. Any suggestions?
    Thanks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,643
    The way you handle errors will make it fail silently. You might try more traditional error handling:

    http://www.baldyweb.com/ErrorTrap.htm

    or temporarily comment out the "On Error..." line and run the code. The recordset is failing for some reason.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Kluaoha is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2017
    Posts
    20
    Thanks, I was getting error 13 (Type mismatch) for not specifying it as DAO.Recordset.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,643
    No problem, happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 6
    Last Post: 09-14-2017, 07:31 AM
  2. Replies: 11
    Last Post: 06-16-2014, 08:52 AM
  3. Replies: 2
    Last Post: 09-04-2013, 11:01 PM
  4. Replies: 24
    Last Post: 07-08-2013, 12:59 PM
  5. Excel sheet open in recordset
    By waqas in forum Programming
    Replies: 3
    Last Post: 09-22-2011, 11:47 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