[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Global function/subs

Posted on 2003-11-25
4
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 300 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

650 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