Solved Changing a labels caption using a variable (1 Viewer)

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
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.


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
 

deletedT

Guest
Local time
Today, 05:28
Joined
Feb 2, 2019
Messages
1,218
I'm answering from my phone and it's hard to see the whole code. Try this:
Me.lblWelcome.Caption =Username
 

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
I'm answering from my phone and it's hard to see the whole code. Try this:
Me.lblWelcome.Caption =Username

Thanks for the response, it says variable not definded, and highlights the "Private Sub Form_Load()" just above it on the code
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:28
Joined
Sep 21, 2011
Messages
14,238
When ever you assign a value you use the equal sign =, unless it is an object.?
You have done that to get username, why have you not doen it for the caption?

I see Tera has replied since I started this.

I think your Public is only to the form as you declared it there.? Put it into a Module.
Make it a Tempvar.? I made mine a Tempvar so I could use throughout the DB.

You could also pass it to the form via OpenArgs?
 
Last edited:

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
When ever you assign a value you use the equal sign =, unless it is an object.?
You have done that to get username, why have you not doen it for the caption?

I see Tera has replied since I started this.

I think your Public is only to the form as you declared it there.? Put it into a Module.
Make it a Tempvar.? I made mine a Tempvar so I could use throughout the DB.

You could also pass it to the form via OpenArgs?

As I said before, I'm quite new to VBA and MS Access so lots of this stuff is likely gonna be new to me.

I put "Dim Username As TempVar" at the top of the code, that still didn't work.
If I didn't understand you correctly with TempVar then please correct me.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:28
Joined
Sep 21, 2011
Messages
14,238
Probably the easiest for you at this stage is just take that Public statement and put it into a seperate module. Then it will be available everywhere.
A Tempvar is just a later form of Public/Global variable.
 

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
Probably the easiest for you at this stage is just take that Public statement and put it into a seperate module. Then it will be available everywhere.
A Tempvar is just a later form of Public/Global variable.

Sorry, what do you mean by a seperate module? I'm assuming the modules are all the different pages that I have on the VBA IDE?
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:28
Joined
Sep 21, 2011
Messages
14,238
Open the VBA window, (Alt + F11) and you will see your forms etc on the left.
A module is an object where you place code that will be used elsewhere many times. It could be a function or a sub. A Form is actually a module, but code in that has only scope to that form.
TBH you should have been taught all this.? :unsure:
Below you will see some of mine. This is a test DB, so not fussy with the names. I have one module for Functions, a ModTest, a Module etc etc.

In Module1 I have

1589277714594.png


1589277609115.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:28
Joined
May 7, 2009
Messages
19,229
remove the Public username from the Form's module.
on VBA, Add a Module and move your variable there declaring it Public or Global.
I see flaw on your code.
you are limiting 1 user for a distinct user/password.
when in reality, like your name, there can be duplicate or many.
the only difference is that each same name uses different password.
 

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
Open the VBA window, (Alt + F11) and you will see your forms etc on the left.
A module is an object where you place code that will be used elsewhere many times. It could be a function or a sub.

Below you will see some of mine. This is a test DB, so not fussy with the names. I have one module for Functions, a ModTest, a Module etc etc.

In Module1 I have

View attachment 82071

View attachment 82070

ah I understand, thanks I'll give it a shot now
 

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
Open the VBA window, (Alt + F11) and you will see your forms etc on the left.
A module is an object where you place code that will be used elsewhere many times. It could be a function or a sub. A Form is actually a module, but code in that has only scope to that form.
TBH you should have been taught all this.? :unsure:
Below you will see some of mine. This is a test DB, so not fussy with the names. I have one module for Functions, a ModTest, a Module etc etc.

In Module1 I have

View attachment 82071

View attachment 82070

I didn't end up getting any errors, but this time the label disappeared... I'm assuming it had nothing in it so it looked like it wasn't there?

Just to confirm this is the right code for changing the label caption: "Me.lblWelcome.Caption = Username"
 

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
remove the Public username from the Form's module.
on VBA, Add a Module and move your variable there declaring it Public or Global.
I see flaw on your code.
you are limiting 1 user for a distinct user/password.
when in reality, like your name, there can be duplicate or many.
the only difference is that each same name uses different password.
ah thanks for the feedback, I'll look into that!
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:28
Joined
Sep 21, 2011
Messages
14,238
I didn't end up getting any errors, but this time the label disappeared... I'm assuming it had nothing in it so it looked like it wasn't there?

Just to confirm this is the right code for changing the label caption: "Me.lblWelcome.Caption = Username"
That is the correct syntax, but the label will only show the UserName? You would need
Code:
Me.lblWelcome.Caption = "Welcome " & Username
If you set the Caption to "Welcome" in the first place then you would use
Code:
Me.lblWelcome.Caption = Me.lblWelcome.Caption & " " & Username
that way you would at least always have something to see?

HTH
 

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
That is the correct syntax, but the label will only show the UserName? You would need
Code:
Me.lblWelcome.Caption = "Welcome " & Username
If you set the Caption to "Welcome" in the first place then you would use
Code:
Me.lblWelcome.Caption = Me.lblWelcome.Caption & " " & Username
that way you would at least always have something to see?

HTH

Yeah that works better actually, but there's still a problem, it's not finding the username. So when I use your example of:

Code:
Me.lblWelcome.Caption = "Welcome " & Username

It changes the label to just "welcome" so I'm assuming there's an error in actually getting the username maybe I don't know
 

Ryan142

Member
Local time
Today, 05:28
Joined
May 12, 2020
Messages
52
That is the correct syntax, but the label will only show the UserName? You would need
Code:
Me.lblWelcome.Caption = "Welcome " & Username
If you set the Caption to "Welcome" in the first place then you would use
Code:
Me.lblWelcome.Caption = Me.lblWelcome.Caption & " " & Username
that way you would at least always have something to see?

HTH
Nevermind, I fixed it. I changed the way it got the username from instead of taking it from the databse, to grab it from the text box since the system already knows that it's correct. Then I had to change the order of it as it was trying to grab it from a text box that was in the form that had been closed a couple lines before...

Thanks for your help! Should be all fixed now!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:28
Joined
May 7, 2009
Messages
19,229
look at your code again.
you are assigning the value to the variable After you have opened the Form.
 

Users who are viewing this thread

Top Bottom