Visual Basic Introductory Tutorial
In this article we shall cover some of the basics of Visual Basic programming. Despite what the name of the programming suggests, Visual Basic can be used for very complex programs. ]
BASIC stands for Beginner's All-purpose Symbolic Instruction Code, and the visual element is due to the fact that the programming relies on a graphic layout, as the Tutorial shall demonstrate later.
The first bit of coding we shall look at is setting up a messagebox to display a message.
Hello world msgbox
Code:
Msgbox Prompt, [buttons as VbMsgBoxStyle = vbOKOnly], [ Title], [helpfile], [context], As VbMsgBoxResult
- Prompt is the actual text you want the msgbox to display
- This first major variable tells the compiler (visual Basic) which type of buttons or sometimes the style you want on the MsgBox. As default this is set to vbOKOnly. After entering the prompt and a Comma and a space, you get a dropdown box of all the possible options here. It’s easier for you to do this by trial and error, as most of these are fairly self explanatory, than to simply exhaustiavely list all of them.
- The “title” variable dictates, as the name suggests, allows you to change the title text displayed in the blue bar at the top of the message box.
- This part of the Msgbox is far beyond the scope of this tutorial, so we shall simply ignore it in this case, as we will with context.
- The final part of the MsgBox statement simply sums up what this function does, we do not need to worry about its effect or do anything to code with it.
So, now you understand all the components of this statement, lets code a Msgbox command. Firstly, on the General Toolbar on the left-hand side of the Visual Basic screen, Click on the Command icon, the Rectangle grey button, as shown below.
Move the cursor back to the main Form view area. A Crosshair cursor should now replace the normal arrow cursor. Click and drag to place the command button.
This should look something like the image on the left, although the placing and size is entirely up to you.
To change the text displayed with Command Button, we need to go into the properties area. This is by default on the right hand side of the screen, and usually in the middle of all the other sections, although it can be detached. If you can’t see this, you need to do the following to make it appear.
View | Properties Window
Or press the F4 key on the keyboard.
To change the text on the button, we are concerned with the property called “caption” On the right hand side of the properties bar is the text currently in the box. We simply select this, and start typing what we want in there. For this exercise, this can be anything you want.
To code for the Msgbox, go back into the Form Mode by pressing SHIFT + F7
Double click on the Command button again, and you will be presented with your first page of code. By double clicking on that button, the compiler adds the start and end bits of code that allow that button to do anything.
Code:
Private Sub Command1_Click()
Msgbox “Hello World”, vbOkOnly, “Hello World”
End Sub
This should display the following Message box. If it does not, you will need to do some basic troubleshooting. Check that all the spaces are in the correct places, there should be a space after ever comma in the code, not before, although if you do put a space before a comma, the compiler will automatically remove it. Diagnosing issues in the code at this early stage is crucial, because as code gets more and more complicated tiny errors can cause massive headaches. We’ll look at error trapping solutions later on.
You have written your first bit of code. In the next section you will look at conditional coding, which is crucial part of many programs.
Conditional Statements:
Unless you are programming a completely static program, where only one set of outcomes can arise, you will need to use conditional statements of some kind. The most basic type is the IF statement, and this is how it works and how we shall use it.
The logic of an If statement is as follows:
Code:
If Statement is (whatever we choose to test, usually true or false) Then do the following
Code
Else ( the above statement resolves as false)
Do this code
End the if statement
This is pretty much exactly the code we use. To make this really effective, we will be combining If statements with the msgbox we did above (with a bit added)
Project: The idea of this project is to display a Msgbox, to capture the result of this MsgBox and display a relevant response in the program.
The first job is to create variables, pieces of data stored by the program that allow us to do something dynamically and store responses to certain events in the program.
View | Code
Just above the area containing the code there are two dropdown menus. On the left hand one, select “General” The dropdown menu on the right will automatically change to “declarations” There is now a space above the Msgbox code we did last. Enter the following code:
Code:
Dim response As Integer
The Dim bit tells the compiler we want to define a variable here. response is the name of the variable and “As integer” tells the compiler that we shall be inputting a number into this variable at some point. Notice that the variable doesn’t contain any data yet, it simply exists. You will need this later.
Still in Code view, go back to this sub we wrote earlier
Code:
Private Sub Command1_Click()
Msgbox “Hello World”, vbOkOnly, “Hello World”
End Sub
The changes needed to get this to store a result and then turn it into something the user can see, are small, comparatively.
Code:
response = MsgBox("Hello World", vbYesNo, "Hello World")
instead of :
Code:
Msgbox “Hello World”, vbOkOnly, “Hello World”
To display some useful outcome to the user we need to interpret the value of "Response". Yes or No are represented in a msgbox by either a 6 or 7 respectively
Returning to
Code:
If (response = "6") Then
MsgBox ("Yes")
Else
MsgBox ("No")
End If
If we were to take this to its extreme, we would really need an ElseIf statement instead of the Else statement to check the other value was indeed a 7, however in this case the value cannot be anything else, so this is good enough for our purposes.
[end of article]