Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Data into a form

Status
Not open for further replies.

EscapeUK

Programmer
Jul 7, 2000
438
0
0
GB
I would like to pass in some data to a form when it is called. Long story but i don't want to create classes or functions.

Someone mentioned public methods in the form.

Could any help, please keep instructions simple.
 
Create a public variable in the general declarations of the form (Public strData as String). Then from the calling location, load the form in the following manner:

Load Form1
Form1.strData = "Hello World"
Form1.show

Then in the form load

Msgbox strData

You will get the message!

Madlarry
 
Public variables will certainly work, but are a poor programming practice, as you will have no control over values assigned to the variable, nor will you know when a value is assigned. I NEVER use public variables for these reasons. Add a new Property to the form to pass values. This will allow you to validate the incoming value, as well as execute other code inside the form when a new value is passed. Trust me, as your program grows and becomes more complex, little things like this become more important. Program defensively!
 
1. On the form that you want to call (let's call it MyForm), create a label, and name it lblParameter. Set the lblParameter.Visible property to false.

2. Place the following statement in the code that will call the form:

MyForm.lblParameter.Tag = "ValueToPass"

This will (a) place the string "ValueToPass" into MyForm.lblParameter.Tag, and (b) automatically load/show the form. Within the form's code, you can then use the value in lblParameter.Tag for whatever purpose you wish.

Hope this helps -- WB


 
Hexonx could you give me a coded example.

 
'Hexonx sample code maybe something like this

'Paste into declarations section of MyForm...

Private lngParameter as Long

Public Property Let Parameter(lngValue as Long)

'Some validation
If lngValue > 0 then
lngParameter = varValue
else
'Raise an error or something
end if

End Property

Public Property Get Parameter() as Long

Parameter = lngParameter

End Property


'to set value of parameter from elsewhere in the project

MyForm.Parameter = 1234


'to read value of parameter

Debug.print MyForm.Parameter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top