Solved

Determine current project path Ms Access

Posted on 2011-03-08
2
620 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
[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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

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