Solved Public variables (1 Viewer)

Teggun

Registered User.
Local time
Today, 19:22
Joined
Aug 30, 2019
Messages
33
Hi guys, I'm having a problem with public variables. I assumed variables defined as public can be accessed from any form and looks like I can not.

The variable is basically supposed to store IDUser, which is the user logged in and that is operating through the forms, and I need to store it when logging in is successfull and to set the variable as null when logging out.

Code:
Public pbUser as String

Private Sub Login()
If Not isNull(Me.cmbUser) Then
    pbUser = me.cmbUser.Column(0)
    DoCmd.OpenForm "frmMain"
    DoCmd.Close acForm, "frmLogin"
Else
    Msgbox "¡Select the user!"
    Exit Sub
Endif
End Sub

Basically when jumping to the next form pbUser is detected as Null, how can set such a variable up to work like this?

I know it's a very basic thing but I have never worked with "public" variables.

Thanks for your time.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 18:22
Joined
Sep 12, 2006
Messages
15,653
if the variable is in a form, it will disappear as soon as the form is closed. You need it to be in a code module for it to "live" while the database is open.
 

Teggun

Registered User.
Local time
Today, 19:22
Joined
Aug 30, 2019
Messages
33
Thanks, I did not know about this. I created a module with a procedure to set the global variables up and it does work.

mdlGlobalVariables:
Code:
Public pbUser As String

Public Sub GlobalUser(IDUser As String)
pbUser = IDUser
End Sub

frmLogin:
Code:
Private Sub cmdLogin_Click()
If Not IsNull(Me.cmbUsers) then
    Call GlobalUser(Me.cmbUsers.Column(0))
Else
    Msgbox "¡Select the user!"
    Exit Sub
Endif
End Sub
 

isladogs

MVP / VIP
Local time
Today, 18:22
Joined
Jan 14, 2017
Messages
18,216
Do you realise the public variable pbUser is redundant
Just change your GlobalUser sub to a function so it returns a value and set GlobalUser =IDUser in the function
 

Cotswold

Active member
Local time
Today, 18:22
Joined
Dec 31, 2020
Messages
528
Instead of Publics I write variables onto a form that is always open whilst the application is running.
They are then available by referring to them using the Form Name at any point when it is
in use. I've done it now since using Access97 back in 1999 without any issues. One advantage
to me at the time was that by storing them on a form saved memory that I may need later.
Maybe today not so important? If any standing data did happen to change I can just update
the variable on the Form. The user has no access to the variables on the form. Works well for me.
 

Teggun

Registered User.
Local time
Today, 19:22
Joined
Aug 30, 2019
Messages
33
Do you realise the public variable pbUser is redundant
Just change your GlobalUser sub to a function so it returns a value and set GlobalUser =IDUser in the function
Maybe I'm wrong but if I do it like so, wouldn't I need 2 functions? One to set up the variable and an other one to call it?
I need the variable to stay set up while the user is logged in.
 

isladogs

MVP / VIP
Local time
Today, 18:22
Joined
Jan 14, 2017
Messages
18,216
The way your code is written you don't make any use of the pbUser variable. It may as well be deleted.
Just call the GlobalUser FUNCTION in frmLogin and elsewhere as needed.

OR set pbUser = cmbUsers.Column(0) in form frmLogin.

BTW if the first Column is bound in the combo you can also delete the .Column(0) part of that code
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 18:22
Joined
Sep 12, 2006
Messages
15,653
As you can see, there are lots of alternatives. You could also save the login information in a TempVars, which is easier to use in many ways than a public variable. You could create a class module for the login information. Over time, you will pick up lots of style alternatives, and decide on your own preferences.
 

Users who are viewing this thread

Top Bottom