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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use a module?

Status
Not open for further replies.

PB90

Technical User
Jul 11, 2005
65
0
0
US
Can someone explain to me how to use a "module" for something, instead of having the code stored within a form?
and/or where I can find information on it?
Thanks
 
An ordinary module is simply a container for Subs (subroutines) and functions.

Each is a self contained set of code, which can be called from elsewhere in the function. The difference is that a function returns a value, and can therefore be used in an expression, whereas a sub cannot.

For example:

Code:
Public Sub DisplayMessage (strMessage As String)
  MsgBox strMessage
End Sub

and
Code:
Public Function MultiplyTwoNums (intNum1 As Integer, intNum2 As Integer) As Integer
  MultiplyTwoNums = intNum1 * intNum2
End Function

Create a new module and copy and paste the code sections in there, then save the module. It doesn't matter what you call it, as its the functions and sub names that get used when you call it.

Now, go into a form and in something, for example, the click event of a button, type
Code:
  Call DisplayMessage "This comes from DisplayMessage - 1"
  Call DisplayMessage "This comes from Displaymessage - 2"
  MsgBox "2 * 2 is " & MultiplyTwoNums (2, 2)
  MsgBox "3 * 3 is " & MultiplyTwoNums (3, 3)

Switch to form view, then click the button. You should get four messageboxes - with different data.
While the above examples are very simple, they illustrate the fact that you can use them for a lot of different things. Your imagination is the only limitation.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top