Microsoft KB Archive/89681

= ACC: Obtain Name of Current User Database Using Access Basic =

Article ID: 89681

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 Q89681



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

Microsoft Access version 1.x does not have a built-in way to determine the name of the database that the user currently has open. This article lists a sample Access Basic function that you can use in Microsoft Access 1.x to find the name of the database.

In Microsoft Access version 2.0, you can find the name of the database using data access objects (DAO). With DAO, you can use the Name property of the DBEngine object to find the path and name of the currently open database. For more information about the DBEngine object, search for &quot;DBEngine,&quot; and then &quot;DBEngine Object&quot; using the Microsoft Access 2.0 Help menu.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x, or the &quot;Building Applications&quot; manual in version 2.0.



MORE INFORMATION
Although you can obtain the object variable of the current user database by using the CurrentDB function, you are unable to obtain the text representation of the database name. The following example shows how a user- defined function called GetUserDBName obtains a text representation of the user database name.

The GetUserDBName function returns the name of the database with no path or extension. You must add your own path or extension string to the result of the GetUserDBName function when necessary.

NOTE: The code to determine the current database does not work with run-time versions of Microsoft Access (MSARN110.EXE).

  Type the following sample code into a module.

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.

NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code.

Option Compare Database 'Use database order for string comparisons.

Option Explicit

Declare Function GetWindow% Lib &quot;USER&quot; Alias &quot;GetWindow&quot; _ (ByVal hwnd%, ByVal Relationship%) Declare Function GetClassName% Lib &quot;USER&quot; Alias &quot;GetClassName&quot; _ (ByVal hwnd%, ByVal ClassName$, ByVal Size%) Declare Function GetWindowText% Lib &quot;User&quot; Alias &quot;GetWindowText&quot; _ (ByVal hwnd%, ByVal StringText$, ByVal wInt%) Declare Function GetActiveWindow% Lib &quot;USER&quot; Alias _ &quot;GetActiveWindow&quot; Declare Function GetParent% Lib &quot;USER&quot; Alias &quot;GetParent&quot; (ByVal _                      hwnd%)

Const GW_HWNDNEXT = 2 Const GW_CHILD = 5

Const AccessDBC = &quot;ODb&quot; Const AccessMDICLIENT = &quot;MDIClient&quot; Const ACCESSMain = &quot;OMain&quot;

Function GetDBName Dim hwnd As Integer Dim ClassName As String, ClassLen As Integer Dim Caption As String, CaptionLen As Integer Dim Start As Integer, fMDIClient As Integer Dim RetVal

'        ' Get the handle to the Microsoft Access window. '        hwnd = GetActiveWindow ClassName = Space(127) ClassLen = GetClassName(hwnd, ClassName, Len(ClassName)) While ((Left$(ClassName$, ClassLen) <> &quot;OMain&quot;)) hwnd = GetParent(hwnd) ClassLen = GetClassName(hwnd, ClassName, Len(ClassName)) Wend

'        ' Traverse the children looking for the 'MDIClient' child. '        hwnd = GetWindow(hwnd, GW_CHILD) Do While hwnd And Not fMDIClient ClassLen = GetClassName(hwnd, ClassName, Len(ClassName)) If Left$(ClassName, ClassLen) = &quot;MDIClient&quot; Then fMDIClient = True Else hwnd = GetWindow(hwnd, GW_HWNDNEXT) End If        Loop

'        ' Traverse children until we find the Database Window 'ODb' child ' .. which has a title of &quot;Database: &quot;. '        hwnd = GetWindow(hwnd, GW_CHILD) Do While hwnd ClassLen = GetClassName(hwnd, ClassName, Len(ClassName)) ClassName = Mid$(ClassName, 1, ClassLen) If Left$(ClassName, ClassLen) = &quot;ODb&quot; Then Caption = Space$(127) RetVal = GetWindowText(hwnd, Caption, Len(Caption)) Start = InStr(Caption, &quot;:&quot;) + 2 GetDBName = Mid$(Caption, Start) Exit Function End If              hwnd = GetWindow(hwnd, GW_HWNDNEXT) Loop

GetDBName = &quot;Not Found&quot;

End Function  Type the following line in the Immediate window, and then press ENTER:

?GetDBName

Note that the name of the database is returned.

