Global variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KingKen
    New Member
    • Feb 2008
    • 68

    Global variable

    I have the following logon modules which I used to direct users with varying priviledges to log on to the DB. I want to be able to populate my support log form with the user ID once the user logs on and open that form. I am thinking about putting in a global variable that would capture the UserID and set to the supportlogs form userID field to this value. I have never used global variables before... any help would be good

    LOgon module
    Code:
    Public Sub login()
    
    Dim varLog, varPw, varGrp As Variant
    varLog = DLookup("[userId]", "users", "[logonid]=Forms!login!loginid")
    varPw = DLookup("[userId]", "users", "[password]=Forms!login!password")
    
    If varLog = varPw Then
        MsgBox ("Login id confirm")
        varGrp = DLookup("[groupid]", "users", "[password]=Forms!login!password")
    Else
        MsgBox ("Invalid LoginID, Try again")
        loginid.SetFocus
    End If
    
    
    Select Case varGrp
    Case 1
      DoCmd.Close acForm, "login"
      DoCmd.OpenForm "mainmenu"
    Case 2
      DoCmd.Close acForm, "login"
      DoCmd.OpenForm "mangermenu"
    Case 3
      DoCmd.Close acForm, "login"
      DoCmd.OpenForm "dataentrymenu"
    End Select
    
    End Sub
    SupportlogForm onCurrent event
    Code:
    Private Sub Form_Current()
    Dim VarLogon As Variant
    VarLogon = userID 'this is the global variable I want to set
    With Me.SupportStaff
    If Nz(.Value, False) Then
       Me.SupportStaff.Value = VarLogon
    End Sub
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    Simply create a new code module. The public variables declared here will be persistent outside your form code.

    Then after
    Code:
    Option Compare Database
    Option Explicit
    use something like
    Code:
    Public userID as String
    Note that if you stop the running of code or have an unhandled error, the stored values should be wiped from memory.

    Comment

    • KingKen
      New Member
      • Feb 2008
      • 68

      #3
      thanks much this idea worked just fine

      Comment

      Working...