Microsoft KB Archive/94599

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

Article ID: 94599

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



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 &quot;Building Applications with Microsoft Access&quot; 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 &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x or the &quot;Building Applications&quot; 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:

 Create a new module.  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 = &quot;User = &quot; & CurrentUser ' Update title bar on screen. Application.RefreshTitleBar Exit Function

ErrorHandler: If Err.Number = conPropNotFoundError Then Set prp = dbs.CreateProperty(&quot;AppTitle&quot;, dbText, _              &quot;User = &quot; & CurrentUser) dbs.Properties.Append prp Else MsgBox &quot;Error: &quot; & Err.Number & vbCrLf & Err.Description End If        Resume Next End Function   Create a new macro with the RunCode action, using the following as the action argument: ChangeTitle Save the macro and name it ShowUser.  Run the ShowUser macro. You should see the Microsoft Access title bar change from &quot;Microsoft Access&quot; to User = .

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:

 Create a new module.  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 &quot;User&quot; (_                  ByVal hw%, ByVal message%, ByVal wParam%, LParam As Any) Declare Function GetActiveWindow% Lib &quot;User&quot;

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$ = &quot;User = &quot; + User         ' For Version 1.x only stTitle$ = &quot;User = &quot; + CurrentUser  ' For Version 2.0 only x% = SendMessage(GetActiveWindow, WM_SETTEXT, 0, ByVal stTitle$) End Function

'=====================================================================  ' End of code section. '=====================================================================                       </li> On the Run menu, click Compile All.</li> Save the module.</li>  Create a new macro with the RunCode action, using the following as the action argument: ChangeTitle Save the macro and name it ShowUser. </li> Run the ShowUser macro. You should see the Microsoft Access title bar change from &quot;Microsoft Access&quot; to User =. .</li></ol>

Keywords: kbhowto kbprogramming KB94599

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.