Lesson 5
Home Up Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Assignment 6

ASCII table

Assignment 5

Loops

Input Boxes

An Input box is a pop up window that can get information from the user. To activate an Input box you use the following function:

varVariable = InputBox(<prompt>,[<title>][,<default>][,<xpos>,<ypos>][,helpfile,context])

wpe1.jpg (7063 bytes)

varVariable = InputBox("This is the prompt","This is the Title","Default")

From the Input box above you can see the first three parts of the function. The optional x and y pos arguments place the upper left corner of the box at a position from the upper left corner of the screen. The last two optional arguments indicate while helpfile and context value is associated with this box. Whatever is in the textbox, when the user clicks OK, is the data that is put into the variable on the left of the assignment. If the user clicks Cancel, then nothing is assigned to the variable.

Message Boxes

Message boxes enable you to communicate with the user through a pop up window. To display a message box use the following function:

intValue = MsgBox(<prompt>,[iconstyle+buttons+defaultbutton][,<title>][,helpfile,context])

wpe2.jpg (6060 bytes)

intValue = MsgBox("This is the Prompt",vbExclamation + vbAbortRetryIgnore + vbDefaultButton2, "This is the Title")

The prompt and title are the same as the Input box, except argument position is different. The icon style, type of buttons and default buttons are developed from adding the Visual Basic constants from the tables below.

Visual Basic Icon constants
Visual Basic Constant Description Appearance
vbCritical Display Critical Message icon wpe3.jpg (1175 bytes)
vbQuestion Display Warning Query icon wpe6.jpg (1043 bytes)
vbExclamation Display Warning Message icon wpe5.jpg (1092 bytes)
vbInformation Display Information Message icon wpe4.jpg (1041 bytes)

 

Visual Basic Button Type constants
Visual Basic Constant Description Appearance
vbOKOnly Display OK button only wpe8.jpg (1184 bytes)
vbOKCancel Display OK and Cancel buttons wpe6.jpg (1595 bytes)
vbAbortRetryIgnore Display Abort, Retry, and Ignore buttons wpe2.jpg (2392 bytes)
vbYesNoCancel Display Yes, No, and Cancel buttons wpe4.jpg (1965 bytes)
vbYesNo Display Yes and No buttons wpe3.jpg (1788 bytes)
vbRetryCancel Display Retry and Cancel buttons wpe9.jpg (1629 bytes)

 

Visual Basic Button Type constants
Visual Basic Constant Description
vbDefaultButton1 First button is default
vbDefaultButton2 Second button is default
vbDefaultButton3 Third button is default
vbDefaultButton4 Fourth button is default

When the one of the buttons is clicked the MsgBox function will return a value based on the type of button selected, the value returned is summarized in the table below:

Return value of Button selected
Button Selected Value Returned Visual Basic constant
OK 1 vbOK
Cancel 2 vbCancel
Abort 3 vbAbort
Retry 4 vbRetry
Ignore 5 vbIgnore
Yes 6 vbYes
No 7 vbNo

The Counted Loop - For...Next

Loops allow you to execute the same code repeatedly a set number of times or while a certain condition exists. There are two loop structures that Visual Basic has to allow repeat code. One structure is a counted loop and the second is a conditional loop structure. The For...Next looping structure is a referred to as a counted looping structure. You will use this structure when you know how many times the loop must be executed. The basic structure of a For...Next loop is:

For LoopControlVariable = <startvalue> To <endvalue> [Step <increment>]

    <statement/s to repeat>

Next LoopControlVariable

The LoopControlVariable (LCV) must be a declared integer type. At the first iteration, the LCV is assigned the startvalue. The statements in the body of the loop are executed. When the keyword Next is encountered, the LCV is assigned the next integer value in the range. If the value of the LCV is less than the endvalue, the loop undergoes another iteration. If the value of the LCV is larger than the endvalue, the loop is terminated. If the optional Step keyword is used, then the next integer assigned to the LCV is larger than the current value by the amount determined by the increment value.

For example, the following loop will start with the LCV (intLoopExample) being assigned the value 1. The label, lblShowLCV's caption property will display the message, The LCV is now 1. Then the next value in the range is assigned to the LCV, since the step increment is 3, the value assigned will be a 4. The label will then display the message, The LCV is now 4. This will repeat with the LCV being 7, then 10. The last message shown in the label will be  The LCV is now 10. The LCV is incremented to 13, which is larger than the range, so the loop terminates.

Dim intLoopExample As Integer

For intLoopExample = 1 To 10 Step 3

    lblShowLCV.Caption = "The LCV is now " & intLoopExample

Next intLoopExample

Variations of a For..Next loop involve using variables in place of the startvalue, endvalue and increment value.

Conditional Loop - Do...Loop

The conditional loop structure in Visual Basic has four variations. The Do..Loop has two keywords to use with a conditional statement, the While and Until.

Variation 1

Including the While keyword and condition after the Do could result with the loop not being executed at all. If the condition is false, the loop will not execute even once. If the condition is true the loop executes. There must be a statement inside the loop to affect the condition, to keep it true to have the loop repeat again, or make it false to end the loop.

Do While <condition>   'Will enter loop if condition is true, and continue if true

    <statement/s>

Loop

Variation 2

Including the Until keyword and condition after the Do could result with the loop not being executed at all. If the condition is true, the loop will not execute even once. If the condition is false the loop executes. There must be a statement inside the loop to affect the condition, to keep it false to have the loop repeat again, or make it true to end the loop.

Do Until <condition>    'Will enter loop if condition is false, and continue if false

    <statement/s>

Loop

*Note* Both of the above variations require that a statement must affect the condition to make it true or false.

Variation 3

Including the While keyword and condition after the Loop will result with the loop being executed at least once. There must be a statement inside the loop to affect the condition, to make it true to have the loop repeat again, or make it false to end the loop.

Do

    <statement/s>

Loop While <condition>    'Will execute loop at least once, and continue if condition is true

Variation 4

Including the Until keyword and condition after the Loop will result with the loop being executed at least once. There must be a statement inside the loop to affect the condition, to make it false to have the loop repeat again, or make it true to end the loop.

Do

    <statement/s>

Loop Until <condition>    'Will execute loop at least once, and continue if condition is false

*Note* All of the above variations require that a statement within the loop must affect the condition to make it true or false. If the condition is not affected then the loop may never end. The dreaded ENDLESS LOOP..ENDLESS LOOP..ENDLESS LOOP..ENDLESS LOOP......

Which variation you use it totally up to you. Some programmers only use one of the variations in all their work. Others use all four, since different looping problems require different logic. Also by using the appropriate variation, it tends to help in self-documenting your code.

Accumulator Statement

Accumulator statements are assignment statements that are used by programmers to keep track of running totals or to count. The main part of an accumulator is the fact that the same variable is used on both sides of the equal sign. By doing this the computer takes the current value in that variable, performs the operation, then the result of the operation is stored in the variable. Therefore the variable is updated with a new value.

In the following line, the current value of curTotal is added to the value of curPay, with the result replacing the current value of curTotal with the sum. This statement when included into a loop will keep a running total.

curTotal = curTotal + curPay

In the following line the result when put into a loop is a way of counting by by one.

intCount = intCount + 1

Assignment