Solved

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

Posted on 2016-11-16
5
34 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 49

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 49

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 119

Expert Comment

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

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

863 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

27 Experts available now in Live!

Get 1:1 Help Now