You can use the Microsoft Access security features to control user access to different objects in your database. Then you can use the
CurrentUser() function to get the name of the user who is currently logged in.
Displaying the User Name in a Control on a Form
To display a user name in a control on a form, follow these steps:
- Create a form, and then add an unbound text box control to the form.
- Set the ControlSource property to =CurrentUser().
- On the View menu, click Form View to see the current user name displayed in the control.
Displaying the User Name in the Title Bar
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click
References on the
Tools menu in the Visual Basic Editor, and make sure that the
Microsoft DAO 3.6 Object Library check box is selected.
To display a user name in the title bar of the
Microsoft Access window, follow these steps:
- Create a new module.
- Type or paste the following code into the new module:
Function ChangeTitle()
Dim dbs As DAO.DATABASE
Dim prp As DAO.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
- Type the following line in the Immediate window, and then press ENTER:
The Microsoft Access title bar change from "Microsoft Access" to "User = username."