Solved

Global function/subs

Posted on 2003-11-25
4
295 Views
Last Modified: 2010-05-01
Hi..

I have some repeated functions/subs in my program, i would like to know how to make a global function/sub for use throughout all my forms.

Thanks
0
Comment
Question by:leeh0003
4 Comments
 
LVL 8

Expert Comment

by:dds110
ID: 9817243
You don't.  However, you can make the functions Public.  Insert a new module into your project and drop the code in it.  Then, when you need the function, you can call it like so:

Call ModuleName.FunctionName(arguments, arguments, etc...)

I'm not sure about your subs.  Try changing them to functions instead.

HTH
0
 
LVL 14

Expert Comment

by:aelatik
ID: 9817263
Just add a module and start your subs/function like :

Public MyFunction()
End Function

or

Public Sub MySub()
End Sub

This way you can call them through all forms like

Call MyFunction

or

Call MySub
0
 
LVL 11

Accepted Solution

by:
dbrckovi earned 75 total points
ID: 9817278
Just use Public keyword and it will be accessible from anywhere im your app.

Example:

Public Function func_Message() As Integer
    func_Message  = MsgBox("Displayed some text", vbYesNo)
End Function

Or:

Public Sub sub_Print
       MsgBox "Test"
End Sub

If you create theese functions in form (let's say Form1)  then it is accessed like:           Call  Form1.sub_Print
and if you declare them in module then you don't need to specify     Module1.sub_Print,     you just have to call it like any other function
0
 
LVL 9

Expert Comment

by:dancebert
ID: 9818586
If your function is in a class in an ActiveX DLL project, you can set it's instancing property to GlobalMultiUse.  That makes your function as available as a VB native function.  From the VB Help:

GlobalMultiUse. Similar to MultiUse, with one addition: properties and methods of the class can be invoked as if they were simply global functions. It’s not necessary to explicitly create an instance of the class first, because one will automatically be created.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question