?
Solved

Global function/subs

Posted on 2003-11-25
4
Medium Priority
?
317 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Suggested Courses

765 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