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!

Pass Arguments to a Form 2

Status
Not open for further replies.

figler

Programmer
Dec 26, 2001
155
0
0
US
Is it possible to pass arguments to a form (other than the optionval vb arguments "style" and "ownerform")? If not, any workarounds? I want to open a form and load it with data for a particular customer. So it would be great if I could write:

CustForm.Show 1234

where "1234" is the customer ID and then in the OnLoad event I can use that ID to populate the form. help much appreciated. thanks! -brad
 
Like any other object, you can add your own properties and methods to forms that easily solve problems like that:

'in CustForm:
Private m_CustID As String

Property Let CustomerID(ByVal NewCustID As String)
m_CustID = NewCustID
End Property
Property Get CustomerID() As String
CustomerID = m_CustID
End Property

Form_Load()
'Do your loading stuff using m_CustID
End Sub

'From anywhere else
CustForm.CustomerID = "1234"
CustForm.Show

HTH!

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Yah but doesn't the form_load procedure run the first time you reference a property of the form? I have to pass those values BEFORE i run form_load.
 
Form_Load didn't run on my machine while setting CustomerID. I think it fires if you reference an intrinsic property of the form, like BackColor or Width. I've used this same technique successfully in my own projects, setting my custom properties and letting the Show method call Load when everything's set up. Put a debug statement in CustForm_Load and see for yerself that it doesn't load until the Show method is called. : )

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
I don't think it possible to do this before you load the form, because it doesn't 'exist' at that time.

But what you suggested in your first post:
CustForm.Show 1234
is no problem to accomplish, you just add a Sub to your form like, let's say, Do_Show(Data as Integer)
which does everything that needs to be done before showing the form and at the end just show it.

ex:
Public Sub Do_Show(Data as Integer)
Set m_iNeededData
Me.show
End Sub

Regards
Johpje
 
Paste this into a blank form1
[tt]
Public mLabelTxt As String
Public mTxtDeflt As String

Private Sub Form_Load()
Debug.Print mLabelTxt, mTxtDeflt
End Sub
[/tt]
paste this into a blank module1
[tt]
Sub Main()
Form1.mLabelTxt = "Hello"
Form1.mTxtDeflt = "Werlt"
Form1.Show
End Sub
[/tt]
Set project startup to Sub Main then Press F5.
Click the close box.
See the Immediate window...
It contains:
[tt]
Hello Werlt
[/tt]


Wil Mead
wmead@optonline.net

 
Great suggestions. Thanks everyone for the help. Johpje, your solution is perfect -- wish I had thought of it :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top