How to call repetitive code from a Module?

  • Thread starter Thread starter Bucky
  • Start date Start date
B

Bucky

I know this should be simple, but I'm kinda new at this and have never
used a module before.

I've got some VB code behind a form that I want to happen on the OnOpen
and OnTimer events. The code is identical for both, and is about 120
lines long. Seems like I should be able to create a module and call
the code from both events, but I've never done it.

In the module, would I create a procedure or function or? How do I
call it from the OnOpen and OnTimer events?

Thanks all...
 
You simply add the name of the sub to the other one so you could for example
use:

Sub Form_Timer()
Form_Open
End Sub

or you can create a named sub or function and call it from both of them:

Private Sub Whatever()
' yada yada
End Sub

Sub Form_Timer()
Whatever
End Sub

If the sub or function is public and/or in a standard module you can call it
from another form the same way as the last example above.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access
 
Create a module. In it make a function (your 120 lines of code) including,
of course, any arguements you need to pass to it.

In your form's OnOpen & OnTimer events just call the function.

e.g. Suppose you have a function in your module:

Function MyFunction(str as string, num as integer) as string
....bunch of code....
end function

From your form:

Sub On_Open()
dim returned_string as string
returned_string = MyFunction("This is a string", 5)
end sub

That's all there is to it. What's even nicer is you can call this function
from any of your forms/queries/macros etc in your database without ever
repeating it. Definately use modules to store functions (in this amateur's
opinion anyway!)
 

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

Back
Top