Accessing Module functions

  • Thread starter Thread starter Peter Brase
  • Start date Start date
P

Peter Brase

I have a form with a subform. I have a class module
containing properties and methods used by the parent form
and instantiated when the parent form is loaded. I want
to be able to access a method in the class module from a
control on the subform. According to the help file's
discussion of the Module Object, this should be possible.
The help file says:

Dim mdl As Module
Set mdl = Forms!Employees.Module
Once you've returned a reference to a Module object, you
can set or read its properties and apply its methods.

It says quite clearly: "you can apply its methods". But
how? I created the mdl variable, but how exactly do you
reference the method? I have tried all the obvious forms
of Access syntax to run a function or a procedure
(e.g., "Call mdl.MyFunctionName", etc.), but Access
invariably tells me that the "data member or method cannot
be found". Can anyone shed light on this question? I
have the same problem when trying to get or set properties.
Thank you!
 
In order use a function within a module, it must be declare Public, i.e.,

Public MyFunction()

MyFunction = "whatever"

End Function

If you are trying to use a class from within 2 separate forms, the class
must be public, or you must somehow pass the class to the other form. For
example

'Form 1 code

dim clsX as new clsWhatever

form2.SetClass clsX


'Form 2 code

Sub SetClass(Arg As Object)

Dim y as object

Set y = Arg

End Sub
 
Here's how I got it to work:
Where "MyInstance" is the instantiated object of the class
module in the parent form:

Set obj = Forms.Item(ParentFormName).MyInstance
Call obj.MyFunction(param1, param2...)

The trick here was that all this was being invoked by a
command button click on the subform, so it wasn't possible
to pass parameters, which made it a heck of a lot harder.
Also I want all the references to be generic. Thanks for
responding!
 
Back
Top