Hack 58. Implement Unique Usernames
Access Security is great in many multiuser situations because you can assign rights to groups and individuals. However, sometimes this is just a lot of overhead. In a small group of users who all use the same objects, you don't get much added value by implementing security.
The downside of not using security is that all users are given the name Admin. You can confirm this in an unsecured database by going to the Immediate window (Ctrl-G in the VB Editor) and typing the following:
An easy way to use specific names in an unsecured database is to first have users enter their names and then have the entered names available throughout the session. This technique makes sense only in a configuration in which each user works on a local database with the forms. The data tables remain on in a back-end database on the server.
When a user starts up her client-based front end, she is asked to enter her name. This is just an easy affair handled by an input box. A loop keeps testing for her entry, and when she is done, the entry is handed off to the Tag property of the main form. This works best if the main form opens automatically when the user starts up the database. This code goes into the main form's Open event:
Private Sub Form_Open(Cancel As Integer) Dim user_name As String user_name = "" Do Until user_name <> "" user_name = InputBox("Please enter your name", "Enter Name") Loop Me.Tag = user_name End Sub
Throughout the session, the person's name is always available via this simple reference back to the main form and its tag, assuming that the main form is named frmMain. Change the form name to match yours:
Because the application is configured in the way that each user is using a local version of the main form, the reference to the Tag property always returns the user's unique name.
That's all it takes. By accessing the entered username in this way, you can use the name in reports, populate field text boxes with it, use it in queries; in other words, use the name wherever you need it in your application.