March 1, 2021
Hot Topics:

Get Out The Saurus

  • By Karl Moore
  • Send Email »
  • More Articles »

Sorry folks, but this is where it gets serious. Put away your Beano annual and get out Programmers Weekly. In this section, we're going to explain Subs.

What are subs, and how are can they help VB coders simplify their source?

Corny jokes to one side, these things are really useful. And pretty darn simple when you get the hang of it.

In preparation for this article, I found the word in my Thesaurus. Its alternate suggestions included "inferior", "assistant" and "helper" - the latter of which probably best defines the term.

A Sub is basically a piece of code separate from the rest. It's often used when you have a piece of common code that you don't want to rewrite each time you use it.

Let's pretend I'm Bill Gates (imagine the salary!). I helped created Microsoft Word. How does a user exit Word? They can either click on the top-right hand Close button ( ) or press the Exit button.

What if each time they exited, I wanted to say "Thanks for using Office and adding another $500 bucks to my wallet!"?

Well, I'd have to duplicate code.

Each time the user clicks on that corner cross button, the QueryUnload event of the form fires up. So, I'd write my code there.

Then, if the user click on my Exit button, I'd have to write the code in the command button's Click event. Phew!

And what if I had an Exit option on my menu, too? You can see the problem here, folks. Duplication of code leads to huge maintenance issues - particularly if you're not just dealing with simple message boxes. Every time the user logs out of your program, you may want to record the time and date in your security log. You wouldn't want to duplicate all that code, would you?

You can get around this using Subs. You can create a singular, generic chunk of code that displays the message and then ends your program.

Therefore, in the QueryUnload event of the form and the Click event of the command button, you simply need to tell Visual Basic - "Run the code behind that Sub!"

Let's try this one. Create a Standard Exe, with a form and command button. Open the code window and change the Object drop-down list to (General). Now type-

Sub EndProgram

and press enter. Visual Basic should add "End Sub" to your code. Your screen should look something like this:

Sub EndProgram()End Sub

You've just told Visual Basic you're creating a Sub called "EndProgram" and have defined the appropriate code boundaries. Now you need to write code inside that Sub.

Type in -

MsgBox "Thanks for using my program. Have a really super day, geek!"End

Great! You've just defined your own Sub called "EndProgram" and have written code within it. Now each time we run EndProgram, it will display the insulting message and end your application.

But how do we get it to run? Simple.

Firstly, we'll tell it to run the Sub whenever the QueryUnload event of the form occurs.

In the code window, change the Object drop-down list to Form and the Procedure drop-down list to QueryUnload. In the code that appears, type the name "EndProgram".

Your code should look something like this:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)EndProgramEnd Sub

Now add the same code to your command button - and try running your application.

Whenever you click the command button or try closing the form, Visual Basic runs your Sub and processes the code within it.

The ability to create such Subs can be a real time saver! It avoids unnecessary code and therefore cuts down on the size of your final program. It also saves on boring maintenance time.

Page 5 of 7

This article was originally published on November 5, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date