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



The information in this article applies to:

  • Microsoft Access 1.0
  • Microsoft Access 1.1
  • Microsoft Access 2.0
  • Microsoft Access for Windows 95 7.0
  • Microsoft Access 97
  • Microsoft Visual Basic for Applications 1.0

This article was previously published under Q94599

SUMMARY

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.

MORE INFORMATION

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.

Displaying the User Name in a Control on a Form

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).
  3. Switch the form to Form view to see the current user name displayed in the control.

Displaying the User Name in the Title Bar (Microsoft Access 97 and 7.0)

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>.

Displaying the User Name in the Title Bar (Microsoft 2.0 and 1.x)

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>..

Modification Type:MinorLast Reviewed:8/15/2005
Keywords:kbhowto kbProgramming KB94599