ACC: Obtain Name of Current User Database Using Access Basic

This article was previously published under Q89681
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.

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

In Microsoft Access version 2.0, you can find the name of the databaseusing data access objects (DAO). With DAO, you can use the Name propertyof the DBEngine object to find the path and name of the currently opendatabase. For more information about the DBEngine object, search for"DBEngine," and then "DBEngine Object" using the Microsoft Access 2.0 Helpmenu.

This article assumes that you are familiar with Access Basic and withcreating Microsoft Access applications using the programming toolsprovided with Microsoft Access. For more information on Access Basic,please refer to the "Introduction to Programming" manual in MicrosoftAccess version 1.x, or the "Building Applications" manual in version 2.0.
Although you can obtain the object variable of the current user databaseby using the CurrentDB() function, you are unable to obtain the textrepresentation of the database name. The following example shows how auser- defined function called GetUserDBName() obtains a textrepresentation of the user database name.

The GetUserDBName() function returns the name of the database with nopath or extension. You must add your own path or extension string tothe result of the GetUserDBName() function when necessary.

NOTE: The code to determine the current database does not work withrun-time versions of Microsoft Access (MSARN110.EXE).
  1. 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 "USER" Alias "GetWindow" _                       (ByVal hwnd%, ByVal Relationship%)      Declare Function GetClassName% Lib "USER" Alias "GetClassName" _                       (ByVal hwnd%, ByVal ClassName$, ByVal Size%)      Declare Function GetWindowText% Lib "User" Alias "GetWindowText" _                       (ByVal hwnd%, ByVal StringText$, ByVal wInt%)      Declare Function GetActiveWindow% Lib "USER" Alias _                       "GetActiveWindow" ()      Declare Function GetParent% Lib "USER" Alias "GetParent" (ByVal _                       hwnd%)      Const GW_HWNDNEXT = 2      Const GW_CHILD = 5      Const AccessDBC = "ODb"      Const AccessMDICLIENT = "MDIClient"      Const ACCESSMain = "OMain"      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) <> "OMain"))            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) = "MDIClient" 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 "Database: <app name>".         '         hwnd = GetWindow(hwnd, GW_CHILD)         Do While hwnd            ClassLen = GetClassName(hwnd, ClassName, Len(ClassName))               ClassName = Mid$(ClassName, 1, ClassLen)               If Left$(ClassName, ClassLen) = "ODb" Then                  Caption = Space$(127)                  RetVal = GetWindowText(hwnd, Caption, Len(Caption))                  Start = InStr(Caption, ":") + 2                  GetDBName = Mid$(Caption, Start)                  Exit Function               End If               hwnd = GetWindow(hwnd, GW_HWNDNEXT)         Loop         GetDBName = "Not Found"      End Function						
  2. Type the following line in the Immediate window, and then press ENTER:


    Note that the name of the database is returned.
For more information about using DAO, please see the following article inthe Microsoft Knowledge Base:

113919 ACC: How to Get the Name of the Current Database

dde dbname

Article ID: 89681 - Last Review: 10/14/2013 17:51:05 - Revision: 2.0

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • kbnosurvey kbarchive kbhowto kbprogramming KB89681