问题
I have an Access database I'm working on which has a simple login system consisting of a drop down (combo) menu to select the username, a password field and a users table.
Inside the users table, I'm storing the username, password and the first name of all the users. I've managed to get my login form working, but I'd like the user's first name to be displayed in a text box on the next form.
I've managed to get the user's ID number, and display it in a text box, but I haven't been able to get the user's first name.
Does anyone know of a simple way to display the user's first name in a text box, keeping in mind I'll have several forms where I wish to display the user's first name?
Thanks
回答1:
As your description says you are using a ComboBox, I am sure the RowSource would be.
SELECT ID, userNameField
FROM EmpAuth;
The ComboBox properties are Bound Column - 1, Column Count - 2, Column Widths - 0cm;2.5cm (something like this, but 0cm for sure). Now all you have to do is, make the following changes, RowSource :
SELECT ID, userNameField, firstNameField
FROM EmpAuth;
ComboBox properties are Bound Column - 1, Column Count - 3, Column Widths - 0cm;2.5cm**;0cm**
Then, you can simply use the OpenArgs method where you can pass the ComboBoxes Column 3. like,
If Me.password.Value = DLookup("password", "EmpAuth", _
"[ID]=" & Me.username.Value) Then
ID = Me.username.Value
DoCmd.OpenForm "POSMenu", OpenArgs:=Me.username.Column(2)
'Close logon form and open splash screen
DoCmd.Close acForm, "AuthenticationService", acSaveNo
Else
MsgBox.............
Then Finally in the Second Form's On Load event you can use.
Private Sub Form_Load()
If Len(Me.OpenArgs & vbNullString) > 0 Then _
Me.yourTextBoxName = Me.OpenArgs
End Sub
Hope this helps.
回答2:
If you are going to use the user name information in several forms, perhaps you may consider using session variables to access the information. Its a bit more cleaner than passing it from object to object as an argument.
'On the login page,upon confirming the login is correct,
'Initialise session variables
TempVars.RemoveAll 'Destroy any previous Session
'Retrieve the username from the table
UserName = Dlookup("Usernamecol","UsersTable","UserID=" & SelectedUSerID
'Load the retrieved username into a global session variable
TempVars.Add "GlbUserName", UserName
'Use this whenever you want to show the user name
LoggedInUser = TempVars![GlbUserName]
Me.txtUserName = LoggedInUser
'or
Me.lblUserName.caption = LoggedInUser
来源:https://stackoverflow.com/questions/25500739/microsoft-access-show-name-of-logged-in-user-from-userid