Global Variables (setting and using)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kstevens
    New Member
    • Mar 2009
    • 74

    Global Variables (setting and using)

    Ok, the first thing i found out is that i should be using 'Public' instead of 'Global", so i did that. Here is what i tried to do (yes i have read as much as i could before i posted...... i promise). I set my variables in a module (not a class module) like this
    Code:
    Public ShippingType as Integer
    Then i set a value to it with buttons on a popup form like this
    Code:
    Private Sub customer_Click()
    DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd
    ShippingType = 1
    DoCmd.Close acForm, "frmshiptype"
    End Sub
    
    Private Sub transfer_Click()
    DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd
    ShippingType = 2
    DoCmd.Close acForm, "frmshiptype"
    End Sub
    
    Private Sub vendor_Click()
    DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd
    ShippingType = 3
    DoCmd.Close acForm, "frmshiptype"
    End Sub
    Then on the form "frmshippin g" i placed in the OnCurrent Event
    Code:
    If ShippingType = 1 then
    blah blah blah
    end if
    
    if ShippingType = 2 then 
    blah blah blah
    end if
    
    if ShippingType = 3 then
    blah blah blah
    end if
    Then when it wasnt working properly i added this before the if/thens
    Code:
    msgbox (ShippingType)
    Then ( i will say it this way on purpose) when i open the form the second time using Transfer (ShippingType should be equal to 2), after opening it the first time with Customer (ShippingType = 1) then i get a 1 in the msgbox. Every time i do it it gives me the value from the press before. Beleive it or not, while trying to cheat i decided to do this
    Code:
    ShippingType = 1
    ShippingType = 1
    Just to see if delcaring it twice would work...... Well it didnt. Can someone please give me the tip i need to move on. I thought i had global variables figured out because this method actually worked somewhere else (summing numbers from some queries) but it doesnt seem to work here. Any help is appreciated.

    Also, when a global variable is used.... is it the same for all users, or are different users able to set different values to it at the same time.

    KStevens
  • kstevens
    New Member
    • Mar 2009
    • 74

    #2
    Nevermind, i cuaght it when reading my post.

    I changed
    Code:
    Private Sub customer_Click() 
    DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd 
    ShippingType = 1 
    DoCmd.Close acForm, "frmshiptype" 
    End Sub
    to this
    Code:
    Private Sub customer_Click() 
    ShippingType = 1
    DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd 
    DoCmd.Close acForm, "frmshiptype" 
    End Sub
    I cant beleive I missed that.....

    Comment

    • Stewart Ross
      Recognized Expert Moderator Specialist
      • Feb 2008
      • 2545

      #3
      Hi. Well done for catching the error in the sequence of opening the form before you had set the variable concerned.

      Something you may consider in the future - as it would do away with the need to use a global variable to pass a value to a form - is to use the OpenArgs string parameter of the OpenForm method to pass the value instead. This can be tested in the form's On Open event to see what its value is, and do whatever is necessary for the form accordingly. The extract from MS Help below for the OpenArgs method (the one that retrieves the value passed) clarifies how it's done:

      Originally posted by MS Help
      Example
      The following example uses the OpenArgs property to open the Employees form to a specific employee record and demonstrates how the OpenForm method sets the OpenArgs property. You can run this procedure as appropriate— for example, when the AfterUpdate event occurs for a custom dialog box used to enter new information about an employee.

      Code:
      Sub OpenToCallahan()
          DoCmd.OpenForm "Employees", acNormal, , , acReadOnly, _
           , "Callahan"
      End Sub
      
      Sub Form_Open(Cancel As Integer)
          Dim strEmployeeName As String
          ' If OpenArgs property contains employee name, find
          ' corresponding employee record and display it on form. For
          ' example,if the OpenArgs property contains "Callahan",
          ' move to first "Callahan" record.
          strEmployeeName = Forms!Employees.OpenArgs
          If Len(strEmployeeName) > 0 Then
              DoCmd.GoToControl "LastName"
              DoCmd.FindRecord strEmployeeName, , True, , True, , True
          End If
      End Sub
      -Stewart

      Comment

      Working...