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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you pass variables to a Form_Load event procedure? 2

Status
Not open for further replies.

perplexd

Programmer
May 9, 2002
154
0
0
US
Is it possible to pass parameters to a form_load event, or any other VB-generated event? eg is it possible to do:
Private Sub Form_Load(var1 As Long, var2 As String)

And then in the calling procedure do something like:
Load Form1(FirstVariable, SecondVariable)

Any insight is appreciated. Thanks
 
No, not without subclassing to intercept the event.
However if you are looking for a way to input parameters to you program (from DOS promt or shortcut string), use the Command$ string:

Private Sub Form_Load()
msgbox "Your input parameter(s): " & command$
end sub

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Another common approach is to place hte args in the "Tag" property of the form during the load event / process. And Yet ANother Process is to just use Global Vars and have the form load event check / use these. MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Yes, I'm using Global variables at the moment.

What are hte arguments? (I'm new!)
 
"hte arguments" is a typo and should be "the arguments". If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
Is the the first form or some subform? If it's a subform you can put a procedure in the subform that has variables passed to it and have that form show itself.
 
Is the the startup form or some subform? If it's a subform you can put a procedure in the subform that has variables passed to it and have that form show itself.
 
Form1 is a standard form. I'm now loading form2 up and then calling a proceedure from form2 and passing the variables through that.
 
Aside from the solutions that MichaelRed & jlschulte offers, you can make a new property of form2 and use that to pass the variable. It corresponds the solution offered by jlschulte, exept that properties are designed to do extactly that.
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
That sounds like a good way. How do you add a property? Is this a run-time or design time activity? Could you please post the code or method through which you can add a property to a form, Sunaj?

Thanks
 
You add a property to a form in exactly the same way as you do to a class; a VB form is, with a few idiosyncrancies, just a class with a visual interface...

 
Here is an example:

Form1:
------------------------------------------------------------
Private Sub Command1_Click()
Form2.PassVar = "hello"
Form2.Show
End Sub
------------------------------------------------------------

Form2:
------------------------------------------------------------
Dim Form2Var As String
Private Sub Form_Load()
MsgBox Form2Var
End Sub

Public Property Let PassVar(VarValue As String)
Form2Var = VarValue
End Property
------------------------------------------------------------

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
And if I may add yet another way:
Create a method in the form called something like:

Public sub Display(Value1 as SomeType)
m_Value1 =Value1
'or
If Value1 = XX then
'Some Code
End if
me.Show
end sub

Open the form now like this:

Dim f as New Form1
f.Display(PassSomeValue)

I do it either this way if feasible, or I do it the way sunaj suggested.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top