ACC: How to Determine Startup Folder (Directory) of a Program

ID: Q140909

The information in this article applies to:

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

You can use the SysCmd() function to perform several functions. One of these functions is the acSysCmdAccessDir(), which returns the name (<Drive:>\<Path>\) of the folder (directory) where Msaccess.exe is located. It does not return the name of the executable file, Msaccess.exe. This article includes two sample functions that you can use to return the name of the folder and the name of the executable file for any program running in the Microsoft Windows environment.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.

MORE INFORMATION

The CurDir() function returns the current folder. Because you can start Microsoft Access from a folder other than the current folder, and because you can change the current folder with the ChDir statement, you cannot use the CurDir() function to determine the startup folder.

The following sample function uses the Windows API functions GetModuleHandle() and GetModuleFileName(). With the module handle, you can obtain the path with the GetModuleFileName() function.

In Microsoft Access 7.0 and 97

1. Create a module and type the following lines in the Declarations

   section:

      Option Explicit

      Declare Function GetModuleHandle& Lib "kernel32" Alias _
         "GetModuleHandleA" (ByVal FileName$)

      Declare Function GetModuleFileName& Lib "kernel32" Alias _
         "GetModuleFileNameA" (ByVal hModule&, ByVal FileName$, ByVal _
            nSize&)

      NOTE: Aliases used in Win32 are case-sensitive.

2. Type the following procedure:

      Function StartUp_Dir()
         Dim hModule&, Buffer$, Length&, Msg$
         hModule& = GetModuleHandle("MSACCESS.EXE")
         Buffer$ = Space$(255)
         Length& = GetModuleFileName(hModule&, Buffer$, Len(Buffer$))
         Buffer$ = Left$(Buffer$, Length&)
         Msg$ = "Startup path and filename: " & Buffer$
         MsgBox Msg$
      End Function

3. To test this function, type the following line in the Debug Window,
   and then press ENTER.

      ? StartUp_Dir()

   Note that the Microsoft Access startup folder and executable name are
   displayed in the Debug window.

In Microsoft Access 2.0

NOTE: In the following sample code, an underscore (_) is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

1. Create a module and type the following lines in the Declarations

   section:

      Option Explicit
      Declare Function GetModuleHandle% Lib "kernel" (ByVal FileName$)
      Declare Function GetModuleFileName% Lib "kernel" (ByVal hModule%,_
                       ByVal FileName$, ByVal nSize%)

2. Type the following procedure:

      Function StartUp_Dir ()
         Dim hModule%, Buffer$, Length%, Msg$
         hModule% = GetModuleHandle("MSACCESS.EXE")
         Buffer$ = Space$(255)
         Length% = GetModuleFileName(hModule%, Buffer$, Len(Buffer$))
         Buffer$ = Left$(Buffer$, Length%)
         Msg$ = "Startup path and filename: " & Buffer$
         MsgBox Msg$
      End Function

3. On the Run menu, click Compile Loaded Modules.

4. On the View menu, click Immediate window. Type the following line in

   the Immediate window, and then press ENTER:

      ? StartUp_Dir()

   Note that the Microsoft Access startup folder and executable name
   appear in the Immediate window.

Uses and Variations

You can both incorporate this function in other program modules and use it in expressions. For example, entering =Startup_Dir() as the OnClick property of a button on a form returns the startup folder of Microsoft Access whenever the button is clicked.

NOTE: You can change the MSACCESS.EXE argument for the Windows API GetModuleHandle() function so that the function returns the startup folder of another program started in the Windows environment. Furthermore, you can pass a program name as a variable to the Windows API function, giving even more flexibility to the function.

Keywords          : kbprg MdlGnrl 
Version           : 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998