4: Write Code
Up
Recall that these pages were originally made for VB5/6 - code is rather different in VB.NET but is introduced properly in the VB.NET section of this site.

isual Basic is an 'event-driven' language - this means that certain events such as clicking a command button, choosing a menu item or pressing a key on the keyboard can trigger a sequence of actions in the program (a 'subroutine' or 'procedure'). Let's see how the first type of event, the clicking of a command button, could be coded.

Imagine that the form contains a command button called Cmd1 and that we want to give a "Hello" greeting when the button is clicked by the user. Double-clicking the button opens up the code window and we see that two lines of code have been automatically generated for us already:

Private Sub Cmd1_Click()

End Sub

These two lines are called 'wrappers' - they mark the beginning and end of the subroutine and therefore the code we write must be inserted between these wrappers. Note the word 'sub' in the first line - this is short for subroutine, of course. At the end of the first line we see that this subroutine will be activated when Cmd1 is clicked. The parentheses are empty here but sometimes they will contain variables called 'parameters' that are used to pass information to the subroutine.

Add two lines of code and the subroutine will be complete:

Private Sub Cmd1_Click()
MsgBox ("Hello")
End Sub

MsgBox is short for 'messagebox' and will display your message on the screen. Your program is now complete! Click the Run icon (or press the F5 key) and test your program by clicking the command button. Stop the program by hitting the stop icon or by clicking the X in the top-right corner of the form (don't close VB itself!).

Now let's extend the code to add an 'input' instruction - we will make the computer ask the user for his or her name. Modify the code so it looks like this:

Private Sub Cmd1_Click()
UserName = InputBox("Enter your name")
MsgBox ("Hello, " & UserName)
Beep
End Sub

The second line displays an 'inputbox' that asks the user to enter his or her name, and then stores the answer in a variable called UserName. A variable is like a container - it can contain numbers or text, and the contents of the variable can change while the program is running - hence the name variable! You've met variables before in your math classes, of course. Note that variable names can't contain spaces - this variable could not be called 'User Name'. Interestingly, the name 'Name' is also not allowed, for it is one a few dozen 'keywords' that are reserved by Visual Basic.

Look again at the second line. Note how the equals sign is being used in a way here that is very different to its use in your math class. In VB, the equals sign is usually used to modify the contents of a variable, or to change the properties of a control. When used in this way, the equals sign is called the 'assignment operator'. Notice that the variable on the LEFT of the operator is assigned the value given on the RIGHT of the operator.

Imagine that your program uses a variable called x. What would the following line do?

x = x + 1

This line (which would make no sense in a math class!) works out the value of the expression on the right by adding one to the existing value of x and then stores the result back into the variable called x. In other words, this line has the effect of adding one to x.

Sometimes the equals sign is used as in your math class, as in the following line:

If (x = y) then beep

This line checks to see whether the variables x and y have the same value and if they do then the computer beeps.

Notice the parentheses in the above line - VB is very fussy about grammar (called 'syntax' in computer programming). If the syntax is not correct then your program may not run at all! VB tries to help you avoid syntax errors - it will display code in red if it finds errors in a line of code that you have just typed.

All computer programs consist of a combination of linear (step-by-step) sequences of actions, or statements, conditional branches where a test condition determines what the program will do next, and loops, where the same sequence of instructions can be repeated many times.

Previous Up Next