Troubles with global variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobH
    New Member
    • Jul 2007
    • 56

    Troubles with global variables

    I know its probably been asked a million times - however I can't seem to get any examples to work..

    In Module1 I have a line: Global gvarMyEmpID As Integer

    I then have an opening splash page for the logon process that has a drop down of the employees and an Enter Button

    The Enter Button has some code that includes....
    gvarMyEmpID = Me!AgentID.Valu e
    SQLtxt = "UPDATE [Version Control] SET [Version Control].[Last Agent] = " & gvarMyEmpID & ";"
    DoCmd.RunSQL SQLtxt

    I then have the Opening Splash Page close and then the Menu appears - With a text box that has the gvarMyEmpID in the control source however all I get when I run the screen in Form View is "#Name?" in the box.

    How can I use this variable in textboxes, Queries - in rowsources, and Event Procedure Code on other pages?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by RobH
    I know its probably been asked a million times - however I can't seem to get any examples to work..

    In Module1 I have a line: Global gvarMyEmpID As Integer

    I then have an opening splash page for the logon process that has a drop down of the employees and an Enter Button

    The Enter Button has some code that includes....
    gvarMyEmpID = Me!AgentID.Valu e
    SQLtxt = "UPDATE [Version Control] SET [Version Control].[Last Agent] = " & gvarMyEmpID & ";"
    DoCmd.RunSQL SQLtxt

    I then have the Opening Splash Page close and then the Menu appears - With a text box that has the gvarMyEmpID in the control source however all I get when I run the screen in Form View is "#Name?" in the box.

    How can I use this variable in textboxes, Queries - in rowsources, and Event Procedure Code on other pages?
    1. The Keyword Global is only used for backward compatability - use Public instead:
      [CODE=vb]Public gvarMyEmpID As Integer[/CODE]
    2. Public Variables cannot be used as a Control Source for a Control.
    3. The areas in which they can use used are numerous and range from SQL Statements to code blocks, to Property values, etc.

    Comment

    • RobH
      New Member
      • Jul 2007
      • 56

      #3
      Originally posted by ADezii
      1. The Keyword Global is only used for backward compatability - use Public instead:
        [CODE=vb]Public gvarMyEmpID As Integer[/CODE]
      2. Public Variables cannot be used as a Control Source for a Control.
      3. The areas in which they can use used are numerous and range from SQL Statements to code blocks, to Property values, etc.

      I tried that and then on the Menu page entered the following on the "On Load"

      Me!Text22.value = gvarMyEmpID

      And all i got was a 0 when it should have been a 2 - bing the ID of the staff member I logged in with.

      Do I have to call the Module somehow??

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Originally posted by RobH
        I tried that and then on the Menu page entered the following on the "On Load"

        Me!Text22.value = gvarMyEmpID

        And all i got was a 0 when it should have been a 2 - bing the ID of the staff member I logged in with.

        Do I have to call the Module somehow??
        Hi, RobH.

        I've tried an example and everything works fine with the following.

        In public module "Module1"
        Code:
        Public intVar As Integer
        in form module
        Code:
        Private Sub Form_Load()
            Me.Text0 = intVar
        End Sub
        
        Private Sub Text0_AfterUpdate()
            intVar = Me.Text0
        End Sub

        Comment

        • RobH
          New Member
          • Jul 2007
          • 56

          #5
          Originally posted by FishVal
          Hi, RobH.

          I've tried an example and everything works fine with the following.

          In public module "Module1"
          Code:
          Public intVar As Integer
          in form module
          Code:
          Private Sub Form_Load()
              Me.Text0 = intVar
          End Sub
          
          Private Sub Text0_AfterUpdate()
              intVar = Me.Text0
          End Sub

          Ok I only have 1 module in modules and I have changed to as you have indicated..

          In the Logon form 'Logon Button' I have

          Code:
                  intVar = Me.AgentID
                  SQLtxt = "UPDATE [Version Control] SET [Version Control].[Last Agent] = " & intVar & ";"
                  DoCmd.RunSQL SQLtxt
          Now this does update the Vesion Control tbl correctly.. so the variable is working with-in the logon form..

          In the Menu Form I have a text box which should display the ID no..
          Code:
            Private Sub Form_Current()
              Me.Text22 = intVar
            End Sub
          I have tried on this form both 'Private Sub Form_Load()' and 'Private Sub Text0_AfterUpda te()'

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by RobH
            Ok I only have 1 module in modules and I have changed to as you have indicated..

            In the Logon form 'Logon Button' I have

            Code:
                    intVar = Me.AgentID
                    SQLtxt = "UPDATE [Version Control] SET [Version Control].[Last Agent] = " & intVar & ";"
                    DoCmd.RunSQL SQLtxt
            Now this does update the Vesion Control tbl correctly.. so the variable is working with-in the logon form..

            In the Menu Form I have a text box which should display the ID no..
            Code:
              Private Sub Form_Current()
                Me.Text22 = intVar
              End Sub
            I have tried on this form both 'Private Sub Form_Load()' and 'Private Sub Text0_AfterUpda te()'
            It seems that you are experiencing some very peculiar behavior with your Public Variable. Try changing the Declaration to:
            [CODE=vb]Public intVar As Variant[/CODE]
            NOTE: Let me know how you make out.

            Comment

            • RobH
              New Member
              • Jul 2007
              • 56

              #7
              Actually - I found the problem ...

              I had further down in the coding another IF statement - That was allocating the variable properly..

              It was while I was exporting the required modules to a seperate database so i could attach it that i can accross it.

              Thanks Guys.

              Hey If I want to store the Value of a Yes/No in a variable should I use Variant or String?

              Eg I have tbl-UserSecurity which has a field "Admin" being yes/no format.

              If I want to Dlookup that value into a public variable so that i can use it for IF statements later. What 'As' should I use in the Public intAdmin as ?????

              Thanks Again - already this has the potential to cut so much text from coding and lead off the database from doing these lookup's all of the time..

              Rob.

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Originally posted by RobH
                Actually - I found the problem ...

                I had further down in the coding another IF statement - That was allocating the variable properly..

                It was while I was exporting the required modules to a seperate database so i could attach it that i can accross it.

                Thanks Guys.

                Hey If I want to store the Value of a Yes/No in a variable should I use Variant or String?

                Eg I have tbl-UserSecurity which has a field "Admin" being yes/no format.

                If I want to Dlookup that value into a public variable so that i can use it for IF statements later. What 'As' should I use in the Public intAdmin as ?????

                Thanks Again - already this has the potential to cut so much text from coding and lead off the database from doing these lookup's all of the time..

                Rob.
                Hey If I want to store the Value of a Yes/No in a variable should I use Variant or String?
                The answer is neither. The value would be stored in a Boolean Type Variable, as in:

                [CODE=vb]'Public/Global Declaration
                Public blnAdmin As Boolean[/CODE]
                [CODE=vb]'Initialization
                blnAdmin = True[/CODE]
                [CODE=vb]
                'Evaluates to True and 'blnAdmin = True' appears in the Message Box
                If blnAdmin Then
                MsgBox "blnAdmin = True"
                Else
                MsgBox "blnAdmin = False"
                End If
                [/CODE]

                Comment

                • RobH
                  New Member
                  • Jul 2007
                  • 56

                  #9
                  Originally posted by ADezii
                  The answer is neither. The value would be stored in a Boolean Type Variable, as in:

                  [CODE=vb]'Public/Global Declaration
                  Public blnAdmin As Boolean[/CODE]
                  [CODE=vb]'Initialization
                  blnAdmin = True[/CODE]
                  [CODE=vb]
                  'Evaluates to True and 'blnAdmin = True' appears in the Message Box
                  If blnAdmin Then
                  MsgBox "blnAdmin = True"
                  Else
                  MsgBox "blnAdmin = False"
                  End If
                  [/CODE]


                  If I want to store the colour for Fore Colour ie #887433 so that i can use it to effectively make a template for colours across the app would I use String?

                  I've tried
                  Code:
                  Public intBlue as String
                  then in the form On Open

                  Code:
                      Me!Label1.ForeColor = intBlue
                  But I get a "Type Mismatch' error when the page loads.

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    Originally posted by RobH
                    If I want to store the colour for Fore Colour ie #887433 so that i can use it to effectively make a template for colours across the app would I use String?

                    I've tried
                    Code:
                    Public intBlue as String
                    then in the form On Open

                    Code:
                        Me!Label1.ForeColor = intBlue
                    But I get a "Type Mismatch' error when the page loads.
                    Color values as you indicate would have to be LONG Integers - you cannot assign a String Value to a Control's ForeColor Property.

                    Comment

                    • RobH
                      New Member
                      • Jul 2007
                      • 56

                      #11
                      Originally posted by ADezii
                      Color values as you indicate would have to be LONG Integers - you cannot assign a String Value to a Control's ForeColor Property.

                      So how can one find out what can be assigned where?

                      eg What can be assigned to Source Doc?

                      Comment

                      • ADezii
                        Recognized Expert Expert
                        • Apr 2006
                        • 8834

                        #12
                        Originally posted by RobH
                        So how can one find out what can be assigned where?

                        eg What can be assigned to Source Doc?
                        1. A String would be assigned to Source Doc.
                        2. If you are not sure about the Variable Type assignments, check the Help Files. You will typically see information such as: Read/write String, returns an Integer, etc.

                        Comment

                        • RobH
                          New Member
                          • Jul 2007
                          • 56

                          #13
                          Originally posted by ADezii
                          1. A String would be assigned to Source Doc.
                          2. If you are not sure about the Variable Type assignments, check the Help Files. You will typically see information such as: Read/write String, returns an Integer, etc.

                          Is there a simple way of setting up a Template at all - instead of having to copy the same text to all of the On Open in every form?

                          Sorry these are probably stupid questions.

                          Comment

                          • FishVal
                            Recognized Expert Specialist
                            • Jun 2007
                            • 2656

                            #14
                            Originally posted by RobH
                            So how can one find out what can be assigned where?

                            eg What can be assigned to Source Doc?
                            I recommend you to use object browser to view object properties/methods syntax. This also helps to understand object model better.

                            Comment

                            • ADezii
                              Recognized Expert Expert
                              • Apr 2006
                              • 8834

                              #15
                              Originally posted by FishVal
                              I recommend you to use object browser to view object properties/methods syntax. This also helps to understand object model better.
                              Good suggestion, FishVal. The only problem is that the Object Browser can be very intimidating and confusing to a Newbie.

                              Comment

                              Working...