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 Chris Miller 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 a form object to a function 1

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I've created general purpose routines to handle events on a form (i.e. Activate, Current, etc.). So, for each event I'm handling, I set the appropriate property on the property sheet equal to the function I want to call. But, I need to be able to pass the form object to the routine. For example, the On Current event on the property sheet is set to =MyFunction([Me]). MyFunction is declared as "Function MyFunction(frm as form)". When I open the form I get the error "...The object doesn't contain the Automation object Me".

How do I pass the form object to the function MyFunction?

For simplicity sake (long story) I don't want to pass the name of the form as a string. And I would rather not have to declare a form object inside the function and set it equal to Screen.ActiveForm.
 
One way to reference a form in your project through VBA is:
[Forms]![frmYourFormName].Repaint for instance.

Beir bua agus beannacht!
 
Right, but the function "MyFunction" does not know the name of the form. So I have to pass that info to "MyFunction". As I stated, I would rather not have to pass the name of the form as a string.

In my second post I pass it as [System].[ActiveForm]. Is there a more efficient way of doing this?
 
public function someCalledFunction (frm as access.form)
'do something with the form
end function

or

public function someCalledFunction2 (frmName as string)
dim frm as access.form
if not currentProject.allforms(frmName).isloaded then
docmd.openform frmName
end if
set frm = forms(frmName)
'do something with the frm
end function


to call
'assuming function is not returning anything
somecalledFunction forms("myFormName")
or
someCalledFunction2 "MyFormName
 
I understand, but as I stated earlier, I would rather not pass it as a string. And here's the reason. 99% of the Access databases I create consist of a main form (portal), a main edit form (this is the form the users enter their information), a report criteria form (users are allowed to select the report they want and specify how they want the report filtered), and a bunch of lookup tables.

The lookup tables can be opened 1 of 3 ways:
1) Open datasheet view Via the menu bar (ribbon in 2007) and the form that was shown prior to selecting the lookup table is hidden. When the lookup form exits, it unhides the original form.

2) User enters something in a combo box that does not exist. Lookup form pops up (modal) in form view and the user is only allowed to add a new record.

3) User right clicks on the combo box and the appropriate lookup form pops up (modal) so the user can view the definition of the lookup record and modify it if they choose.

Note that only users with the proper rights can perform the above.

There are tons of other stuff my dbs do (i.e. auto download new versions, broadcast messages, etc.) Consequently, after I define the tables, I run a wizard I created that creates the 3 main forms and a form for each lookup table. The wizard also generates all of the code that makes all of these things work together.

I'm currently upgrading my library and wizards for Access 2007 and 2010 and am attempting to do things a little different for my lookup forms (i.e. take advantage of split forms...very buggy in 2007).

So, in order to minimize the amount of code I would have to change in my wizards, I would rather pass the variable as a form object rather than a string.

For example, on the OnCurrent event of the form, I was hoping to set the property to "=MyFunction([Me])". But Access doesn't like "Me". Instead of [Me], I found that this works (as posted above) "=MyFunction([Screen].[ActiveForm])".

I surprised "Me" didn't work.
 
I surprised "Me" didn't work.
Me has a meaning only in the class module.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I would rather not pass it as a string
Sorry, much of thelogic following this statement is incorrect.

1) First I showed how to pass a form as an object. Maybe you missed that, or maybe I was not clear.
Code:
public function someCalledFunction (frm as access.form)
  'do something with the form
end function

So from any where in your project you could pass a form object to this function like this
Code:
 'assume the form is called "someFormName"
 someCalledFunction forms("someFormName")
The "forms" collection returns the form object, and passes it the function

However, if you want to call this function from a form's module and reference that form, you can simply do
Code:
 someCalledFunction Me

Every form has a module and Me only can be used to return a reference to that form.

However, the argument about passing the form name versus the form object does not make sense. If you use the second function I provided to pass a name, you simply would call that function from module form as
Code:
  someCalledFunction2 Me.Name
Or call it anywhere outside of that module with
Code:
  someCalledFunction2 "SomeFormName"

Passing either the form object or passing the name and instantiating a form object makes no difference in the ease and reusability of functions.

Screen.Active form is a sloppy method, and difficult to ensure the correct form is passed. You always want to precisely define the form either by the name or the object.
 
Howdy FancyPrairie . . .

Use VBA instead of the property line and you'll have no problem:
Code:
[blue]Private Sub Form_Current()
   Call MyFunction(Me)
End Sub[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top