PDA

View Full Version : Solved: Other than using a "Public" variable....



Kicker
11-01-2005, 10:31 AM
Other than using a public variable, is there any way to conveniently "pass" a value from one form to another? For example:

I am using form-1 for working with a table. Periodically, I need some information not available on the form or table. The data isn't something that can be related to the original form. I have a command button that loads and shows a form-2 which allows me to calculate another piece of information.

I know I can just move the data into a "public" variable and close the form-2
I know I can use form-1.txtbox.value = new value
What I would like to do is create a function to get the data such as:

myData = getData() and have getData() load the form and return the value....

Just a question

xCav8r
11-01-2005, 10:47 AM
Pass it in the OpenArgs parameter of the OpenForm method.

Kicker
11-01-2005, 04:05 PM
Xcav8r

I want to go the other way around.

Form-1 is already open and being used. From it, I open form-2 without any arguements and want to send a piece of information BACK to form-1 which is already open.

xCav8r
11-01-2005, 05:54 PM
It sounds like you've already got a handle on how it can be done. Is putting it into a function where you're having trouble?

Kicker
11-01-2005, 06:06 PM
Actually, yes. I try to stay away from "public" variables as much as possible (just a personal thing) and I don't always have the same "calling" form.

Nothing wrong with what I am doing, just thought there might be a different and/or better way.

chocobochick
11-02-2005, 07:37 AM
If you need the form to reference a value that doesn't need its own control to edit or display, you can always wrap the value in a Private module-level variable and create a user-defined property as the interface.

Let's say Form-1 is set to receive the new data (in this case, a String), and has a button named Button1 that displays the String in a MessageBox. We could make a user-defined property named "Stuff" and code the form module like so:

Option Explicit

Private strStuff as String

Property Let Stuff(s as String)
strStuff = s
End Property

Property Get Stuff() as String
Stuff = strStuff
End Property

Private Sub Button1_Click()
MsgBox Me.Stuff
End Sub


Form-2 has a textbox named TBox1 and a button named Button2, and is set to pass the value of Tbox1 to Form-1 when the button is clicked.

Private Sub Button2_Click()
Forms("Form-1").Stuff = Tbox1.Value
End Sub