View Full Version : Can't call a sub from another form.


suepowell
06-10-2008, 08:06 AM
HI,

I have a problem calling a subprocedure.

I have a sub in the module of a form that I have declared as public.

Public Sub BOMForm_AfterUpdate()
lots of code
end sub

If I call it from within the same form module it's fine, but if I call it from another form, it give the error "Sub or Function not defined"

I have checked the obvious like spelling (I cut and pasted the sub name so there can be no typo's)

I am calling it using the syntax

Call BOMForm_AfterUpdate

Is this possible.

I don't want to put it in a separate module as there will be lots of rewriting and replacing "me" with long definitions including form names.

Thanks for your thoughts.

Sue

RuralGuy
06-10-2008, 08:15 AM
A Public Sub in a Form's class module becomes a *method* of the form. Try calling it that way through the Forms collection. Forms.BOMForm.BOMForm_AfterUpdate

Pat Hartman
06-10-2008, 07:54 PM
Using a sub in a different form does not work this way. "Me" refers to the form which contains the code. It will not operate on the second form. Move the code that you want to reuse to a standalone module and replace "Me" with "frm". Pass in the name of the calling form:

Call CommonSub(Me)


Public CommonSub(frm as Form)
'''''your code with "me" replaced by "frm"
End Sub

suepowell
06-12-2008, 01:09 AM
Thanks for your replies, it does explain why I can't get it to work.

I have worked out another method of achieving what I want in this case, but it is very useful to know why I couldn't get it to work.

I'll know whats possible next time I want to do something similar.

Thanks

Sue

boblarson
06-12-2008, 05:38 AM
I have found that using

Form_BOMForm_AfterUpdate

works.

Pat Hartman
06-12-2008, 08:30 PM
Yes it does. But what does "me" refer to in that context?