Microsoft KB Archive/88174

= ACC: Sample Function to Determine the Access Startup Directory =

Article ID: 88174

Article Last Modified on 5/9/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q88174



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

This article describes the steps involved in creating a function that returns the Microsoft Access startup directory.

Microsoft Access does not have a built-in command to return the Microsoft Access startup directory. However, you can accomplish this by making Windows application programming interface (API) calls in Access Basic.

NOTE: Microsoft Access 2.0 does include the function SysCmd to return the directory where the Msacess.exe file is located. See the bottom of this article for more information.



MORE INFORMATION
The Access Basic CurDir$ function returns the current directory. Because Microsoft Access can be started from a directory other than the current directory or the current directory can be changed with the ChDir statement, the CurDir$ function cannot be used to determine the startup directory.

The following sample program module makes use of the Windows API functions GetModuleHandle and GetModuleFileName. With the module handle, the path can be obtained with the GetModuleFileName function.

Creating the Module
The following steps demonstrate how to create the sample Access Basic function that returns the startup directory:

 From the File menu, choose New, and select Module.  Type the following lines in the Declarations section.

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. Option Explicit

Declare Function GetModuleHandle% Lib &quot;kernel&quot; (ByVal FileName$) Declare Function GetModuleFileName% Lib &quot;kernel&quot; (ByVal hModule%, _                         ByVal FileName$, ByVal nSize%)   Type the following function: Function StartUp_Dir Dim hModule%, Buffer$, Length%, Msg$ hModule% = GetModuleHandle(&quot;MSACCESS.EXE&quot;) Buffer$ = Space$(255) Length% = GetModuleFileName(hModule%, Buffer$, Len(Buffer$)) Buffer$ = Left$(Buffer$, Length%) Msg$ = &quot;Startup path and filename: &quot; & Buffer$ MsgBox Msg$ End Function  From the Run menu, choose Compile Loaded Modules in Microsoft Access version 1.x, or choose Compile All in version 2.0. Save the module as Startup Directory.  From the View menu, choose Immediate window. Type the following line in the Immediate window, and then press ENTER: ? StartUp_Dir Note that the Microsoft Access startup directory is displayed in the Immediate window. </ol>

Uses and Variations
The Startup directory is displayed when you type the following line in the Immediate window: ? StartUp_Dir This function can also be incorporated in other program modules and can be used in expressions. For example, entering =Startup_Dir as the OnPush or OnClick property of a button on a form returns the startup directory of Microsoft Access whenever the button is chosen.

NOTE: You can change the MSACCESS.EXE argument for the Windows API GetModuleHandle function so that the function returns the startup directory 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.

SysCmd Function
If all you need is the directory in which the Msacess.exe file is located, you can use the SysCmd function, as in the following example: Function test Dim AccDir As String AccDir = SysCmd(acSysCmdAccessDir) Debug.Print AccDir End Function

<div class="references_section">