Solved

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

Posted on 2016-11-16
5
52 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 50

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 50

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 36

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

749 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