PDA

View Full Version : variable between forms



mwork
06-14-2007, 09:21 AM
Hi, I am trying to make a menu with access forms. Basicly what I would like it to do is have the person select like drink on the first form, which would open a second form that has a list of drinks. They would select thier drink and that form would close and the selection they made would go into a listbox on the first form.

So my questions are, is there a way to get the variable from the selection on the second form over to the first form so that it can be put into the listbox. Also, is there a way to make the first form react to the second form closing so that it puts the value in the list box after the second box is closed.

Ebrow
06-14-2007, 12:16 PM
Hi,

I don't understand why you want to pasz the value from one form to another back the the same form.

I created a simple db with your requirement that automatically populates one listbox depending on what you selected on the other.

1. Create two tables, a one-many link (i.e Types in one, items in the other, many items per type)
2. Create first list box with your left table (types)
3. Create a query of the right table (items), critera value of first box on type field
4. Create second list box with your query
5. Set the listbox2.requery under the listbox1 before update event.

If that doesn't make sense here it is:-

6001

Let me know your thoughts.

mwork
06-14-2007, 01:28 PM
That works. Much easier than what I was trying to do. I'll have to look at it and see exactly what you did. Thanks!

icthus123
06-15-2007, 03:58 AM
Btw if you had wanted to pass the variable between forms you would have just declared it publicly e.g. in a seperate module like this.


Public VariableName As Variable Type



You would set it in one form, and then get it again in the next.

mwork
07-04-2007, 01:49 PM
So, I'm trying to do this again. In the module would I just declare the variable? What code would I use to set the variable and get the variable?

Ebrow
07-04-2007, 03:30 PM
Hi.

Just as Itchus was saying.

Above any sub procedures or functions type the following code:

<VBA> Public myVariableName as myType <VBA>

replace the myVariableName with the name of your variable
replace the myType with the type of variable you want to use.

You then give it a value anywhere in your code, and once it is given a value it will be stored for you to use anywhere else in any other form etc. You must not Dim the same variable name anywhere in your code or you will lose the value and will revert back to a private variable.