ACC: Obtain Name of Current User Database Using Access Basic
ID: Q89681
|
The information in this article applies to:
-
Microsoft Access versions 1.0, 1.1, 2.0
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
"DBEngine," and then "DBEngine Object" 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 "Introduction to Programming" manual in Microsoft
Access version 1.x, or the "Building Applications" 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 "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
- Type the following line in the Immediate window, and then press ENTER:
?GetDBName()
Note that the name of the database is returned.
REFERENCES
For more information about using DAO, please see the following article in
the Microsoft Knowledge Base:
Q113919 ACC: How to Get the Name of the Current Database
Additional query words:
dde dbname
Keywords : kbprg
Version : 1.0 1.1 2.0
Platform : WINDOWS
Issue type : kbhowto
|