Apologies if this is quite a basic question - it certainly sounds it.
I'm doing a project on MS Access and I'm completley new to it. I've managed to get by for the most part, however, I've ran into a problem with changing a labels caption using a variable within that caption.
Basically, what's happening is the user logs in. I then declare a variable and set it as public and assign it to the username that the user entered. Then it closes the login screen and loads the menu screen. This has a label that says "Welcome, [Username], this is your user account." Where [Username] is, is where I want the variable.
I've annotated every line of code (which I have to do for my school project) so you can use that to see what I think it's meant to do. As mentioned in the comments, the second subroutine is on a different form, that being frmMainScreenAdmin. And don't worry, yes, I am logging in using the admin login.
Any help would be greatly appreciated!
Thanks, Ryan
I'm doing a project on MS Access and I'm completley new to it. I've managed to get by for the most part, however, I've ran into a problem with changing a labels caption using a variable within that caption.
Basically, what's happening is the user logs in. I then declare a variable and set it as public and assign it to the username that the user entered. Then it closes the login screen and loads the menu screen. This has a label that says "Welcome, [Username], this is your user account." Where [Username] is, is where I want the variable.
Rich (BB code):
Option Compare Binary
Option Explicit
Public Username As String 'Defines a public variable "Username" as data type String
Dim rsLogins As Recordset 'Defines rsLogins as a recordset variable
Private Sub btnLogin_Click() 'On pressing the Login button, begin this subroutine
Set rsLogins = CurrentDb.OpenRecordset("tblUserLogins", dbOpenSnapshot, dbReadOnly) 'Sets rsLogins to take a snapshot of tblUserLogins in readonly mode
'This part of the code checks if the inputted username and password is valid and in the database
rsLogins.FindFirst "UserName='" & Me.txtUsername & "'" 'Finds the first instance of the string put in the text box in the tblUserLogins, then equates this to variable "UserName"
If rsLogins.NoMatch = True Then 'If there is no match for the above code, then perform:
Me.lblUsernameFail.Visible = True 'Make the Label "lblUsernameFail" visible, indicating to the user that the username is wrong
Exit Sub 'Exit the subroutine
End If 'End this IF statement
Me.lblUsernameFail.Visible = False 'If the code doesn't take the route of the above IF statement, ensure that "lblUsernameFail" is not visible
If rsLogins!Password <> Me.txtPassword Then 'If the password in the same record is not equal to the password inputted into the text box then:
Me.lblPasswordFail.Visible = True 'Make the Label "lblPasswordFail" visible, indicating to the user that the password is wrong
Exit Sub 'Exit the subroutine
End If 'End this IF statement
If Trim(Me.txtPassword.Value & vbNullString) = vbNullString Then 'Ensures that there are no spaces in the entry for password, if there is then:
Me.lblPasswordFail.Visible = True 'Make the Label "lblPasswordFail" visible, indicating to the user that the password is wrong
Exit Sub 'Exit the subroutine
End If 'End this IF statement
Me.lblPasswordFail.Visible = False 'If the code doesn't take the route of the above IF statement, ensure that "lblPasswordFail" is not visible
'This part of the code will check what access level the user logging in has
If rsLogins!AccessID = 1 Then 'If the record has a AccessID of 1 then:
DoCmd.Close 'Closes the current form that's open
DoCmd.OpenForm "frmMainScreenUser" 'Then open form called "frmMainScreenUser"
Username = rsLogins!Username 'Sets the variable "Username" to the Username that the user inputted
ElseIf rsLogins!AccessID = 2 Then 'If the record has a AccessID of 2 then:
DoCmd.Close 'Closes the current form that's open
DoCmd.OpenForm "frmMainScreenInstructor" 'Then open form called "frmMainScreenInstructor"
Username = rsLogins!Username 'Sets the variable "Username" to the Username that the user inputted
ElseIf rsLogins!AccessID = 3 Then 'If the record has a AccessID of 3 then:
DoCmd.Close 'Closes the current form that's open
DoCmd.OpenForm "frmMainScreenAdmin" 'Then open form called "frmMainScreenAdmin"
Username = rsLogins!Username 'Sets the variable "Username" to the Username that the user inputted
Else
DoCmd.Close 'Closes the current form that's open
DoCmd.OpenForm "frmMainScreenUser" 'Then open form called "frmMainScreenUser"
End If 'End this IF statement
End Sub 'End the subroutine
'This part here is on a different form hence why I made the variable Public, which I believe is what I'm meant to do.
Private Sub Form_Load()
Me.lblWelcome.Caption (Username)
End Sub
I've annotated every line of code (which I have to do for my school project) so you can use that to see what I think it's meant to do. As mentioned in the comments, the second subroutine is on a different form, that being frmMainScreenAdmin. And don't worry, yes, I am logging in using the admin login.
Any help would be greatly appreciated!
Thanks, Ryan