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!

Run a Module Function from an event

Status
Not open for further replies.

puforee

Technical User
Oct 6, 2006
741
US
I have converted a macro to a module and it has a function name. I would like to run this function from a click event on my form.

How do I do this>? The function name is Macros_Update(). What do I put in the Click event field of the object that should trigger the function?

Thanks,
 
If the function doesn't take any parameters or return a value then this should suffice (assuming you have a button called Command0 on your form and the Function isn't declared as Private):
Code:
Private Sub Command0_Click()
Macros_Update
End Sub
Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
HarleyQuinn - Thanks.

The function is Macros_Update(), from your answser do I understand the () are not needed?

Thanks,
 
Yep, the braces aren't needed if you're not passing parameters or assigning a return value from the function.

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
Glad I could help [smile]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
I stumbled upon this when I was looking for a way to use a module level function trigerred by the before update event that I would like to return a value (cancel =true or false). Is there a way to accomplish this?
 
Yeah, something like:
Code:
Private Sub MainDateRecd_BeforeUpdate(Cancel As Integer)
Cancel = YourFunction
End Sub

Function YourFunction() As Boolean
YourFunction = True
End Function
If you need to pass parameters to the function you'd call it like this:
Code:
Private Sub MainDateRecd_BeforeUpdate(Cancel As Integer)
Cancel = YourFunction([i]param1,param2,param3...[/i])
End Sub
Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
Thanks for the reply Harley.

Hmm, I see my question was not clear.
What I meant was that I would like to skip the whole classmodule behind the form (property hasmodule=false) and only use a module level function. Reason is that this way improves maintainability and speed.
This solution works fine as long as no return values are needed. i was wondering if I could use this for the beforeupdate event as well.

In code this would be something like:

Public Function FunctionBehindBeforeUpdate() As Boolean
FunctionBehindBeforeUpdate = false
end function

Does not work.....
 
puforee,

If you're still reading this, and you really did find HarleyQuinn's advice helpful, then you ought to make it known by using the [blue]Thank HarleyQuinn for this valuable post![/blue] link.

You've participated now in the forums for about 3 years. You've started 77 threads, replied 133 times, and received 2 "helpful post" votes, yet you've not marked a single other post helpful yourself.

The way the tek-tip forums work does depend partly on us making known which posts are helpful by clicking the "thank... for this valuable post" links. When someone wants to search on a topic, they can specify to sort the results by most helpful, for instance. Also, you can often see who seems to know the most (or give the most helpful answers) in a particular area by looking at who the MVPs list for each forum.

It's also a nice little pat on the back, or "atta boy" for those that do help others in their questions and problems.

I just wanted to point that out. I figured it's possible you just didn't know, but after 3 years, you'd think you would have read some posting guidelines, or noticed the little pink stars or something. [wink]

--

"If to err is human, then I must be some kind of human!" -Me
 
Reinier10,

I'm not sure you can do what you're after - with Access. It may be possible, and it'd be interesting to know, but here's why I doubt it:

If you click on the button on a form, then SOMETHING has to call the function. The way Access is setup, it uses events to call things. Those events ARE the code behind the form which shows in the module.

Also, when asking a question, even if related to an existing post, you should start your own thread. That's also in the forum guidelines. Just remember that going forward. It makes searching for answers for everyone much easier, and it probably makes the forum more manageable.

--

"If to err is human, then I must be some kind of human!" -Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top