Microsoft Access - Show name of logged in user from UserID - sql

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

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.

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

Related

INSERT INTO table in access from inputbox

I'm stumped - and also very new to VBA in access but here is my intent:
I have want the member to select a "Begin" button in access, from there, multiple inputboxes will pop up allowing the user to enter data. I want the data entered (name, date, etc.) to be stored into a label on the form, and at the end the next person who opens the "begin" button has a fresh bunch of inputboxes pop-up and their data is then stored in each label of form. End game, a pretty form is emailed to me.
I've mastered the inputboxes and msgboxes - they pop up and run all the way through. The problem I'm at is when the user enters their information I need it to go onto the form into the appropriate fields so in the end the form can be "printed" as a .pdf and sent to me. Any help would be greatly appreciated!
Sub Begin()
Dim AddName As String
Dim AddAddress As String
Dim AddSupv As String
Dim AddPhone As String
AddName = InputBox("What is your rank and full name?", "Enter Rank and Name")
AddAddress = InputBox("What is your home address? Please include street, city, state and zip.", "Enter Address")
Where is any, would I add a function to insert the data after each inputbox into a field on my form. Form Name is Form1 and I also have a table labeled 'AUS Table' that have fields called 'Name_and_Rank' and 'Address' if the inputbox data must go to a table first.
Variables are not needed, just reference controls.
For label controls:
Me.lblName.Caption = InputBox("What is your rank and full name?", "Enter Rank and Name")
Me.lblAddress.Caption = InputBox("What is your home address? Please include street, city, state and zip.", "Enter Address")
For textbox controls:
Me.tbxName = InputBox("What is your rank and full name?", "Enter Rank and Name")
Me.tbxAddress = InputBox("What is your home address? Please include street, city, state and zip.", "Enter Address")
But if there are textboxes on form, users should just type directly into textboxes.

How to declare a public variable for a specified user login?

I have a login system that works well, using XML files to store logins, and then I can read the users and passwords, see if it matches, if it does, move to the next form, if it doesn't, error message.
However, when the form switches over, I want to be able to display the username in the to left corner, so the user knows who they are displayed as. But I don't know how to memorise the specific user that logged in on the previous form onto the new form now?
I tried making a "public variable" (sort of hard to do for me, not sure if i even did it right), which will read the textbok that the user inputs their name in if the username was correct, and gets dipaslayed for the next form. I don't thin the public vairable worked very well.
If BlnUserFound = True Then
'ActiveUser is the "public vairable"
ActiveUser = tbxUser.Text
'Open the main screen
Me.Hide()
Home.Show()
Else
MessageBox.Show("User Details Not found" & vbCrLf _
& "Please Try Again", "Login Error")
End If
What am I doing wrong?!
Just simply connect it to another new variable name and use it in form 2 e.g
dim ActiveUser2 as string
ActiveUser2 = Form1.ActiveUser then just simply easiest way would be have a label or something on your form, set it to not visible and then just apply ActiveUser2.text to the Label.Text

append data from form to column

I am working on a "issue tracker" access data base where the user enters there data through forms, create new form and edit form.
I have a comment section on my edit form which I have been requested to remained lock, for viewing purposes. So the user can only view the comments.
I have another box below that must "append" or add data to the comment section with a time and user stamp.
My approach is to create a vba code that will allow the user to enter data, and once entered will show in the locked comment section. As the user is working through a "editing" form.
I am fairly new to access and vba and unfortunately I cant find anything that I understand online.
Below is some code use and found searching online. I have but can figure out how to append ( or add ) it to the existing column. It is running fine but the data value I wish to add don't go anywhere?
Private Sub Add_Click()
Dim StrSQL As String
Dim addComments As String
' where addcomment.value is the value desire to append
addCommentStr = Me!addComment.Value
'where IssueTrack is Table, AdditionalComments is column
StrSQL = "INSERT INTO IssueTracker(AdditionalComments) VALUES ('" &
addCommentStr & "' );"
DoCmd.SetWarnings False
DoCmd.RunSQL StrSQL
DoCmd.SetWarnings True
MsgBox ("Comment Added")
End Sub
You wouldn't just keep on appending text to the same record. Where would you end?
So, skip all the code and create a form bound to the table.
Have one field for the date. Set its DefaultValue to: Now()
Set the form's property AllowEdits to: False

Pass Username through LoginForm, to GreetingForm, to NavigationForm

I have a problem with my database. When I Login the Username into the LoginForm the GreetingForm opens and the LoginForm closes. The username appears on the GreetingForm, but when I click on a button on the GreetingForm which than closes and Open the NavigationForm the username does not appear on the NavigationForm. Can you please help me fix this problem. Click on the link below to see more details.
Click on this link please
I would create a tempvars. These were introduced in access 2007 and unlike public variables, they will hold their value under the duress of a code error.
Tempvars are global , can be created in any module type and are supported in queries.
I would also change the textbox to a combobox. Then you can refer to the column number to select the initials.(column width 0cm)
How to use:
Option Compare Database
Option Explicit
Public G_Username As TempVars
Private Sub cmdLogin_Click()
If IsNull(cbo_username) Then
MsgBox "Please Select User"
Else
TempVars!G_Username = Me.cbo_username.column(1)
DoCmd.Close
If CurrentProject.AllForms("GreetingForm").IsLoaded = False Then
DoCmd.OpenForm "GreetingForm", acNormal, , , acFormEdit
End If
End If
End Sub
You can now refer to =G_Username in any form you require by creating a text box and entering =G_Username. Alternatively you should be able to do this in VBA like so:
Forms![GreetingForm]![txtLogin] = tempvars!G_Username
You can repeat this for initials, but refer to whichever column initials is in. Remember counting starts from column(0).

Creating a dynamic table from a combobox in Access

I am trying to set up a query where a field is searched for on a table, the table I want to select the information from will change each time. I have tables for different years of admission to a club (2011,2012 etc). I want a user to be able to select a year from a combobox on a form and then this dynamically change the table the data is being selected from.
The code I am using makes sense to me, but I am a novice. Access says there is a syntax error. Please help!
Code:
SELECT [Admission No#]
FROM [Forms]![Control Form]![YearSelect];
it would be better to use a filter.
Create a form (Continuous Forms or Datasheet) to show search result. You can open it or use as a subform.
Implement OnClick event for Search button.
Dim stFilter as String
stFilter = "age = " & Me.FLD_AGE
'-- first way
DoCmd.OpenForm "SearchResult", , , stFilter
'-- second way
With Me.SearchResult.Form '-- SearchResult is the name of SubForm control
.Filter = stFilter
.FilterOn = True
End With