Form Variable into a Global

JBurlison

Registered User.
Local time
Today, 00:45
Joined
Mar 14, 2008
Messages
172
How do i make a Variable that is specific to a form to a global Variable. i want to save the name of the person who logs in (via a login screen that i made) , so that i can stamp some there activity within the database to that name.
 
How do i make a Variable that is specific to a form to a global Variable. i want to save the name of the person who logs in (via a login screen that i made) , so that i can stamp some there activity within the database to that name.
I'm not sure here, but you're probably right. Maybe you can change it to a global variable, and assign it a value when they login. Just put the word "global" in front of the variable in a separate module, so it can be assigned the value when the user logs in. Could this work, I wonder...??
 
how do i assign the module to the login button? i see it there but i cannot add it how do i do it?
 
how do i assign the module to the login button? i see it there but i cannot add it how do i do it?
You don't have to assign a module to the button. If you have a module with this in it:
Code:
global MYVALUE as string
...behind the login button, all you have to do is write this:
Code:
MYVALUE = me."Login"FormControlName
Follow? That will assign a value to the global variable, and it will stay that way until you assign another value to it with another piece of code.
 
global is outdated terminology. It is PUBLIC and you would put it in a Standard module (not a form module) in the general declarations section as:

Public gMyValue As String

But, another way which Pat Hartman (former Access MVP) has suggested in the past, and I've started using, is a hidden form to store the user name in an unbound text box. Then you find yourself safer because sometimes, depending on what your database does, or because of errors, you can find your global variable losing its value.
 
So My Login screen looks Like This right now behind the "ok" button. this form is called "Login Screen"

Code:
Private Sub Ok_Click()

Static LogonAttempts As Integer
Dim SaiCurrentUser As String


SaiCurrentUser = ""

    If Me.Password.Value = DLookup("[Password]", "[User Name]", "[User Name] = '" & Me.User_name & "'") Then
    
        SaiCurrentUser = [User Name]
        DoCmd.Close acForm, "Login Screen", acSaveNo
        DoCmd.OpenForm "Switchboard"
            
    Else
    
        LogonAttempts = LogonAttempts + 1
        
      MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
            "Invalid Entry!"
        Me.Password.SetFocus
        Me.Password.Value = ""
        
         
    
    End If

  If LogonAttempts > 3 Then
        MsgBox "You do not have permission to access this database.Please contact your database administrator."
        Application.Quit

   End If


End Sub

Now i Made a Module with this in it:

Code:
Global SaiCurrentUser As String

i must be doing this wrong have not tried it just wanted to see if it is correct. do i have to put anything else in the module?
 
lol posted at the same time bob,a hidden form, thats a good idea, how do i "hide" a form? this will probably be alot easyer on me becaus im still new to VBA.
 
You go to the database window, right-click on the form and select the checkbox labeled HIDDEN. To be able to work on it, you need to go to Tools > Options > VIEW and select the HIDDEN checkbox to display the hidden objects in the database window. When you are through needing to work on it, go back to the Tools > Options > VIEW and uncheck the Display Hidden.
 
Humm dont see it i should mention that i am working with 07
 
hidden01.png



hidden02.png



hidden03.png



hidden04.png
 
Lol im so thick i needed pictures to do that lol hahaha. So Do i open the Form, or just set the field in it? if i open the form i see it and its not hidden from the UI.

Code:
Private Sub Ok_Click()

Static LogonAttempts As Integer
Dim SaiCurrentUser As String


SaiCurrentUser = ""

    If Me.Password.Value = DLookup("[Password]", "[User Name]", "[User Name] = '" & Me.User_name & "'") Then
    
        SaiCurrentUser = [User Name]
        DoCmd.OpenForm "InfoKeeper"
        Forms![InfoKeeper]![Loginname] = SaiCurrentUser
        DoCmd.Close acForm, "Login Screen", acSaveNo
        DoCmd.OpenForm "Switchboard"
            
    Else
    
        LogonAttempts = LogonAttempts + 1
        
      MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
            "Invalid Entry!"
        Me.Password.SetFocus
        Me.Password.Value = ""
        
         
    
    End If

  If LogonAttempts > 3 Then
        MsgBox "You do not have permission to access this database.Please contact your database administrator."
        Application.Quit

   End If


End Sub

Should i remove the open form command?
 
You can use an AutoExec macro to open it when you start the database. When you set the macro's Action of Open Form there is a property for Hidden. Then for your logon code you need only use:

Forms!YourHiddenFormName.YourTextBoxName = SaiCurrentUser

in the If statement where you have validated the user.
 

Users who are viewing this thread

Back
Top Bottom