Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Place the User Logon Name in a Control on a Form or in the Title Bar of the Microsoft Access Window


View products that this article applies to.

This article was previously published under Q210591
Advanced: Requires expert coding, interoperability, and multiuser skills.


This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

↑ Back to the top


Summary

This article shows you how to display the current user name in a control on a form or in the title bar of the Microsoft Access window.

↑ Back to the top


More information

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:
  1. Create a form, and then add an unbound text box control to the form.
  2. Set the ControlSource property to =CurrentUser().
  3. 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:
  1. Create a new module.
  2. 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
    						
  3. Type the following line in the Immediate window, and then press ENTER:
    ?ChangeTitle()
    						
    The Microsoft Access title bar change from "Microsoft Access" to "User = username."

↑ Back to the top


Keywords: KB210591, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 210591
Revision : 2
Created on : 6/30/2004
Published on : 6/30/2004
Exists online : False
Views : 336