Results 1 to 12 of 12
  1. #1
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18

    Can't Save Target WB

    Hello,
    I’m new to vba and the below code works fine to export datato the target workbook. But now tryingto save the target file to the current path plus add “\template” path. I have tried a lot of things but can’t get itto work. Can someone please help me withthe code to do this? It would bemuch-appreciated.

    Function ExportAppeals28()



    Dim dbs As Database
    Dim Path As String, Path1 As String


    Path1 = "/NH_Appeals_tbl0028_template.xlsx"
    Set dbs = CurrentDb

    Set rstable =dbs.OpenRecordset("tbl0028_All_Appeals")

    Set excelApp = CreateObject("Excel.application","")
    excelApp.Visible = True

    Set targetWorkbook =excelApp.Workbooks.Open(CurrentProject.Path & Path1)
    targetWorkbook.Worksheets("Sheet1").Range("C2").Co pyFromRecordsetrstable

    End Function


  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,369
    Your path string has a forward slash instead of a back slash. Suggest you do yourself and us these favours when posting code:
    copy and paste your code to avoid typos (maybe you did that)
    use code tags with proper indentation. See how this is easier to read and does not add extra spaces or in some cases, drop characters like \

    Code:
    Function ExportAppeals28()
    
    Dim dbs As Database
    Dim Path As String, Path1 As String
    
    Path1 = "\NH_Appeals_tbl0028_template.xlsx"
    Set dbs = CurrentDb
    
    Set rstable =dbs.OpenRecordset("tbl0028_All_Appeals")
    
    Set excelApp = CreateObject("Excel.application","")
    excelApp.Visible = True
    
    Set targetWorkbook =excelApp.Workbooks.Open(CurrentProject.Path & Path1)
    targetWorkbook.Worksheets("Sheet1").Range("C2").CopyFromRecordset rstable
    
    End Function
    Why do you not declare four of your object variables here. Have done that at the module level?
    Also, look at the copyrecordset part in your original code and compare.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    Quote Originally Posted by Micron View Post
    Your path string has a forward slash instead of a back slash. Suggest you do yourself and us these favours when posting code:
    copy and paste your code to avoid typos (maybe you did that)
    use code tags with proper indentation. See how this is easier to read and does not add extra spaces or in some cases, drop characters like \

    Code:
    Function ExportAppeals28()
    
    Dim dbs As Database
    Dim Path As String, Path1 As String
    
    Path1 = "\NH_Appeals_tbl0028_template.xlsx"
    Set dbs = CurrentDb
    
    Set rstable =dbs.OpenRecordset("tbl0028_All_Appeals")
    
    Set excelApp = CreateObject("Excel.application","")
    excelApp.Visible = True
    
    Set targetWorkbook =excelApp.Workbooks.Open(CurrentProject.Path & Path1)
    targetWorkbook.Worksheets("Sheet1").Range("C2").CopyFromRecordset rstable
    
    End Function
    Why do you not declare four of your object variables here. Have done that at the module level?
    Also, look at the copyrecordset part in your original code and compare.
    Thanks for finding the slash mistake. This is the module level; it's all the code there is. I'm still not sure how to save this target file.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,264
    Probably something along the lines of
    Code:
    targetWorkbook.Save
    https://www.google.com/search?q=save...hrome&ie=UTF-8
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,369
    targetworkbook.Save? Or perhaps targetworkbook.Close - cannot recall if you will get a prompt. You didn't say if saving to a new file from a template (or I've missed that) otherwise I would suggest targetworkbook.SaveAs

    This is where I go when I need automation refreshers for Excel
    http://www.accessmvp.com/KDSnell/EXCEL_MainPage.htm

    This is the module level; it's all the code there is
    No, that is at the procedure level. Maybe you mean this code is in a standard module? Module level code exists at the top of the module in its declaration section and lies outside of any procedures. I presume you do not have Option Explicit right after the Option Compare statement otherwise you wouldn't get away with not declaring these object variables within the function. It may work but it's not the best approach. I must be getting rusty because I'm not getting why that code doesn't error out because of it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    Quote Originally Posted by Micron View Post
    targetworkbook.Save? Or perhaps targetworkbook.Close - cannot recall if you will get a prompt. You didn't say if saving to a new file from a template (or I've missed that) otherwise I would suggest targetworkbook.SaveAs

    This is where I go when I need automation refreshers for Excel
    http://www.accessmvp.com/KDSnell/EXCEL_MainPage.htm

    No, that is at the procedure level. Maybe you mean this code is in a standard module? Module level code exists at the top of the module in its declaration section and lies outside of any procedures. I presume you do not have Option Explicit right after the Option Compare statement otherwise you wouldn't get away with not declaring these object variables within the function. It may work but it's not the best approach. I must be getting rusty because I'm not getting why that code doesn't error out because of it.
    Here is my Final code that works fine. Thanks for your help.

    Function ExportAppeals16()

    Dim dbs As Database
    Dim Path As String, Path1 As String

    Path1 = "\ACNH_APPEALS.16.xlsx"
    Set dbs = CurrentDb

    Set rstable = dbs.OpenRecordset("tbl0070_FINAL_APPEALS_16")



    Set excelApp = CreateObject("Excel.application", "")
    excelApp.Visible = False


    Set targetWorkbook = excelApp.Workbooks.Open(CurrentProject.Path & Path1)
    targetWorkbook.Worksheets("Appeals.16").Range("A2" ).CopyFromRecordset rstable


    If Dir(CurrentProject.Path & "\template" & Path1) <> "" Then
    Kill (CurrentProject.Path & "\template" & Path1) 'Delete (strPath)
    End If

    targetWorkbook.SaveAs FileName:=CurrentProject.Path & "\template" & Path1

    targetWorkbook.Close savechanges:=True


    End Function

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,369
    use code tags with proper indentation.

    You're welcome.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You have several issues with your code.

    I would be willing to bet that you do not have
    Code:
    Option Compare Database
    Option Explicit
    as the top two lines of EVERY code module.

    You should have "Dim dbs As DAO.Database"
    You have not declared variables "rstable, excelApp, targetWorkbook"
    You did not do proper clean up
    Code:
        rstable.Close
        Set rstable = Nothing
    
        Set targetWorkbook = Nothing
        Set excelApp = Nothing
        Set dbs = Nothing
    I've modified your code - but it is UNTESTED!
    Code:
    Option Compare Database
    Option Explicit
    
    Function ExportAppeals16()
    
        Dim dbs As DAO.Database
        Dim rstable As DAO.Recordset
        Dim Path As String, Path1 As String
        Dim excelApp As Object
        Dim targetWorkbook As Object
    
        Path1 = "\ACNH_APPEALS.16.xlsx"
        Set dbs = CurrentDb
    
        Set rstable = dbs.OpenRecordset("tbl0070_FINAL_APPEALS_16")
    
        Set excelApp = CreateObject("Excel.application", "")
        excelApp.Visible = False
    
    
        Set targetWorkbook = excelApp.Workbooks.Open(CurrentProject.Path & Path1)
        targetWorkbook.Worksheets("Appeals.16").Range("A2").CopyFromRecordset rstable
    
    
        If Dir(CurrentProject.Path & "\template" & Path1) <> "" Then
            Kill (CurrentProject.Path & "\template" & Path1)    'Delete (strPath)
        End If
    
        targetWorkbook.SaveAs FileName:=CurrentProject.Path & "\template" & Path1
    
        targetWorkbook.Close savechanges:=True
    
        rstable.Close
        Set rstable = Nothing
    
        Set targetWorkbook = Nothing
        Set excelApp = Nothing
        Set dbs = Nothing
    End Function

    You might check out Write Data From a Recordset into an EXCEL Worksheet using EXCEL's CopyFromRecordset (VBA) by Ken Snell
    Hid site is Ken's ACCESS Examples and Information for Working With EXCEL Workbook Files



    And -- Please learn how to use code tags.......

  9. #9
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    Thank you again!

    You're right, I don't know how to use code tags. I will try to look on this site for examples.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,264
    Quote Originally Posted by pnpez View Post
    Thank you again!

    You're right, I don't know how to use code tags. I will try to look on this site for examples.
    You either use the # icon, or manually add code and /code, surround by square brackets [ & ]
    I cannot put them together as the site will then rightfully interpret the characters as it should.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    pnpez is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    18
    Steve, your code worked BTW. I'm learning!!!!

  12. #12
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Excellent!! ( that the code worked and you are learning)

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

Similar Threads

  1. Replies: 4
    Last Post: 12-31-2019, 09:45 AM
  2. lookup field source and target?
    By ntambomvu in forum Forms
    Replies: 2
    Last Post: 04-26-2019, 08:23 PM
  3. How To Target a section of a field
    By lccrews in forum Programming
    Replies: 9
    Last Post: 05-29-2018, 10:11 AM
  4. Update query to not include skus NOT in target
    By OldenMcdonald in forum Import/Export Data
    Replies: 6
    Last Post: 05-25-2017, 10:41 PM
  5. Open,new and save as an excel target file
    By dacodac in forum Programming
    Replies: 7
    Last Post: 01-31-2013, 05:25 AM

Tags for this Thread

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