views:

3686

answers:

3

I know this is a simple question for someone out there, but I have never really used function module at all because I did not understand what they were.

So I have a whole bunch of things I can use this for (cut down on redundancy), but I want to know how I call call into into a sub (like a button click) procedure from a form.

I tried this...

Sub Command_Click()

call "pptCreator"

end sub

I know that is pretty bad, but I have no idea how to bring this into a procedure.

Thanks for the help

+1  A: 

if pptCreator is a function/procedure in the same file, you could call it as below

call pptCreator()

shahkalpesh
i wrote the function in a module and saved it as pptCreator. then I go to one of the forms that I want to use this in conjunction with, and the event trigger is a button click.so...Public Sub Command1_click ()call pptCreatorend subis this correct? i tried using it and I get a compile error stating that expected end of statements; not modulethanks for the help
Justin
+4  A: 

Here are some of the different ways you can call things in Microsoft Access:

To call a form sub or function from a module

The sub in the form you are calling MUST be public, as in:

Public Sub DoSomething()
  MsgBox "Foo"
End Sub

Call the sub like this:

Call Forms("form1").DoSomething

The form must be open before you make the call.

To call an event procedure, you should call a public procedure within the form, and call the event procedure within this public procedure.

To call a subroutine in a module from a form

Public Sub DoSomethingElse()
  MsgBox "Bar"
End Sub

...just call it directly from your event procedure:

Call DoSomethingElse

To call a subroutine from a form without using an event procedure

If you want, you can actually bind the function to the form control's event without having to create an event procedure under the control. To do this, you first need a public function in the module instead of a sub, like this:

Public Function DoSomethingElse()
  MsgBox "Bar"
End Function

Then, if you have a button on the form, instead of putting [Event Procedure] in the OnClick event of the property window, put this:

=DoSomethingElse()

When you click the button, it will call the public function in the module.

To call a function instead of a procedure

If calling a sub looks like this:

Call MySub(MyParameter)

Then calling a function looks like this:

Result=MyFunction(MyFarameter)

where Result is a variable of type returned by the function.

NOTE: You don't always need the Call keyword. Most of the time, you can just call the sub like this:

MySub(MyParameter)
Robert Harvey
So does that mean instead of "calling" the function into the form's sub, I would being "calling the form into the function module??I think i understand, but am still a little confused.I have a powerpoint automation procedure that I want to use all throughout the db, because the purpose of this db is tracking metrics, and the end game for the client is presentations (mostly power point). I have different sets of code scattered throughout different forms that acomplish this for me, but mostly it's all repetitive. From a recent explaination the Functions in modules will help me.....
Justin
eliminate most of the repetitive coding by calling a "certain set of code" from a module function. But it operates off the forms. So the event trigger is still "button clicks" from the forms.It sounds like I have the idea in reverse, that I have to call the form sub to the function? instead of the other way around.Sorry I know its pretty obvious I am at best a novice at this...thanks very much for your help.
Justin
In your case I think you just need to put your sub in a module, make the sub public, and then call it: call pptCreator. See "To call a subroutine in a module from a form" in this answer.
Robert Harvey
thanks again! appreciate it!
Justin
GOT IT! thanks very much again for the help. this is something I should have already been doing!!
Justin
that really helped, but how do I call a "function" into a procedure? is this the same idea? i have tried all this for a function and it did not work; every error message in the book.
Justin
+1  A: 

Procedures in a Module start being useful and generic when you pass in arguments.

For example:

Public Function DoSomethingElse(strMessage As String)  
    MsgBox strMessage
End Function

Can now display any message that is passed in with the string variable called strMessage.

Mark3308
ok. how do I call a "function" into a form's sub event (like a button click)? the module contains a function. can i call that the same way as a sub? i have tried over and over and i get a host of error messages.this is code that i found, and I am not sure if it will be useful to me or not, but I cannot even get past compile errors. thanks
Justin
A forms sub event as you call it is not a function it is a sub procedure. Also an event in the form is Private so it can only be called from within the form class. To call a form from out side then a procedure has to be declared as Public. Also Remember a function always has a return value and a sub procedure does not.
Mark3308