Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Determine current project path Ms Access

Posted on 2011-03-08
2
Medium Priority
?
635 Views
Last Modified: 2012-05-11
Hi,
I have been using the following code for some time to allow me to transfer data to an upgraded version of the database. See code snippet 1 below.

Basically it determines the existing Desktop file path.

I now want to change it to all me to transfer the data to a DB in the same folder as the open DB. I have the following code that seems to work, I just need to know if there are any issues I should consider that I have not in the code below.

Do I really need the following part.....
   
If Dir$(sUpgradePath, vbDirectory) = "" Then
        sUpgradePath = Environ("UserProfile") & "\Desktop\"                  'For use with Vista/Win7
    End If

Is there any possibility that Access just would not be able to work out the directory path ?

Any advice would be greatly appreciated.

Kev
'********************code snippet 1 ****************************
On Error GoTo Err_Handler
Dim RS As DAO.Recordset, sSql As String, strQuestion As String, sUpgradePath As String
    DoCmd.SetWarnings False



'Determine user Desktop path
    sUpgradePath = "c:\Desktop\"                                             'For use with WinXP on DRN
    
    If Dir$(sUpgradePath, vbDirectory) = "" Then
        sUpgradePath = Environ("UserProfile") & "\Desktop\"                  'For use with Vista/Win7
    End If
    
    sUpgradePath = sUpgradePath & DLookup("SysUpgradeTitle", "systblVersion", "")


'************************* code snippet 2 **************************************

'Determine current path
    sUpgradePath = CurrentProject.path
    
    'Check for terminating backslash sUpgradePath filepath.
    If Right(sUpgradePath, 1) <> "\" Then
       sUpgradePath = sUpgradePath & "\"
    End If
    
    If Dir$(sUpgradePath, vbDirectory) = "" Then
        sUpgradePath = Environ("UserProfile") & "\Desktop\"                  'For use with Vista/Win7
    End If

Open in new window

0
Comment
Question by:Kev
2 Comments
 
LVL 5

Author Comment

by:Kev
ID: 35068438
Hi,

I just realized that I should reorder the sequence as follows. Any other suggestions ???

Kev
'Determine current path
    sUpgradePath = CurrentProject.path
    
   If Dir$(sUpgradePath, vbDirectory) = "" Then
        sUpgradePath = Environ("UserProfile") & "\Desktop\"             
   End If
    
	'Check for terminating backslash sUpgradePath filepath.
    If Right(sUpgradePath, 1) <> "\" Then
       sUpgradePath = sUpgradePath & "\"
    End If
           
    sUpgradePath = sUpgradePath & DLookup("SysUpgradeTitle", "systblVersion", "")

Open in new window

0
 
LVL 11

Accepted Solution

by:
LambertHeenan earned 2000 total points
ID: 35070469
Using Environ() will work in some situations and not in others. It depends how the network / login scrips have been set up and also on the Office security settings.

My suggestion is to use the API that retunrs those special folders.

Using the attached code the desktop folder is returned by

GetSpecialFolder(&H10)



'
' Declares for Shell Items
'
Private Type SHITEMID
    cb As Long
    abID As Byte
End Type
'
Private Type ITEMIDLIST
    MKID As SHITEMID
End Type
'
' Enumerations of Folders - useful for providing access
' to the GetSpecialFolder Path routine
'
Public Enum fldrIDs
    fldrPrograms = &H2
    fldrControls = &H3
    fldrPrinters = &H4
    fldrPersonal = &H5
    fldrFavorites = &H6
    fldrStartup = &H7
    fldrRecent = &H8
    fldrSendTo = &H9
    fldrBitBucket = &HA
    fldrStartMenu = &HB
    fldrDeskTopDir = &H10
    fldrDrives = &H11
    fldrNetWork = &H12
    fldrNetHood = &H13
    fldrFonts = &H14
    fldrTemplates = &H15
    fldrCommonStartMenu = &H16
    fldrCommonPrograms = &H17
    fldrCommonStartup = &H18
    fldrCommonDeskTopDir = &H19
    fldrCommonAppData = &H1A
End Enum
'
' API Declares
'
Private Declare Function SHGetSpecialFolderLocation _
        Lib "shell32.dll" (ByVal hwndOwner As Long, _
        ByVal nFolder As Long, pidl As ITEMIDLIST) As Long

Public Function GetSpecialFolder(ByVal CsIdl As fldrIDs) As String
'
' This routine returns the fully qualified path to the
' required folder.
'
' Obviously this can change depending on the logged in
' user
'

    Dim lRet As Long
    Dim sPath As String
    Dim sTemp As String
    Dim IDL As ITEMIDLIST
'
On Error GoTo vbErrorHandler
'
' Get the location into our ITEMIDLIST
'
    lRet = SHGetSpecialFolderLocation(0, CsIdl, IDL)
'
    If lRet = 0 Then ' no error !
'
' Build a buffer string to handle the returned value
'
        sPath = Space$(255)
        lRet = SHGetPathFromIDList(ByVal IDL.MKID.cb, ByVal sPath)
        If lRet <> 0 Then
'
' Make sure we append a '\' to the path
'
            sTemp = TrimNull(sPath)
            sTemp = sTemp & IIf(right$(sTemp, 1) = "\", "", "\")
            GetSpecialFolder = sTemp
        End If
    End If
'
'
    Exit Function
'
' Something strange happened - raise an error !
'
vbErrorHandler:
    Err.Raise Err.Number, "cSpecialFolders::GetSpecialFolder", Err.Description
'
End Function

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

885 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