How do you take a value from a form and assign it to a global variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramprat
    New Member
    • Oct 2008
    • 60

    How do you take a value from a form and assign it to a global variable

    Hi Everyone,

    I have this code on a login form. I wish to flag the username and group of all users and enter those values in a table whenever someone modifies something in the table through another form. I can't seem to be able to take the value of g_username and g_group and use them elsewhere in my database. I've tried refering to them simply by their variable names and also tried including the name of the form in front of the variable eg login_form.g_gr oup. I don't get errors I just don't get any results when I've tried inserting the value of g_username into a field in the table based on the before update event of the 2nd form (the one with the underlying table). I thought this was going to be a 10 minute job but I guess I was wrong. Any ideas would be appreciated. Thanks
    Ramprat

    The first two lines are in the declarations section at the top of the code on the login form.

    Public g_username As String
    Public g_group As String


    Private Sub Login_Go_Button _Click()

    Forms!login_for m.Refresh
    g_username = Me.Username
    g_group = Me.Group
    Forms!Count_Sta tions_Form.Visi ble = True

    End Sub
  • Krandor
    New Member
    • Aug 2008
    • 50

    #2
    Your global variables lose scope as soon as you go to a different form. They are "global" only to your login form.

    You need to create a module and remove the global declarations from your login screen and move them there. Once you do that, your variables will be available to the whole application.

    Comment

    • ramprat
      New Member
      • Oct 2008
      • 60

      #3
      How do you take a value from a form and assign it to a global variable

      Thanks,
      So are you saying that if I took the 2 lines below and created a new module and pasted them in and called it module 1 for example, the rest of my code should work fine the way it is?

      Public g_username As String
      Public g_group As String

      Comment

      • Krandor
        New Member
        • Aug 2008
        • 50

        #4
        Originally posted by ramprat
        Thanks,
        So are you saying that if I took the 2 lines below and created a new module and pasted them in and called it module 1 for example, the rest of my code should work fine the way it is?

        Public g_username As String
        Public g_group As String
        Yes. Exactly. Anything that you want to be global to the whole app (including functions and subs) has to be located in a module.

        Comment

        • ramprat
          New Member
          • Oct 2008
          • 60

          #5
          thanks guys I got it to work!

          Comment

          Working...