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 call a public function

Status
Not open for further replies.

BuilderSpec

Programmer
Dec 24, 2003
383
GB
Hi

I have a form called Form1 , on Form1 I have a subform called Child7. Child7 shows as source object a form called "Form2". In Form2's VBA section I have a public function called testit().

What is the syntax to call testit() from Form1 ?
I have tried

call forms("Form2").testit()
call forms("Form1").child7.testit()

None seem to work but sure there must be a way. can someone point me right please ?






Hope this helps!

Regards

BuilderSpec
 

Did you try just:
[tt]
Call Form2.testit()[/tt]

You may want to move your function to a standard Module - since it is a Public Function - and just call it this way:
[tt]
Call testit()[/tt]

Have fun.

---- Andy
 
No

I want to have the same function in several forms so the function needs to be defined at form level not global.

I want to make the subform go to a specific record so I have made the standard method public.

Public Sub GotoComplianceID(ID As Long)
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[ID] = " & str(ID)
If Not rs.EOF Then Me.Bookmark = rs.Bookmark

End Sub

I have this repeated in 4 subforms. On my main form I want to dynamically select which form to display then call this function with a record ID to go to.



Hope this helps!

Regards

BuilderSpec
 
In Access all open forms are added to the Forms collection. Andy's solution works in vb but not in Access because in Access only open forms are added to the forms collection not all forms. Also forms opened as subforms are not added to the forms collection. To call code on a subform you have to reference the subform through the main form or use the form class not the form instance.
assume i have a form called
frmName
a subform control called
subFrmControlName
and a subform called subFrmName

So the following work
forms("frmName").subfrmControlName.form.testIt
Form_subFrmName.TestIt

BTW you should do as Andy says and put in a standard module.
 
In a standard module
Code:
Public Sub GotoComplianceID(ID As Long, subFrm as access.form)
    subfrm.recordset.findfirst "[ID] = " ID
End Sub
to call from a main form with a subform control called "subFrmControl"

gotoComplianceID(me.IDFieldName,me.subFrmControl.form)
 
What about this ?
Call Forms("Form1")!Child7.Form.GotoComplianceID(someIDhere)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Brilliant . The

Form_subFrmName.TestIt works. never knew you could do that.



Hope this helps!

Regards

BuilderSpec
 
The issue was never about whether it was in a standard module or not. Either way I would still have to reference the same function I wanted to call in the subform, that was the syntax I was unsure of.

Problem solved now thanks all.



Hope this helps!

Regards

BuilderSpec
 
No. My point is that the code does not have to go into any subform. If you look at my code you can move any form/subform that has the ID field. Even forms you have not built yet. You only have to code in one location. Think about maintainability.
 

I want to have the same function in several forms

Bad idea.

You will end up with n functions that are the same, so when you need to change them, you wil hit (most likely) n - 1 functions.

Listen to MajP's (and mine) suggestion and do it in the Module. You can pass anything you want into this ONE function, even the recordset.

Have fun.

---- Andy
 
I JUST WANTED TO KNOW HOW TO REFERENCE THE FUNCTION IN THE SUBFORM!!!

Hope this helps!

Regards

BuilderSpec
 
Sorry,
No need to yell. Most people are interested in learning how to write better code and properly design their applications. If you are satisfied with your poor design, and not interested in learning how to do it correctly that is fine. But do not get updset with someone trying to help you code better.
 
Most people are interested in learning how to write better code"

I was interested in knowing how to reference the function, for it's own sake not for my application. I did it another way in the end but it baffled me that I couldn't work out how to reference it.

Your own comment about referencing it from the class not the instance was great I didn't know that and I appreciate it.



Hope this helps!

Regards

BuilderSpec
 
Ok this may sound trivial, but you are still much safer referring to the specific instance of the subform and not the subform class. Most people will not see the difference, but in big applications you could cause some serious problems

This calls a procedure in a specific instance of the subform.
forms("MainForm").subfrmControlName.form.procedureInSubform

This calls the same procedure in the class and in fact only works on the first opened instance
Form_subFrmEmployees.procedureInSubform

You are clearly referring to a specific instance because you want to move records in the subform instance of the main form.

If you start working with multiple form instances the latter will cause unexpected and seemingly random problems. But here is a simple test.
1) Open the subform as a standalone form.
2) Open the Main form with the subform in it.
3) Call the method using the class. The record is moved in the standalone main form instance and not the subform instance.
4) call the method by using the reference through the subform control. The subform moves as desired.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top