ID: Q94599
The information in this article applies to:
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article shows you how to use the CurrentUser() function in Microsoft Access (or User() function in Microsoft Access 1.x), to display the current user name in a control on a form or in the title bar of the Microsoft Access window.
This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.
NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0.
In Microsoft Access, you can implement security to control user access to the different objects in your database. Then, you can use the User() or CurrentUser() function to display the current user's name.
Use the following procedure to display a user name in a control on a form:
1. Create a form and add an unbound text box control to the form.
2. Set the ControlSource property to "=CurrentUser()", (or "=User()" in
Microsoft Access 1.x), without the quotation marks.
3. Switch the form to Form view to see the current user name displayed in
the control.
CAUTION: Following the steps in this example will modify the sample database Northwind.mdb (or NWIND.MDB in Microsoft Access 2.0 or earlier). You may want to back up the Northwind database file and perform these steps on a copy of the database.
Use the following procedure to display a user name in the title bar of the Microsoft Access window in Microsoft Access 97 and 7.0:
1. Create a new module.
2. Enter the following Visual Basic for Applications code:
Function ChangeTitle()
Dim dbs As DATABASE, prp As Property
Const conPropNotFoundError = 3270
On Error GoTo ErrorHandler
' Return Database variable pointing to current database.
Set dbs = CurrentDb
' Change title bar.
dbs.Properties!AppTitle = "User = " & CurrentUser
' Update title bar on screen.
Application.RefreshTitleBar
Exit Function
ErrorHandler:
If Err.Number = conPropNotFoundError Then
Set prp = dbs.CreateProperty("AppTitle", dbText, _
"User = " & CurrentUser)
dbs.Properties.Append prp
Else
MsgBox "Error: " & Err.Number & vbCrLf & Err.Description
End If
Resume Next
End Function
3. Create a new macro with the RunCode action, using the following as the
action argument:
ChangeTitle()
Save the macro and name it ShowUser.
4. Run the ShowUser macro. You should see the Microsoft Access title bar
change from "Microsoft Access" to "User = <username>."
Use the following procedure to display a user name in the title bar of the Microsoft Access window in Microsoft Access 2.0 and 1.x:
1. Create a new module.
2. Enter the following Access Basic code:
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.
'=====================================================================
' Declarations section of the module
'=====================================================================
Option Explicit
Declare Function SendMessage& Lib "User" (_
ByVal hw%, ByVal message%, ByVal wParam%, LParam As Any)
Declare Function GetActiveWindow% Lib "User" ()
Const WM_SETTEXT = &HC
DIM stTitle$
DIM x%
'=====================================================================
' The following function places the user name in the title bar.
'=====================================================================
Function ChangeTitle ()
' Use only one of the following two lines of code,
' depending on your version of Microsoft Access
stTitle$ = "User = " + User() ' For Version 1.x only
stTitle$ = "User = " + CurrentUser() ' For Version 2.0 only
x% = SendMessage(GetActiveWindow(), WM_SETTEXT, 0, ByVal stTitle$)
End Function
'=====================================================================
' End of code section.
'=====================================================================
3. On the Run menu, click Compile All.
4. Save the module.
5. Create a new macro with the RunCode action, using the following as the
action argument:
ChangeTitle()
Save the macro and name it ShowUser.
6. Run the ShowUser macro. You should see the Microsoft Access title bar
change from "Microsoft Access" to "User = <username>."
Additional query words:
Keywords : kbprg
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto
Last Reviewed: November 20, 1998