Calling functions from one form to another form?

A

Aldred@office

Hi,
I am currently asked to develop an Access 2007 application. This is my
first time to develop apps in Access. I have a question about calling
function.

I have many forms which will be calling the same functions or subroutine
which is already in another one. How could I make the form to have the
reference to another so the current working form could call the functions
which is already in another form?

Should I place all the functions in one common module and make all the
forms/reports... etc to have reference to that module and make the call?
Wouldn't it be easier to maintain?

Thanks.
 
M

Marshall Barton

Aldred@office said:
I am currently asked to develop an Access 2007 application. This is my
first time to develop apps in Access. I have a question about calling
function.

I have many forms which will be calling the same functions or subroutine
which is already in another one. How could I make the form to have the
reference to another so the current working form could call the functions
which is already in another form?

Should I place all the functions in one common module and make all the
forms/reports... etc to have reference to that module and make the call?
Wouldn't it be easier to maintain?


Yes, it would be easier to maintain if they were in a
standard (not a form/report) module. Be sure to use
arguments so the procedure can reference any objects it
needs to do the job. Most likely, your current procedures
use Me to refer to the form in which it resides, so you will
need to pass that to the procedures and replace Me with the
form object. For example:

Public Sub MySub(frm As Form)
frm.Section(0).BackColor = vbGreen
. . .
End Sub

and call it from any form when you want to make the detail
section's color green

MySub Me

If the procedures are only manipulating a control on the
calling form, then the control object would be sufficient.

Public Sub Xyz(ctl As Control)
ctl.ForeColor = vbRed
End Sub

Xyz Me.sometextbox

Don't use global variables to pass values or references
around your application.

If you really had a need for one form to call a public
procedure in another form, you would refer to it the same
way you refer to any class's methods.
I.e. classinstance.procedure
e.g. Forms!someform.somesub arg1,arg2
 
A

Aldred@office

Thanks, and I still have questions.

So, Say now I have a common module called Common.
Then I have a form called FormA

In Common, I have a public sub called FetchA()

So now in FormA, I do not need to declare anything and just call directly
Common.FetchA()?

If I have many such functions I need to call, can I declare it ahead so in
FormA like using Common, then I could just call the function FetchA()?

Thank you so much again.
 
J

John W. Vinson

Should I place all the functions in one common module and make all the
forms/reports... etc to have reference to that module and make the call?

Access has Modules within the database structure. Put your shared code in a
module as a Public Sub or Public Function, and then you can call it from
anywhere within the database. It's not necessary to refer to the name of the
module, and in fact the name of the module must be DIFFERENT from the names of
any of the subs or functions within the module.
 
M

Marshall Barton

In Access there are two kinds of modules, "standard" and
"class". Form/report modules are class modules, but you
really do not need to be concerned with the details of using
class modules until you get into more advanced object
programming.

What you want to use for your common procedures is a
standard module. Public procedures in standard modules can
be used in any VBA module in your application by simply
using the procedure's name the same way you would any built
in procedure. In addition, Public Functions in a standard
module can be also be used in control source expressions,
Access queries, and several other contexts.

Unless you have the same procedure name in two different
standard modules, you do not need to use the module name
when calling a public procedure in a standard module. It
doesn't matter where you use them, just use the procedure
name. There is nothing to declare outside the module that
contains the procedure.

All this makes what you want an extremely easy and natural
way to do what you want.

Class modules are a different kind of critter so don't
confuse the two types of modules.
 
M

Mike Painter

Aldred@office said:
Thanks, and I still have questions.

So, Say now I have a common module called Common.
Then I have a form called FormA

In Common, I have a public sub called FetchA()

So now in FormA, I do not need to declare anything and just call
directly Common.FetchA()?

Yes, but what are you fetching?

It could be that there is an easier or better way to do what you want and
your way might be left over from some previous environment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top