A family of Microsoft relational database management systems designed for ease of use.
If you use a variable or a text box in an unbound form the user will have to enter their name every time they open the database. You can avoid this in one of the following ways depending on your set-up:
1. If each user has their own front end, either on their local drive, or in a personal location on the network to which only they have permissions, then you can easily avoid their having to re-enter their name each time.
To do this add a table, UserDetails say, to the front end with just a single column, UserName say. To start with 'seed' the table with one row with the value 'Enter your name here' for instance. Then create a little form bound to this table with no navigation buttons, and its AllowAdditions and AllowDeletions properties set to False (No). You can either automatically open the form at start-up or provide some mechanism for the user to open it on demand.
To get the name at any time create a little function in a standard module:
Function GetCurrentUser() As String
GetCurrentUser= DLookup("UserName ","UserDetails")
End Function
You can then call the GetCurrentUser() function wherever you wish to return the user's name. You could of course extend this by adding other columns to the UserDetails table for any other information about a user you might wish to use in the database, and write other little functions to get this.
2. If users don't use their own personal front end you can achieve the same thing by storing all users' names in a single table in the back end, but as well as the UserName column add a LoginName column in which the user's system login name is stored. Leave the table empty to start with.
Add the following module to the database to get the login name:
' module basGetUser
Option Compare Database
Option Explicit
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal _
lpBuffer As String, nSize As Long) As Long
Public Function GetUser() As String
Dim strBuffer As String
Dim lngSize As Long, lngRetVal As Long
lngSize = 199
strBuffer = String$(200, 0)
lngRetVal = GetUserName(strBuffer, lngSize)
GetUser = Left$(strBuffer, lngSize - 1)
End Function
Then base the form for entering the name on the following query:
SELECT *
FROM UserDetails
WHERE LoginName = GetUser();
To provide a means for a user to enter their name for the first time, put the following in the form's Load event procedure:
Dim strCriteria As String
Dim strSQL As String
strSQL = "INSERT INTO UserDetails(LoginName,UserName) " & _
"VALUES(""" & GetUser() & """,""Enter name here"")"
strCriteria = "LoginName = """ & GetUser() & """"
If IsNull(DLookup("LoginName", "UserDetails", strCriteria)) Then
CurrentDb.Execute strSQL, dbFailOnError
Me.Requery
End If