Solved

Determine current project path Ms Access

Posted on 2011-03-08
2
613 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:budorat
2 Comments
 
LVL 5

Author Comment

by:budorat
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 500 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

17 Experts available now in Live!

Get 1:1 Help Now