Greeting
Up
startup

Greeting Program

By making this program you will learn about:

bulletlabel controls
bullettextbox controls
bulletcommand button controls
bulletthe messagebox statement (MsgBox)
bulletthe inputbox function
bulletthe assignment operator (=)

A three minute animation is available to introduce this lesson. If (and only if) you are on the European School 3 Brussels internal network then you can view an animation via the local network by following one the links on the left below. If you are located outside European School 3 then please follow one of the links on the right (these are large files so you must have broadband):

  in school, from server outside school, via internet
Greeting project3 (3 minutes, 8.5MB) web page SWF file web page SWF file

Start Visual Basic and create a Standard EXE project (if VB is already running then choose File/New Project). Save the project immediately with File>Save Project (not Ctrl-S, for that only saves the form file). Create a new folder called Greeting to hold the various parts of the project, then save the form file and the project file, both with the same name as the folder. It is important that every time you create a new project you should save it immediately (using File>Save Project) into a special folder. This is important because every project consists of at least two files, a project file and a form file, and you will lose track of them if you do not keep them together in a special folder. Don't forget to save your work every ten minutes or so - you can use Ctrl-S for this.

Make the form look like the one below by adding a label, a textbox and a command button. Set the Name property of each control to the names indicated below (lblPrompt etc). Even though changing the name of a control does not change its appearance, the name is very important since it will be used by the program's code (instructions) to identify that specific control. Also, the 3 letter prefix conventionally placed at the beginning of names helps you to remember what type of control it is. For example, if the name begins 'txt' then you know it belongs to a textbox control.

I have set the BackColor property of the label to white so that you can see its edges more clearly - you can leave it gray if you like. I have set the font property of every control to 12 since size 8 is rather too small to be easily legible.

Once you have set the name and caption/text property of each control (as well as the caption property of the form itself) then we can write code so that this program inputs the user's name and then greets the user and tells the time. Double-click the command button so that the code window opens and type (or copy and paste) the following line (the first and last lines, called the 'wrapper lines', will be generated automatically by Visual Basic).

Private Sub cmdGreet_Click()
MsgBox ("Hello, " & txtName.Text)
End Sub

Try running the program by clicking the run button in the toolbar at the top of the screen (or press the F5 key). Type your first name in the textbox then press the 'OK' command button - the program should greet you with a message box. Dismiss the message box and stop the program by pressing the stop button or by by pressing the close box in the corner of your form.

If your program does not work then you will need to 'debug' it. The most common error message is 'Object Required' - this means that the control names referred to in your code do not match the names you gave the controls using the properties window. Either you have made a mistake when you wrote the code or possibly when you set (or forgot to set!) the name property of each control. Check that you have named each control correctly, and that you have made no spelling mistakes in the code.

In the above code, the ampersand character '&' is used to join together the text string "Hello, " and the user's name, which is found in the text property of the textbox. The process of joining bits of text together is called 'concatenation', so the '&' character is the concatenation character in Visual Basic. Whenever you use the concatenation character '&' in your code, make sure you put a space on both sides of it. Note how the text property of the textbox is referred to using 'dot notation': controlname.property - this is very standard in Visual Basic.

Now modify the code like this:

Private Sub cmdGreet_Click()
MsgBox ("Hello, " & txtName.Text & ". The time is " & Time)
End Sub

Try running the program - the computer should now tell you the exact time according to its internal clock. Try replacing the word Time at the end of the code with Now - what does the program do now? Replace Now with Date and run the program again - what happens?

Using a textbox  is only one way of getting input from the user of your program - another neat way is to use an inputbox. An inputbox is rather like a messagebox except that it allows the user to type into it. An inputbox is an example of a 'function' because it 'returns' something to the program - in this case it returns the text that was typed by the user. We will store the text returned by the inputbox into a 'variable' - a variable is like a box or container. In our projects we will use a general-purpose variable type called a 'variant'. Variants can contain text (a block of text is called a 'string'), dates or numbers.

Modify the command button code like this:

Private Sub cmdGreet_Click()
country = InputBox("In which country were you born?")
MsgBox ("Hello, " & txtName.Text & " from " & country)
End Sub

Run the program and see what happens. It's very important to understand how the above code works, for the meaning of the '=' character is not at all what you would expect. You have seen this character in your math classes, so you think it is an equals sign. WRONG!! In Visual Basic the '=' operator does not usually work like an equals sign, instead it is an 'assignment operator' that takes what is on the right hand side and then stores the answer in the variable or property named on the left hand side. So a line such as

x = 3 + 6 

would evaluate the right hand side to equal 9 then store that number into the variable called x. In English, you could read the above line as 

'Set x to equal the result of 3 + 6'

You can see that a line like this

3 + 6 = x

which would be perfectly OK in a math class, would not be OK in Visual Basic because VB would first look for a variable called x, which it would not find, and then would try to store that 'nothingness' into '3 + 6'. This would crash the program because '3 + 6'  is neither a variable nor a property, so VB cannot store anything there.

In our project, then, the text returned by the inputbox function is stored into a variable called country and then, in the next line, the word "from" and the contents of the country variable are added after the user's name, in the message box. Note that the word country does not have quotes around it in this line, so the program assumes that this refers to the contents of a variable. Just to check that you understand this, change the code as shown below (note the extra quotes):

Private Sub cmdGreet_Click()
country = InputBox("In which country were you born?")
MsgBox ("Hello, " & txtName.Text & " from " & "country")
End Sub

Run the program, and then undo the last change to the code before saving your work.

Up Next