Results 1 to 12 of 12
  1. #1
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188

    Exporting query to shared drive

    Hi all,


    I'm using VBA to export a query to Excel. The path is a OneDrive location that is synced to all of our C: drives. It works for me, but when other users try to export it gives them an error because the path has my username (Mateyr).

    My question is - what syntax can I use to replace my username so it works for everyone?

    Code:
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qry_Product_Name", "C:\Users\Mateyr\ABC\Documents\Database\rpt_Product_Name.xlsx", True

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,558
    Their username?

    Look at the Environ() function.
    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

  3. #3
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    I used the " & Environ("username") & " function and got it to work.

  4. #4
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    So the query exports fine, but now I have a different issue. It exports the query to the designated folder but each time we export it, instead of replacing the existing .xlsx file it just adds a new tab. How do I change the code to replace the existing file?

    Here's what I have right now.

    Code:
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qry_Product_Name", "C:\Users\" & Environ("username") & "\ABC\Documents\Database\rpt_Product_Name.xlsx", True

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,558
    Delete the file first? as it does not do that for me?, however I only exported to a temp local drive.
    Try that location and see if it still does it.
    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

  6. #6
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Quote Originally Posted by Welshgasman View Post
    Delete the file first? as it does not do that for me?, however I only exported to a temp local drive.
    Try that location and see if it still does it.

    Yeah, I can ask the users to delete the file first, but I thought if it asked them to replace it that would be easier.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,558
    Quote Originally Posted by matey56 View Post
    Yeah, I can ask the users to delete the file first, but I thought if it asked them to replace it that would be easier.
    No!, you delete the file with VBA. Test to see if it exists first, if it does, delete it.
    Then Transfer the query.
    I would test on a non OneDrive local folder first, as it worked for me. Maybe something to do with OneDrive, but I cannot think what that might be.
    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

  8. #8
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Quote Originally Posted by Welshgasman View Post
    No!, you delete the file with VBA. Test to see if it exists first, if it does, delete it.
    Then Transfer the query.
    I would test on a non OneDrive local folder first, as it worked for me. Maybe something to do with OneDrive, but I cannot think what that might be.
    Ah, I see. What is the syntax to delete the file first with VBA? Interestingly enough I switched the code to save it to my desktop and I get the same thing. It doesn't replace the file - just adds a new tab.

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,558
    Have a google to see how to delete a file with vba. You probably need to do the same to see how you test if a file exists.
    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

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,558
    Have a google to see how to delete a file with vba. You probably need to do the same to see how you test if a file exists.
    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
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Quote Originally Posted by Welshgasman View Post
    Have a google to see how to delete a file with vba. You probably need to do the same to see how you test if a file exists.
    I got it working. I used the Kill function. It's just Kill "path/file"

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,558
    And to see if it exists?
    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

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

Similar Threads

  1. Shared Network Drive Databases
    By twb60 in forum Access
    Replies: 5
    Last Post: 06-21-2016, 11:47 AM
  2. Shared Drive Access Issue
    By Classic in forum Access
    Replies: 6
    Last Post: 09-23-2014, 06:03 AM
  3. Replies: 2
    Last Post: 09-04-2013, 11:01 PM
  4. shared DB on local drive
    By mike02 in forum Access
    Replies: 1
    Last Post: 05-20-2013, 01:54 PM
  5. Multi Users on shared drive
    By wallen in forum Access
    Replies: 0
    Last Post: 08-25-2008, 08:57 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