The
CurDir() function returns the current folder. Because you can start 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 a new database, 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 are case-sensitive.
- 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
- To test this function, type the following line in the Immediate Window, and then press ENTER:
Note that the Access startup folder and executable name are displayed in the Immediate window.
Uses and Variations
You can 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
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.