http://www.developer.com/

Back to article

Visual Basic Tutorial - Part 6


November 6, 2002

Hello and welcome to the very final instalment of this Visual Basic tutorial. This week, we'll review what we've learnt over the past couple of months. We'll run over data types, loops and functions. And we'll recommend where to go from here and which books you'll find useful. We'll even tell you how to get Microsoft certified!

As ever, I'm your amazingly awful host, Karl Moore. When your confusion levels reach an all-time peak, I'm here to soothe the pain with a bit of boring geek-speak.

If you haven't read any of the previous tutorials yet, grab the latest at:

  • Tutorial One - Basic introduction to VB
  • Tutorial Two - Exploring objects, properties and code
  • Tutorial Three - Looking at variables and conditional logic
  • Tutorial Four - An adventure with subs and loops
  • Tutorial Five - Another look at properties, methods and events, then an exciting dive into functions!
  • Each of the above tutorials aroused much public response and an array of questions.

    In this last part, I'll briefly review the top dozen queries, tying up a few of those loose strings, providing simple answers to help you understand Visual Basic to the fullest.

    I appreciate all the friendly e-mails, and related impertinent comments that regular readers/enemies/weirdos have sent me. Indeed, they have been the very foundation of this final section.

    So, without any further delays, let's ride the digital engine into the world of Visual Basic...

    Back in those halcyon days of tutorial number one, we took a look at the Visual Basic basics (forgive the rather obvious pun, if it indeed was a pun). We explored how to start Visual Basic and create a very simple program.

    Let's answer a few of your common questions about this tutorial...

    Q1. When I started Visual Basic, you told me to choose 'Standard EXE' from the long list of things in the 'New' tab. What do all those others thingies do?

    Well put, Son. Those other thingies are all technical thingies that are a bit technical. Thingy.

    The standard EXE is the most popular type of application you will want to create. However, Visual Basic also allows you to create other types of files, too. For instance, you can create your own .DLL file that you can "call" from another program. Or you can create an ActiveX control that you can put on your webpage and allow other people to interact with.

    But most of the time, you'll simply work with a standard EXE.

    Q2. Karl baby! I saw your picture on the Internet and you're a complete love machine. What's your telephone number?

    I'm afraid I couldn't possibly publish it here. BT would be so cross if I jammed all their lines. It's on its way to you via e-mail, honey sugar baby sweetcake.

    Q3. What's that "Project" window thing all about?

    The Project window is usually displayed in the top right hand corner of your VB screen. It allows you to easily select and display a particular component of your project.

    So, say you create a new 'Standard EXE'. The project window should display "Project1", with a subfolder called "Forms" containing one form entitled "Form1". That means your project, called Project1, only has one form in it right now - Form1.

    Of course, you can change that. You can add more forms to your project. You can even get really geeky and add modules, or even classes. But that's for another day.

    In brief, the Project window allows you to see all the bits that make up your project.

    Q4. OK, so I've designed my simple program. But how do I make it into an executable? You know, a .EXE?

    To make your project into an executable, click on the File menu and select "Make ". That's it!

    If you're going to distribute your application to other computers, you'll need to encapsulate it and a few other VB files in a setup program. Don't worry - Visual Basic will do all this for you. Simply choose the "Package and Deployment Wizard" from the Visual Basic folder on the Programs menu. Good luck!

    In this section, we'll be answering a few top questions from the second tutorial. That week, we looked at controls and naming conventions

    Q1. When I started my Visual Basic project, I got a standard 21 or so controls in my toolbox. Can I add more - or is that it?

    Before I answer this question, let's take a couple of seconds to remember exactly what a control is. A control is something you see in the toolbox that adds functionality to your project.

    For instance, a simple textbox is a control. Users can enter information into the textbox and you can then read from that textbox. You can even put text into the textbox, change its colour or make it invisible.

    Another example of a control is the Picture control. You use this to display images in your application.

    However the answer to your question is quite simple. You are not limited to the 21 items you see in your toolbox. There are indeed many, many more available. A fair number ship with Visual Basic, but you can also buy hundreds of third party controls too!

    Try right clicking on a blank bit of your toolbox and selecting "Components..." from the popup menu. You'll see a long list of available controls. Simply ticking one and clicking OK will make it available to your project. Many of the extra controls that ship with Visual Basic start with the name "Microsoft". What an extraordinary surprise!

    Just to prove my answer, here's my toolbox after working on an exceptionally tricky project...

    Q2. How can I make a textbox invisible while my program is running then?

    You can affect an object on your form by changing its properties. Don't forget - a property is simply that - something that tells you about an object. For instance, the Visible property will tell you whether your textbox is visible or not. You can also change this property in code.

    For instance, you could add a command button to your form. Every time, the user clicked the command button you could run this...

    If Text1.Visible=True then
    	Text1.Visible=False
    	MsgBox "Your textbox - Text1 - is now invisible!"
    Else
    	Text1.Visible=True
    	MsgBox "Your textbox - Text1 - is now visible!!!"
    End If

    The first line checks to see whether the box is visible. If it is, it makes it invisible by changing the property. If Visible is not equal to True, Visual Basic moves onto the Else bit and makes the textbox visible!

    In brief, to change the properties of a control in code, use the format:

    ControlName.Property = NewValue

    To change the property of a control at design time - ie, when you're not actually running your program - use the Properties window.

    Q3. Karlos honey sugar. Is that photo of you real? Are you really soooo musclely?

    Of course!! Do you honestly think I'd use a photo editing package (such as, I don't know - PhotoFantasy, #24.99 available from software shops near you) to give readers the idea that I'm a muscle-bound hunk - when I'm not? Tish, tish. Who do you think I am? Some kind of fraud?

    Q4. Here's a question about those naming conventions. What if I don't know the naming convention for a particular control?

    Naming conventions only exist to make your life easier. Let's pretend I have a form with a label and a textbox. The label may have a caption of "Date:" and I might want the textbox to hold a particular date.

    Now, I obviously can't name them both "Date". If I do, I have the problem of distinguishing between the two different controls when I refer to them in code. Therefore, I call the label "lblDate" and the textbox "txtDate".

    Naming conventions aren't enforced. They're simply there to help you recognise what kind of a control something is. If its name starts with "txt", I know it's a textbox. If it begins with "lbl", I know it's a label. And so on.

    Back in tutorial three, we took a look at variables and that terrifying thing they call conditional logic (we saw a bit of that on the last page, with question two).

    Let's dive straight in and answer your frequently asked questions...

    Q1. What's conditional logic then?

    Conditional logic is just the posh name for "checking stuff". Let's pretend you're working on an order system. If the customer order is more than #500, you might want to award them with a free rubber chicken. If it's less than #500, you may want to give them a free rubber dummy.

    Conditional logic is just the process of deciding which option to choose. It works something like this..

    If txtOrderValue.Text > 500 then
    	MyOrder.AddItem "Free Rubber Chicken"
    Else
    	MyOrder.AddItem "Poisonous Rubber Dummy"
    End If

    Conditional logic is the "If-Then-Else-End If" bit.

    Q2. What kinds of information can a variable hold?

    I hope you all recall that a variable is just something you hold information in. In the General Declarations section of your form code, you declare the variable with a line of code, like:

    Dim MyVariable

    Then you can tell that variable to remember some information by setting it's value in code - with something like:

    MyVariable = "Hello There!"

    Or you can read its value with something like:

    MsgBox MyVariable

    You can also declare a variable to hold a specific type of information, such as a date. To do this, declare it like:

    Dim MyVariable as Date

    Of course, you could replace the Date bit with any other data type. You could use String to hold a string of text or perhaps Integer to hold a number.

    Q3. Hiya Karlos! Are you SURE that picture is of you?

    Yes, yes, YES!! The picture is mine. All mine. It's mine!! Stop hassling me, woman. Ed, can't you stop this woman sending me e-mail?

    In tutorial four, we covered loops and subs. Whew, what fun! Here are your top questions:

    Q1. Where can I find more information about loops?

    Have you tried visiting an asylum?

    Seriously though, and all NHS jokes to one side - loops come in many different shapes and sizes. You have "Do While" loops, "Do Until" loops, "For-Each-Next" loops and something else- erm, I can't remember them all.

    But you can find full descriptions of the various loops by consulting the VB help file. Just lookup "Loops" in the available index and you'll spot the many different options.

    Q2. So, in brief, what is a sub?

    A sub can be defined one of two ways. Let's do them both.

    (a) Definition of Sub
    A block of code that can be called by tapping in its name. For instance, if your Sub is called "EndProgram" - as in tutorial four - just type EndProgram anywhere in your code to run it. The Sub may also accept "arguments" or "parameters" - these are simply extra bits of information you pass to the Sub. For instance, you may pass it a username so it can say, "Get Lost <USERNAME>!" instead of an impersonal and rather less harsh, "Get Lost, Geek!". What an advantage.

    (b) Definition of Sub
    Part of usual wage given up front. Borrow. Lend. Scrimp. Debt. Bottomless pit. Alcoholism.

    Q3. Hi Karlie! Me again. That picture isn't of you now, is it?

    OK, it's not of me. It's all one big hoax. A scandal! It's a fake. I'm a phoney. A fraud. One gigantic scam! Haha. HAHA! HAHAHAHA!!

    Tutorial five was the most difficult tutorial of all time, ever. In the entire universe and infinitesimal galaxies beyond. Honest.

    In that section, we took another look at properties, methods and events. Feel free to check out the tutorial here, should you have a strange desire to review that section.

    We then moved onto explaining functions... wow, they're just so useful!

    Q1. So, a function can return a value, right?

    That's right. Let's take an example function:

    Public Function AddThemUp( _
                 NumberOne as Integer, _
                 NumberTwo as Integer)
    
    	AddThemUp = NumberOne + NumberTwo
    
    End Function

    Let's pretend you call this function with the line of code:

    MsgBox "1 plus 2 equals " & AddThemUp(1, 2)

    When Visual Basic hits the AddThemUp part, it runs the function - passing it the parameters 1 and 2 consecutively.

    The function then sets the return value of AddThemUp equal to NumberOne (the first parameter, which is 1) plus NumberTwo (the second parameter we passed, which is 2).

    Therefore, VB displays a message box saying "1 plus 2 equals 3".

    If it doesn't, check that your computer:
    (a) Is Switched On, or
    (b) Is Switched On, or
    (c) Is Switched On

    But who cares. After all, everybody knows that 1 plus 2 equals 4. It's a trick question.

    Q2. Karl? KARLIE? Is that you, honey sugar pumpkin?

    BEEP!

    Q3. In some of the tutorial five examples, you displayed something like this - MsgBox "Textbox One plus Textbox Two equals " & Text1.Text + Text2.Text

    Why did you use the "quotes" and the & ampersand & ?

    Every time you work with a string - basically, a bit of text - you must enclose it in "quotation marks". This is to ensure Visual Basic understands it's a string of text and not a command.

    For instance, your string might contain the text 'MsgBox' - now you don't want VB to go displaying a message box when it sees that. That's why you use quotes - to partition off text strings.

    The ampersands are used to concatenate bits of information together. So "Hello " & "You" in Visual Basic comes out as "Hello You". Just like the example you quoted above comes out as "Textbox One plus Textbox Two equals 35".

    Get it? Got it! Good!

    Congratulations! You've successfully mastered the foundations of Visual Basic. But where do you go from here?

    Your best bet is to grab a Visual Basic book and start working through it, front to back. I recommend either:

    • Beginning Visual Basic 6, from Wrox Press (www.wrox.com), or
    • Visual Basic 6 from the Ground Up, from Osborne (www.osborne.com)

    (Ed - Check out more books at http://www.vb-world.net/books/)

    And if you really want to prove you're king of the nerds, you'll probably want to take a Microsoft Visual Basic examination, such as "Developing Applications with VB"

    For more information, head down to Microsoft's examination site at www.microsoft.com/train_cert/

    And if you're wondering what those petrifying examinations are like, tinker with the Transcender samples at www.transcender.com

    They offer sample Microsoft examinations which are suspiciously similar to the real thang...

    But that's all for me. This is Karl Moore drawing this Visual Basic tutorial to a close. It's goodnight from me, and it's goodnight from me. Goodnight!

    Karl's Visual Basic Tutorial Index

    Visual Basic Tutorial
    Visual Basic can be confusing, especially for newbie programmers. But don't worry - here to help is technology journalist and head geek Karl Moore, with the first installation of his up-and-coming Visual Basic tutorial!

    Visual Basic Tutorial - Part 2
    Karl Moore returns with the third instalment of our exclusive no-geek-speak Visual Basic tutorial. This week, Karl gets his hands dirty with variables and conditional logic, so get out your nerds dictionary!

    Visual Basic Tutorial - Part 3
    Karl Moore returns with the third instalment of our exclusive no-geek-speak Visual Basic tutorial. This week, Karl gets his hands dirty with variables and conditional logic, so get out your nerds dictionary!

    Visual Basic Tutorial - Part 4
    Karl Moore gives you the buzz about looping, grabbing information from users and subs, as always without geek-speak!

    Visual Basic Tutorial - Part 5
    Karl Moore returns with another geek-speak free guide to Visual Basic coding. This week, Karl explains what methods, events and properties actually are, as well as delving into the enigmas that are functions. Lets code geek-to-geek baby!

    Visual Basic Tutorial - Part 6
    Head geek Karl Moore returns with the final instalment of his popular jargon-free VB tutorial!

    Sitemap | Contact Us

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