Results 1 to 4 of 4
  1. #1
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92

    Exporting with VBA problem

    Hello,


    I am using the following code to export 2 queries into a single Excel sheet. The code is working as intended until I rename the database. I had requested some help with testing and gave the database a new name so that it would not replace the master copy. At that point the code starting throwing errors stating that it could not find the Excel workbook. As soon as I changed the database name back it started working again.

    Can anyone explain to me why a name change (to the db not the Excel document) would affect this or how to get around it? Also, what part of the following code would even be dependent on recognizing the database name?

    Thanks


    Private Sub AccAdj_Click()
    Dim rstName As Recordset
    Dim objApp As Object, objMyWorkbook As Object, objMySheet As Object, objMyRange As Object
    DoCmd.SetWarnings False
    DoCmd.OpenQuery "MedAdj_List_qry"
    DoCmd.OpenQuery "DenAdj_List_qry"
    DoCmd.OpenQuery "VisAdj_List_qry"
    DoCmd.OpenQuery "OtherAdj_List_qry"
    DoCmd.SetWarnings True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "AcACt_qry", "Accrual_AdjustmentList.xlsx", True, "Accrual_Adjustments"
    Set rstName = CurrentDb.OpenRecordset("AcSB_qry")

    Set objApp = CreateObject("Excel.Application")
    Set objMyWorkbook = objApp.Workbooks.Open("Accrual_AdjustmentList.xlsx ")
    Set objMySheet = objMyWorkbook.Worksheets("Accrual_Adjustments")
    Set objMyRange = objMySheet.Cells(objApp.ActiveSheet.UsedRange.Rows .Count + 2, 1)
    With objMyRange
    rstName.MoveFirst 'Rewind to the first record
    .Clear
    .CopyFromRecordset rstName
    End With
    objMyWorkbook.Save
    objApp.Quit

    Set ojbMyWorkbook = Nothing
    Set objApp = Nothing
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Changing the db name should have NO effect on the export code. 1 does not effect the other.
    tho looking at yr code, there is no reason to start an excel object....the transferspreadsheet command is all you need to export data.
    The record set is not needed.

  3. #3
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92
    Thanks for the reply. I didn't think the name change should have had an affect either but that is all that changed fromt the time it was working to the time the errors were thrown.

    As for the additional code, I was under the impression that it was specific to exporting the data from the second query to the same worksheet and that with out it the second export would be applied to a new worksheet. Is that not true? I am still learning Access, SQL and VBA so I tend to take stuff at face value when someone is trying to help me out.

    Thanks

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, vQry, vFile, True, vSheet

    The last param is the sheet name. So 3 things:
    1. it will export to the same sheet everytime and the existing data gets written over BUT if the # records is smaller than the existing data there, it WONT erase the extra records.
    2. to prevent the extra old records, you can delete the existing workbook before the export
    3. if you cant erase the existing workbook, you can use the excel object to delete the existing workSHEET, save, THEN export.

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

Similar Threads

  1. Exporting a Table to MS Excel problem
    By nosec in forum Import/Export Data
    Replies: 9
    Last Post: 11-05-2013, 11:28 AM
  2. Replies: 1
    Last Post: 10-19-2011, 07:51 PM
  3. Problem exporting to XML
    By accexp in forum Import/Export Data
    Replies: 2
    Last Post: 09-15-2010, 12:33 PM
  4. Exporting to Excel Problem
    By octu in forum Import/Export Data
    Replies: 1
    Last Post: 04-14-2010, 11:28 PM
  5. Exporting to txt problem
    By timpepu in forum Import/Export Data
    Replies: 0
    Last Post: 03-25-2010, 12:58 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