Passing Arguments

J

Jerry Bodoff

Hi,

This is not in reference to any problem but a general
information question.

Other than using a PUBLIC variable in a module is there
any other way to pass an argument(s) from one form to
another?

Just curious.

Jerry B.
 
J

Jezebel

There are much better ways. Avoid globals as much as possible (which should
be almostentirely). There are basically two approaches according to whether
you need to pass the values actively - form1 delivers values to some other
function, or simply make them available - form1 owns the values, which other
functions might need to retrieve.

Some options --

1. The form can expose properties, the same as a class module. You can add
code to the form like

Public Property Get MyProp() as whatever
....

2. The form can have subs and functions that take arguments.

myForm.MyFunc Arg1, Arg2, ....

3. You can reference the form's controls, if you just want to get the
content of (eg) a textbox on the form.

x = myForm.TextBox1
 
J

Jerry Bodoff

Hi Jezebel,

I think that there is a mis-understanding as to what I am
asking, but I am not sure. What I am really asking is:

form1
form2.load
form2.show arg1 arg2 arg3
or
form1
form2.load arg1 arg2 arg3
form2.show

or something like the above. The criteria is that a user
enters data in form1 and form2 is modified depending on
that data. I hope that this is a little clearer than my
original question.

If I understand your reply, are you saying that I should
set up some form properties in a class module or just
access form1's controls in form2?

Also, what is the reasoning behind avoiding globals?

Thanks for your reply

Jerry B.
 
P

Peter Hewett

Hi Jerry Bodoff

There's no mis-understanding. Jezebel's absolutely correct. You can't use:
form2.show arg1 arg2 arg3
or
form2.load arg1 arg2 arg3

Neither accept arguments and the second syntax is invalid.

You have to use Properties or Methods in your Form. If you want to read/write values then
uses Properties if you just want to pass in a whole bunch of information use a Method (a
Public Sub declared within your Form). This is the correct object oriented approach to
the problem.

Here's an example of both properties and a method in action, this code is part of the
Form:

Private mdateFirst As Date
Private mdateLast As Date
Private mstrTitle As String
Private mstrFirstName As String
Private mstrMiddleName As String
Private mstrLastName As String

Public Property Get FirstDate() As Date
FirstDate = mdateFirst
End Property
Public Property Let FirstDate(ByVal FirstDate As Date)
mdateFirst = FirstDate
End Property

Public Property Get LastDate() As Date
LastDate = mdateLast
End Property
Public Property Let LastDate(ByVal LastDate As Date)
mdateLast = LastDate
End Property

Public Sub NameInfo(ByVal Title As String, _
ByVal FirstName As String, _
ByVal MiddleName As String, _
ByVal LastName As String)
mstrTitle = Title
mstrFirstName = FirstName
mstrMiddleName = MiddleName
mstrLastName = LastName
End Sub

You would instantiate the form and set the properties like this:
Public Sub ShowFormTheTechnicallyCorrectWay()
Dim frmT As frmTest

Set frmT = New frmTest
Load frmT
frmT.FirstDate = "1/1/04"
frmT.LastDate = "31/12/04"
frmT.NameInfo "Mr", "Joe", "Blow", "Doe"
frmT.Show
MsgBox "Form complete"
Unload frmT
Set frmT = Nothing
End Sub

HTH + Cheers - Peter


as neither
 
J

Jerry Bodoff

Hi Peter,

Thanks for your reply. I now understand what Jezebel was
talking about. I thought that Property statements could
only be in a class module. I now know that they can also
be defined in a form.

The other question was whether Jezebel was saying not to
use Global variables at all. At least I read it that
way. I still do not understand why not?

Once again, thanks for your reply.

Jerry B.
-----Original Message-----
Hi Jerry Bodoff

There's no mis-understanding. Jezebel's absolutely correct. You can't use:
form2.show arg1 arg2 arg3
or
form2.load arg1 arg2 arg3

Neither accept arguments and the second syntax is invalid.

You have to use Properties or Methods in your Form. If
you want to read/write values then
uses Properties if you just want to pass in a whole
bunch of information use a Method (a
Public Sub declared within your Form). This is the
correct object oriented approach to
the problem.

Here's an example of both properties and a method in
action, this code is part of the
 
P

Peter Hewett

Hi Jerry Bodoff

Sometimes you just *have* to use global variables but they should be kept to an absolute
minimum. The problem with globals is that you can't tell when or where they change and
that can lead to hard to find bugs. With good careful design you can probably eliminate
most globals from your project. Don't be afraid of passing the value as a parameter to
Subs or Functions, it makes the code clear about what's being used and where.

BTW a user Form is just a Class module with a user interface that's why you can use
Properties and methods with one.

HTH + Cheers - Peter
 
J

Jezebel

It's worth noting that in many respects a form object IS a class module. It
differs in that a form also has a GUI (ie visible) component; but apart from
that it behaves in essentially the same way:

- You can create multiple instances of the form object:

Dim pForm1 as frmMyForm
Dim pForm2 as frmMyForm

set pForm1 = new frmMyForm
set pForm2 = new frmMyForm

- The form can have user-defined properties and events.
 
J

Jerry Bodoff

Hi Jezebel and Peter,

Thanks for your replies. I learned quite a bit from such
a simple question.

Once again thanks.

Jerry B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top