Solved

Global function/subs

Posted on 2003-11-25
4
304 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 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

Independent Software Vendors: 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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

696 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