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

TUTORIAL 1 OVERVIEW

Lesson 1

Lesson 2
Quiz 2

Lesson 3
Quiz 3

Lesson 4
Quiz 4

Lesson 5
Quiz 5

CHECKPOINT

EXTRA INFO

Getting Started with VB6

GUI

Removing the form

Getting to the Next Lesson

Code Used

Variables and Data Types

Operators

Message Box Info

Standard Toolbar and Options

 

 

WELL DONE !!
TUTORIAL1

lesson 4(ii) - Sunday, 27 January, 2008

AIM - To Understand How to use Input boxes

 

Your final answer is

Dim albumPrice as single

album price is the best choice since it is the most descriptive and single is the smallest variable type that allows for decimal places. You could also have used currency, but it would use more storage space.

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 know how to convey information to the user, by using a message box. We will now explore how to get input from the user. To get information we need an Input Box. The code for an input box is

VarName=InputBox("What we want the input box to say")

Note that the input box ALWAYS needs a variable since it will be getting information from the user and the program MUST store that information somewhere. Think of a variable declaration as creating an empty box. The box has a name (variable name i.e. melonCount) and the type of thing that can be stored in that box must be declared (variable type i.e. String, Number or Boolean). If a Number is going to be stored it must be further defined as to what type of number (Byte, Integer, Single etc) since the program needs to know how BIG to make the box (or storage area).

Type in the following code into your new VB module. If the program does not run. It is a typing error DOUBLE CHECK all spelling, it has to be a typo as the code is correct.

Input

Once your program runs - save as InputBox1 to your programs folder.

There are a few things to note in this new code. Name has been declared as a string since it will hold the text entered in to the input box. When the user types in their name it is stored in the space we have created by way of the Dim statement i.e. in 'Name'. The name is displayed back to the user with a message box. Note that the text in speech marks ("Hello") is reproduced in the message box. The stored Name appears in the message box by the program 'fetching' the stored value. The stored value Name does NOT have speech marks. The '&' symbol adds the two strings together to form the phrase. So what we want the message box to say is coded in speech marks, what we want the message box to retrieve is placed in brackets and the two are added together by way of the & symbol.

The space after "Hello_" is necessary because the computer will do exactly as we tell it. So if there is no space it will put the name right next to Hello i.e. "HelloDavid".

The basis of computing is

  • Input
  • Process and
  • Output

The computer gets information, does something with that information (even if it is only to store it) and displays data back to the user. It only becomes data when the computer has processed it.

Now modify the program to ask the user for their favourite colour. send a message back to the user saying

"Wow " & (colour) & " Thats my favourite colour too!"

Once you have modified the program see if it runs and check with the code below. Try not to just copy the code below, you want to learn to program not type someone else's code! Don't forget the REMs and indenting.

Get Colour

With this code the message box is as below. Can you make the code have an exclamation mark after Wow and the correct spacing and punctuation?

Msgbox 1

The message box should look like - Try to get it yourself and then scroll down for the answer! Remember your learning to PROGRAM not copy code!

Msgbox 2

 

 

 

MsgBox "Wow! " & (colour) & ". That's my favourite colour too!"