Solved

Export code - file path based on either at Home or Work

Posted on 2016-11-16
5
53 Views
Last Modified: 2016-11-16
Experts, how could I modify the below code to choose the file path being exported to?  I continually have to change the code based on if I am at home or at work.  

thank you

Private Sub cmdExport_Click()

    Dim shtName As String
    shtName = "Export-" & Format(Date, "yyyymmdd")
   'Work:
    DoCmd.TransferSpreadsheet acExport, , "qryExport_FC_Archive_4Pivot", "C:\Users\johnsoat\Desktop\ExportArch4Pivot.xls", True, shtName
   'home:
    DoCmd.TransferSpreadsheet acExport, , "qryExport_FC_Archive_4Pivot", "C:\Users\pdvsa\Desktop\ExportArch4Pivot.xls", True, shtName


End Sub

Open in new window

0
Comment
Question by:pdvsa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 41889525
Hi,

pls try
Private Sub cmdExport_Click()

    Dim shtName As String
    shtName = "Export-" & Format(Date, "yyyymmdd")
    If Dir("C:\Users\johnsoat\Desktop\", vbDirectory) <> "" Then
        'Work:
        DoCmd.TransferSpreadsheet acExport, , "qryExport_FC_Archive_4Pivot", "C:\Users\johnsoat\Desktop\ExportArch4Pivot.xls", True, shtName
    Else
        'home:
        DoCmd.TransferSpreadsheet acExport, , "qryExport_FC_Archive_4Pivot", "C:\Users\pdvsa\Desktop\ExportArch4Pivot.xls", True, shtName
    End If


End Sub

Open in new window

Regards
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41889534
or
Private Sub cmdExport_Click()

    Dim shtName As String
    shtName = "Export-" & Format(Date, "yyyymmdd")
    If FolderExists("C:\Users\johnsoat\Desktop\") Then
        'Work:
        DoCmd.TransferSpreadsheet acExport, , "qryExport_FC_Archive_4Pivot", "C:\Users\johnsoat\Desktop\ExportArch4Pivot.xls", True, shtName
    Else
        'home:
        DoCmd.TransferSpreadsheet acExport, , "qryExport_FC_Archive_4Pivot", "C:\Users\pdvsa\Desktop\ExportArch4Pivot.xls", True, shtName
    End If


End Sub

Public Function FolderExists(strFolderPath As String) As Boolean
    On Error Resume Next
    FolderExists = (GetAttr(strFolderPath) And vbDirectory) = vbDirectory
    On Error GoTo 0
End Function

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41889720
try using environ("userProfile)  which will give you  "C:\Users\pdvsa" if you are at HOME  or "C:\Users\johnsoat"  if you are at WORK


 Dim shtName As String
 shtName = "Export-" & Format(Date, "yyyymmdd")

DoCmd.TransferSpreadsheet acExport, , "qryExport_FC_Archive_4Pivot",  environ("userProfile) & "\Desktop\ExportArch4Pivot.xls", True, shtName

Open in new window

0
 
LVL 37

Expert Comment

by:PatHartman
ID: 41889801
Don't hardcode paths.  It is too tough to find them all if they need to be changed.  It is better to create a table to hold certain types of settings.  The table can include a HomePath and WorkPath.  The login form should have an option group for Home or Work and the exports check the option and then use the correct path.  Have the option group either default to the most common usage or make it required and have the user choose each time.
0
 

Author Closing Comment

by:pdvsa
ID: 41890081
thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question