Solved

Global function/subs

Posted on 2003-11-25
4
268 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now