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

How to pass objects/controls between procedures? 7

Status
Not open for further replies.

dannydanny

IS-IT--Management
Oct 9, 2002
109
DK
Hi,

I have about 5 forms which have code that is about 90% identical to each other. (Code to hide textboxes, save a record, delete a record etc.) I would like to create a separate class module to centralize this code so any changes I make, I only have to make to the class module and I don`t have to go through all 5 forms to make the change.

However, I don`t know how to pass objects, like textboxes and references to forms, to the class module.

As a test I tried this:
In my forms:
Dim module as MyModule 'declared at the top

private form_open()
Set module = New MyModule

private sub HideTextBoxButton_Click()
Call module.HideTextBox(txtBox2)
end sub


In MyModule class:
public sub HideTextBox(txt as TextBox)
txt.visible = false
end sub


I always get a runtime error "Object is required" when I try to pass txtBox2 to the instance of MyModule (this line: Call module.HideTextBox(txtBox2))


Am I going about this the right way, or am I on the wrong track?

Thanks for any help,
Danny



 
Hi, if 90% is about the same from form to form, try doing a search for the use of Tag Properties in this forum.
That may be a direction you'd like to explore.
 
Danny,

What are you trying to do with the module?

The way this is normally done is to just call the procedure, passing a reference to the form or control.

You might have a proc like this in a generic module:
sub Yaddda (frm as form)
'do whatever
end sub

Then an event on your form would call it like this:
dim frm as form
set frm = me
call yaddda(frm)

(It probably would work to just do call yaddda(me), too.)

But I find the need to pass objects such as forms and controls to come up not-so-often. Usually it is sufficient to pass a value.

Would it be possible to combine some of these forms into one?

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Yes, you can pass Me as a parameter, provided the receiving sub knows that it's a form being passed.

Call Yadda (Me)
Sub Yadda (AForm as Form)

With respect to the Object required error, the first question is which object is failing. It could be that "module" is failing, or "txtBox2" is failing. So the first thing I would do is determine which object is causing the failure.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi all,

Thanks very much for your replies.

pdldavis: I will definitely look into using tags...its something I`ve never touched but I`ve read that they can be useful.

JeremyNYC and CajunCenturion: I tried a test based on JeremyNYC`s example, and the object that keeps failing is in the form module at this line: Call yaddda(me). Since I`m using a Japanese OS, I can`t provide details of the exact error message, but the runtime error code is 13. The error is about the parameter type being inconsistent i.e me is of a type not expected by my generic module. I double checked that my module expects a parameter of type form.

Could it be because I`m using Access 97? Am I missing a component or something?

The reason why I need 5 different forms and can not combine them is that they all have slightly different interfaces with respect to the number of fields, field lengths and some textbox positioning diferences.


Thanks again for your help,
Danny

 
Perhaps Danny, if you could post the actual code that you're using, it might be easier identify the problem.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Here is my exact code.

In the form with a test button:

Dim tst As New testmodule1
Private Sub CommandButton0_Click()

tst.showformname (Me)

End Sub

And here is my test module:
Public Sub showformname(frm As Form)

MsgBox (frm.Name)

End Sub

Thanks CajunCenturion, I appreciate the help
Danny
 
Yeah, get rid of the global module and use the line

call showformname(Me)

instead of

tst.showformname (Me)

and that should do the trick.

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Hi Danny!

Here is another option using the Screen object in your module:

Dim frm As Form

Set frm = Screen.ActiveForm
frm!txtBox.Visible = False (or whatever you want to do)

Or, if this doesn't work try:

Dim FormName As String

FormName = Screen.ActiveForm.Name
Forms(FormName)!txtBox.Visible = False

Alternatively you could pass the names of the control and form as strings and use them like this:

Forms(FormName).Controls(ControlName).Visible = False

hth


Jeff Bridgham
bridgham@purdue.edu
 
JeremyNYC is correct in that you either
Code:
Call showformname (Me)
or
Code:
tst.showformname Me
but not
Code:
tst.showformname (Me)
although the reason why that is correct may not be obvious, as it deals with how parameters are passed and the syntax of the statement. I'll try to explain.

The default parameter passing mechanism in VBA is to pass parameters that are solely variables by reference, but to pass the results of an expression by value.
The syntax of the call statement, using the keyword call, requires that the parameter list be enclosed in parens, just as in calling a function.
Code:
Call ProcName (param1, param2, ...)
However, when not using the call keyword, the param list should not be enclosed in parens
Code:
ProcName param1, param2, ...
If the individual parameter is enclosed in parens, then it will be evaluated as an expression and passed by value, and if not, then it will be passed by reference.
Code:
ProcName param1, (param2)
will result in param1 being passed by reference, and param2 evaluated as an expression and passed by value.
Code:
Call ProcName (param1, param2)
will have both parameters passed by reference (because of the use of the keyword call)
Code:
Call Procname (param1, (param2))
will result in param1 being passed by reference, and param2 evaluated as an expression and passed by value.
Code:
ProcName (param1, param2)
will result in a syntax error because "param1, param2" is not a valid expression.
Code:
ProcName (param1), (param2)
is valid with each parameters treated as an expression and passed by value.

Your specific syntax
Code:
tst.showformname (Me)
causes Me to be evaluated as an expression, because of the parens, with the result (the object's default value) being passed by value, whereas
Code:
tst.showformname Me
will not treat Me as an expression, therefore pass it by reference.

I hope that made some sense.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
CC: excellent clarification.

All that is why I always use Call, so that things are always in parens, so I don;t have to sort out that mess (VBA's not CC's) in my head every time.

But in any case, though the module variable will work, there's no need for it, and it just seems to add a layer between things, which I would try to avoid.

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Thanks Jeremy, now if I can just straighten out the mess in my own head. :)

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi all,

Thanks again for all your responses and insights. My code now works like a charm.
Cheers CC for that explanation about how VBA handles parameters, I definitely learnt something today!

Thanks again,
Danny
 
Jebry,
your code

Forms(FormName).Controls(ControlName).Visible = False

works beautifully for me. I was trying to create a function to process a control, and I was having a difficult time with passing the control. By passing two strings, it became simple.

We have several similar controls on several similar forms. For now, we want these controls to be fully selected when the user enters the control. Later, we may want to do more, or something entirely different. By breaking this into a function, it cost me a little programming time, but it saves me hours of boring cut and paste later.

CajunCenturion and JeremyNYC,
Thanks for the education you guys have provided me yet again. Even though the information you gave didn't help me with my current issues, I'm sure I'll be using it before this year is over. I owe you both (and jebry, and several others) a lot of gratitude for the answers you consistently provide. Keep up the good work, and know that it is appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top