Connecting Tech Pros Worldwide Forums | Help | Site Map

Global Variables (setting and using)

Member
 
Join Date: Mar 2009
Location: Conroe, TX
Posts: 57
#1: Mar 12 '09
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
Expand|Select|Wrap|Line Numbers
  1. Public ShippingType as Integer
Then i set a value to it with buttons on a popup form like this
Expand|Select|Wrap|Line Numbers
  1. Private Sub customer_Click()
  2. DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd
  3. ShippingType = 1
  4. DoCmd.Close acForm, "frmshiptype"
  5. End Sub
  6.  
  7. Private Sub transfer_Click()
  8. DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd
  9. ShippingType = 2
  10. DoCmd.Close acForm, "frmshiptype"
  11. End Sub
  12.  
  13. Private Sub vendor_Click()
  14. DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd
  15. ShippingType = 3
  16. DoCmd.Close acForm, "frmshiptype"
  17. End Sub
  18.  
Then on the form "frmshipping" i placed in the OnCurrent Event
Expand|Select|Wrap|Line Numbers
  1. If ShippingType = 1 then
  2. blah blah blah
  3. end if
  4.  
  5. if ShippingType = 2 then 
  6. blah blah blah
  7. end if
  8.  
  9. if ShippingType = 3 then
  10. blah blah blah
  11. end if
Then when it wasnt working properly i added this before the if/thens
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. ShippingType = 1
  2. 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

Member
 
Join Date: Mar 2009
Location: Conroe, TX
Posts: 57
#2: Mar 12 '09

re: Global Variables (setting and using)


Nevermind, i cuaght it when reading my post.

I changed
Expand|Select|Wrap|Line Numbers
  1. Private Sub customer_Click() 
  2. DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd 
  3. ShippingType = 1 
  4. DoCmd.Close acForm, "frmshiptype" 
  5. End Sub 
  6.  
to this
Expand|Select|Wrap|Line Numbers
  1. Private Sub customer_Click() 
  2. ShippingType = 1
  3. DoCmd.OpenForm "frmshipping", acNormal, , , acFormAdd 
  4. DoCmd.Close acForm, "frmshiptype" 
  5. End Sub 
I cant beleive I missed that.....
Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#3: Mar 12 '09

re: Global Variables (setting and using)


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:

Quote:

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.

Expand|Select|Wrap|Line Numbers
  1. Sub OpenToCallahan()
  2.     DoCmd.OpenForm "Employees", acNormal, , , acReadOnly, _
  3.      , "Callahan"
  4. End Sub
  5.  
  6. Sub Form_Open(Cancel As Integer)
  7.     Dim strEmployeeName As String
  8.     ' If OpenArgs property contains employee name, find
  9.     ' corresponding employee record and display it on form. For
  10.     ' example,if the OpenArgs property contains "Callahan",
  11.     ' move to first "Callahan" record.
  12.     strEmployeeName = Forms!Employees.OpenArgs
  13.     If Len(strEmployeeName) > 0 Then
  14.         DoCmd.GoToControl "LastName"
  15.         DoCmd.FindRecord strEmployeeName, , True, , True, , True
  16.     End If
  17. End Sub

-Stewart
Reply