April 25, 2018
Hot Topics:

When Subs Arent Enough

  • November 4, 2002
  • By Karl Moore
  • Send Email »
  • More Articles »

Everything going well? Understand it all so far? Oh good - 'cause I'm about to throw a massive spanner into the works. You see, sometimes simple Subs just aren't clever enough.

Are standard subs too simple?

A more advanced form of procedure...

For instance, say you were creating an insurance quote system where you need to pass information to the Sub - say, the premium to pay each year. Then the Sub needs to multiply the premium by two and display the information in a message box.

Following me? Good. Create a new Standard Exe with a simple form and two text boxes and command buttons as such:

Call them txtCalcOne, txtCalcTwo, cmdCalcOne and cmdCalcTwo respectively.

Open your code window once more and select (General) from the Object drop-down list. Now type -

Sub CalculateAmount(Amount as Integer)

and press enter. Then tap in the following code:

MsgBox Amount & " times two equals " & Amount * 2

Your Sub should look something like this:

Sub CalculateAmount(Amount)MsgBox Amount & " times two equals " & Amount * 2End Sub

This is getting weird now, so let me explain.

If you can remember doing maths at school, you'll probably recall that most fashionable of topics, algebra. Well, it's back in fashion.

With this code, we're telling Visual Basic that when we call "CalculateAmount" in the future, we'll also be passing it a parameter called "Amount" - the value of which we are currently unaware.

In the code, our Sub refers to this "Amount" - even though its value isn't currently known. The code displays the "Amount" in a message box - as well as multiplying the number by two with the code, "Amount * 2".

We've not talked about calculating in Visual Basic yet, but it's pretty straightforward. The calculation operators are determined by the following keys

  • Multiplication *
  • Addition +
  • Minus -
  • Division /

So if you wanted to add 5 and 7 together, then divide the result by 10 - you could program "(5+7)/10". If you've ever used Microsoft Excel, you'll know all of this.

What did you say? Shut up and get on with the tutorial? Oh. Yes, right. Fine. No problem. Don't worry over me. Lil' old me. That's fine.

Let's imagine we called our Sub and passed it the number four. It would take that "Amount" and display a box that should say something like:

"4 times two equals 8"

To calculate the 8, VB simply multiplies the value of "Amount" by two.

Let's add the code to call this Sub. Under the Click event of cmdCalcOne, add the code:


We're now calling the CalculateAmount Sub, passing it the text value of txtCalcOne as "Amount". If it's a number, it will calculate the appropriate figure.

In the Click event of cmdCalcTwo, add the code:


Notice that we're now passing a different value to the Sub - that of our second text box.

Try running the application, entering various numbers into the text boxes and clicking their associated buttons.

If you don't enter a valid number into the boxes, be sure to expect errors. Don't say I didn't warn you! If you get errors such as "Type Mismatch" just hit the End button and start again. It's just warning you it can't calculate because you didn't enter a proper number.

Are you starting to see the value of Subs, particularly Subs with parameters? You pass this general piece of code a value that it utilises in code.

Perhaps you could use it to change the caption of your form, depending on the customer name. Perhaps you could use that one piece of code to disable or enable all the command buttons on your screen. Perhaps you could just forget about Visual Basic and go back to farming. It's your call.

Page 6 of 7

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

Sitemap | Contact Us
