Visual Basic I   
 

Opp

 

 

 

 

 

 

 

 

 


 

Visual Basic: a Basic outlook

 

Getting started

 

So you've just got Visual Basic, and as basic as it sounds, it doesn't seem so. Don't despair, here is a beginners guide to the language. Visual basic is a great language to get into as it is simple and quite versatile. It is known as a high-level language (which means it is easy to read). You will have to fork out a bit in order to buy it, but it is a great language to use. I will be writing this tutorial for Visual Basic 6.0, but you should be able to follow it with most of the earlier versions of Visual Basic. On opening Visual Basic(VB), select 'Standard Exe' then press Open, you will be presented with this screen:

Double click on the Grey patch and you will be presented with a white screen which reads:


 

Private Sub Form_Load()

End Sub


 

Basically, this is saying: when the form loads do what comes after the final bracket and before end sub. Which, at the moment is nothing.

We are going to create a simple program that will take in two numbers chosen by the user, add them together and then display the answer. We will do so with the use of somethings called variables. Because we don't know the numbers that the user is going to enter, we cannot pre-enter them into our program, thus we will give them a name and the number entered by the user will be stored 'In that name'. In order for us to use one of these variables, we have to declare it. This is done in three parts:

1) Tell the computer for how much of the program you want to use the variable.
2) Tell the computer what the name is that you are going to use for the variable.
3) Tell the computer what kind of information is going to be stored in the variable.


 

So in order to declare a variable called 'FirstNumber' (NB variables cannot contain spaces) which will hold whole numeric values in it we would write this on the next line after Private Sub Form_Load()

Dim FirstNumber as String


 

The 'Dim' tells the computer that the variable will be used throughout this form [which is like a chapter of a book]. The 'FirstNumber' tells the computer that the variable will be refered to in the program as FirstNumber. The 'String' is a data type which will incorporate up to 256 characters in it.

Now do the same thing twice more, but this time replacing FirstNumber with SecondNumber and Answer respectively.

Your piece of code should now look like this:

Private Sub Form_Load()

Dim FirstNumber as String Dim SecondNumber as String Dim Answer as String

End Sub


 

Now that we have declared the variables, we must ask the user to input the numbers they want. This is done like this:

FirstNumber = InputBox("Enter a whole number to be added to another number")


 

The InputBox part does exactly what you think: it brings up an input box in which the user can input a number. That number is then assigned to FirstNumber. NB the equals sign here means make what is on the right become what is on the left. Now do the same thing, but change the FirstNumber to SecondNumber. And make the phrase make sense.

Your code should look like this:

Private Sub Form_Load()

Dim FirstNumber as String Dim SecondNumber as String Dim Answer as String

FirstNumber = InputBox("Enter a whole number to be added to another number") SecondNumber = InputBox("Enter a whole number to add to your first number")

End Sub


 

Now we need to tell the computer what to do with these numbers; we do so like this:

Answer = CInt(FirstNumber) + CInt(SecondNumber)


 

So what's happening here? First of all, the computer turns the strings into integers which means that the computer will deal with them as numbers. Then, the variable Answer (on the left) will take the sum of the variables on the right as its value. Add this line after the second InputBox.

Finally, we need to display the answer to the user. Like this:

MsgBox "This is the outcome " &Answer


 

This will display a message box, with the message followed by the answer. Note the space before the end of the speech marks.

Your finished piece of code should look like this:

Private Sub Form_Load()

Dim FirstNumber as String
Dim SecondNumber as String
Dim Answer as String

FirstNumber = InputBox("Enter a whole number to be added to another number")
SecondNumber = InputBox("Enter a whole number to add to your first number")

Answer = CInt(FirstNumber) + CInt(SecondNumber)

MsgBox "This is the outcome " &Answer

End Sub


 

To run your program, press the play button on the icon bar.

 


 

 

So there you have it, your first Visual Basic program. Now let's take it a bit further, you will have noticed that if you press the cancel button on either of the input boxes, you will receive an error message. This is because the thick computer tries to execute the code, but one or both of the values has not been given. To get around this problem we will have to use something called an if clause.


 

 

If Clauses

 

You will use these a lot in your programming. An if clause is structured like this:

IF argument THEN
?..
ELSE
?
END IF

For example...

If FirstNumber > SecondNumber Then
MsgBox "The First Number is bigger than the Second Number"
Else
MsgBox "The First Number is not bigger than the second Number"
End If

The keywords here are IF THEN ELSE and END IF.

When you press Cancel on an Input Box you are giving no value to the computer. Therefore, if you think about it logically, your if clause should be structured like this:

If FirstNumber = "" Then
MsgBox "You did not enter anything for the First Number"
ElseIf SecondNumber = "" Then
MsgBox "You did not enter anything for the Second Number"
End If

Notice that we used the keyword ELSEIF here, this is the keyword you use when you want to use multiple ifs.

There is one more thing that you need to add to these. You need to tell the computer to go back to the beginning once it has encountered this error, therefore you have to add the keyword GOTO and an IDENTIFIER at the beginning of the code. This is done like so.

If FirstNumber = "" Then
MsgBox "You did not enter anything for the First Number"
GoTo Start
ElseIf SecondNumber = "" Then
MsgBox "You did not enter anything for the Second Number"
GoTo Start
End If

At the beginning of the code you need to add the word Start followed by a colon, this is known as an identifier, because the computer uses this to identify where you want it to go. Your code should now look like this:

Private Sub Form_Load()

Dim FirstNumber as String
Dim SecondNumber as String
Dim Answer as String

Start:

FirstNumber = InputBox("Enter a whole number to be added to another number")
SecondNumber = InputBox("Enter a whole number to add to your first number")

If FirstNumber = "" Then
MsgBox "You did not enter anything for the First Number"
GoTo Start
ElseIf SecondNumber = "" Then
MsgBox "You did not enter anything for the Second Number"
GoTo Start
End If

Answer = CInt(FirstNumber) + CInt(SecondNumber)

MsgBox "This is the outcome " &Answer

End Sub


 

There is so much more that you can do in Visual Basic. You now know the bare basics and on these bare basics, if you put your mind to it, you can build a few basic programs. Keep checking the site and soon enough there will be another tutorial taking this further.

Startside ] Opp ] [Søk]

Copyright © 2002 Øyvind Haugland
Sist endret:  13 januar 2019
 

  Interested in this stuff? Please write to:
 

HTML Counter            stats counter