Site hosted by Angelfire.com: Build your free website today!

EXTRA INFO

Getting Started with VB6

GUI

Removing the form

Object Orientated programming (OOPS)

Getting to the Next Lesson

Code Used

Variables and Data Types

Operators

Message Box Info

 

 

 

WELL DONE !!
lesson 3 -

AIM - To Understand How to Use the VB6 Workspace

 

The answer to the code is

Sub Main()

MsgBox "Hello it's good to see you"

End Sub


Close any existing work and open a new Visual Basic (VB) Programming Environment. Start >> Programs>> Visual Basic 6. Open a new Standard EXE file and remove the form. Now add a module. If your unsure how to do this click on the 'Getting Started with VB Link' on the left.

We now have a 'code' window ready to go, we understand how to create message boxes and make them say what we want. Now we learn how to 'set up' our environment to help us with debugging (sorting code errors) and how to make the message box display a whole host of other items.

First the environment - understanding what the standard toolbar does can save you a lot of time later.

Standard

Click around and have a play with these buttons. IF you get 'lost' close the program don't save and re-open.

Go to the menu toolbar and click Tools then choose Options - your window should look like below


Click on the editor tab and ensure ALL options are selected

Now click on the General tab and ensure it is the same as below.

OK, you are now going to 'play around' with message boxes. We are going to allow the user a choice of answers on a message box - Yes or No. In order to do this the program will need to save the user's answer.

To save the user's choice of button we need a variable, it's called a variable because the answer could vary (it could be a different answer) - in this case it could be Yes or No. We need to tell the computer to allocate some space to store this answer, how much space is allocated depends on the variable type.

The computer needs to know what sort of data will be stored so it can allocate a storage point on the disk (so it can retrieve the data later) and how much space to allocate (the variable type). We do this by declaring a variable AFTER the begin program code.

Sub Main()

Dim Choice as Byte

The first word Dim stands for dimension, think of it as get some space

The second word Choice is the name of the variable - it's how the program will locate the information. The variable name should always reflect what the variable holds - 'x' and 'y' are poor variable names. There are other constraints on names.

The third section as Byte dictates the type of variable and lets the computer know how much space to allocate.

You will need to know the various variable types. For the purpose of this course there are 3 main types of variable. Numbers, Text and Boolean.

Numbers

Numbers are the largest class of variable type and are divided into

  • Byte - whole numbers .........................................0-255
  • Integer - whole numbers .....................................-32,768 to 32,767
  • Long - larger numbers.........................................-2,147,483,648 to 2,147,483,647
  • Currency................................................................used for currency declarations.
  • Single...........................used when you require a +/- decimal or fraction up to six digits of accuracy
  • Double..........................used when you require VERY large or small numbers that are +/- decimals with 14 digits of accuracy.

Text

In programming text is given the name 'string'. We will no longer use the term text. You would use a string variable to save any text such as a person's name or a music album title. You would also use string to save a phone number since you do not want to allow mathematical functions on the number. String can refer to any alphanumeric data.

  • String....................................................................any alpha numeric data - letters, chars or numbers.

Boolean

This returns either true of false and is used to check something

  • Boolean................................................................True or False

Click here for a more detailed summary of variable types.

There are also restrictions on the names we can give variables

Naming conventions

These are the rules to follow when naming elements in VB - variables, constants, controls, procedures, and so on:
  • A name must begin with a letter.

  • May be as much as 255 characters long (but don't forget that somebody has to type the stuff!).

  • Must not contain a space or an embedded period or type-declaration characters used to specify a data type ; these are ! # % $ & @

  • Must not be a reserved word (that is part of the already used code words, like MsgBox, for example).

  • The dash, although legal, should be avoided because it may be confused with the minus sign. Instead of Family-name use Family_name or FamilyName.

Moving On

In your code window (i.e. the module) type the following code. It is important to type in the code as written to see the VB help that shows you the 'order' of message box commands. There is a link on the left to Code Used. Use this link if you get stuck to try to write your own code.

NB - You can cut paste all code BUT the more you have hands on experience at writing code - the faster you'll learn how to code for yourself also you need to understand the 'tool tip' type prompt that appears as you type.

Sub Main()

Dim Choice as Byte

Choice=MsgBox(

I have chosen Byte as the variable type because the buttons on a message box return a number value between 1 and 7.

When typing the code you should see the 'tool tip' prompt below as you add the open speech marks.

msgPrmt

This MsgBox 'prompt' dialogue shows us the things we can do with a message box. The first thing we enter inside the brackets are speech marks and the Prompt or what we want the message box to say, then close the speech marks.

Choice=("Would you like to play a game?"

We now need to add a comma. This moves us to the next feature the type of message box. In this example it is vbOkOnly but we want Yes and No so we add this after the comma - you can select it from the drop down menu and press SPACE to add the chosen text.

Yes No

Sub Main()

Dim Choice as Byte

Choice=MsgBox ("Would you like to play a game?",vbYesNo

Don't forget to close the brackets and END THE PROGRAM.

Sub Main()

Dim Choice As Integer

Choice = MsgBox("Would you like to play a game?", vbYesNo)

End Sub

Run your program - you should get the message box shown below. If not close your program don't save and start again. You'll get it - just keep trying!!

MsgBox Run

Nothing happens when we press the buttons because we haven't written that code yet! First SAVE your work. Lets add an icon to the message box to make it more interesting. There are 4 icons to choose from:-

We can add an icon by placing a + after the type of message box

Choice = MsgBox("Would you like to play a game?", vbYesNo+)

When we add the plus you should see the drop down menu again

Add icon

You can choose vbQuestion from this list and press space OR you can just type in the number 32 - shown in the table above. Now your code should look like this

Sub Main()

Dim Choice As Integer

Choice = MsgBox("Would you like to play a game?", vbYesNo + vbQuestion)

End Sub

OR

Sub Main()

Dim Choice As Integer

Choice = MsgBox("Would you like to play a game?", vbYesNo + 32)

End Sub

When you run your program you should get

Msg box Run

If everything worked - well done now save all your work including the form. You will need this for the next lesson. Now experiment with a few message boxes of your own. Close VB6 and open a new Project remove the form add a module and use the link on the left to help you create message boxes of your own. Save your Project as Test1, don't worry if things go wrong. The more you get wrong the more you learn, just close and start again. You will need to have a good understanding of data types, variable naming conventions and the code used so far before you can move on. When you think you are ready click the button for the quiz.

If things didn't work don't despair. Go back a few steps and try again. Close, don't save and start again.