Solved

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

Posted on 2016-11-16
5
21 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
5 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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 119

Expert Comment

by:Rey Obrero
Comment Utility
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 34

Expert Comment

by:PatHartman
Comment Utility
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
Comment Utility
thank you.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now