Visual Basic Programming

Page 13

Now I think it is about time for loops! Loops are very important in programming. Loops tell a program to execute specific instructions in coding a certain amount of times. There are three types of loops used in Visual Basic, which are listed below, along with their coding structure:

                         Loops

For Next loop:

Structure:

For [Condition]
     [Statements]
Next [Variable name]

  • Example:

    For intCheck = 0 To 9
    chk(intCheck).Value = Unchecked
    Next intCheck

DoWhile loop:

Structure:

DoWhile [Condition]
     [Statements]
Loop

  • Example:

    conPassword = "Forgot"
    Do While strGuess <> conPassword
        strGuess = InputBox("Enter password:")
    Loop

DoUntil loop:

Structure:

Do [Condition]
[Statements]
Loop Until [Condition]

  • Example:

    strPassword = "Forgot"
    Do
    strGuess = InputBox("Enter password:")
    Loop Until strGuess = strPassword

If you have difficulty understanding the structures above, don't worry. I will explain line-by-line as we develop the program. Also, this is a longer section in the tutorial. Be sure you have the patience and attention span to take in all this information. Let's begin.

To explain this code:

Line 1: Declares constant conPassword as a String and stores Forgot within it.
Line 2: Declares string variable strGuess.
Line 3: Begins Do While loop; Says Do below(strGuess = InputBox("Enter password:", "DoWhile Loop")) While strGuess <>(greater than or less than; not equal to) conPassword(Forgot). It will execute Line 3 below if "Forgot" is not entered into the Input Box .
Line 3: Shows user the input box.
Line 4: Integrates loop and indicates the end of the loop statements.
Line 5: Displays message box once the loop ends (if strGuess=Password).
Lines 6 and 7: Centers form on the screen.

Now let's code the DoWhile command button (cmdWhileLoop).

If you run the application and display the form with the DoUntil loop, you notice that the variable Clicks is incremented very quickly. Increase the number to 500,000 (in the Loop Until code line; Ex: Loop Until sngClicks = 500000) and it will take a bit longer. Maybe try 50,000,000? If you put in a very large number, it may hang the machine, or take longer than you want to wait. Just press Ctrl + Break, then press the stop button to exit the application.

You can also use the DoUntil loop to repeat the password example in the DoWhile loop earlier.

ForNext

Now let's do the For Next loop.

Recall that this simply loads the form frmForNext and displays it to the user.

Now let's set up the form frmForNext.

Notice the access keys in the command buttons. The label lblTotal is the label displaying the text Total quantity:. The label lblTotalValue is the label to the right of it, which may be difficult to see in the image below.

Let's do the easy part first; coding the cmdClear button.

To explain this code, see below:

Line 1: Begins loop; takes intItem and sets the range of values from 0 to 2.
Line 2: Clears the text boxes txtQty; it sets the Text property to "" (empty). Since intItem has the values 0, 1, and 2, the intItem in txtQty(intItem) is set to 0, 1, and 2. It clears all the text boxes with values 0, 1, and 2, which covers each text box on the form. Remember as explained in line 1, line 1 sets these values.
Line 3: Same as line 2, but with the check boxes. Unchecks all check boxes with intItem (standing in for object's Index property) equaling 0, 1, and 2.
Line 4: Integrates loop; tells the loop to go to 1 when it finishes 0. Remember: The range is 0 to 2.Therefore, the loop stops once intItem = 2, which is true in the last check box and text box

Try running the application.

Now let's get this thing to display the quantities.

If the text box is enabled, the loop always looks at the value. This way, the text box cannot respond.

For intItem = 0 To 2
    If chkItem(intItem).Value = Checked Then
        txtQty(intItem).Enabled = True
    Else
        txtQty(intItem).Enabled = False
        txtQty(intItem).Text = ""
    End If
Next intItem

Line 1: Begins loop; takes intItem and sets the range of values from 0 to 2.
Line 2: Checks to see which check boxes are checked; checks intItem from 0 to 2.
Line 3: Enables the quantity text box corresponding with the index for chkItem.
Line 4: Otherwise, do the following.
Line 5: Disables the quantity text box according to the index of the check box.
Line 6: Clears the text in the text box according to its index
Line 7: Ends If statement.
Line 8: Integrates loop; goes to next values of intItem and continues to go through the values; 1 then 2.

Now let's get this program working.

The line above simply declares a local variable called intSubtotal as an integer in the cmdCalc sub-procedure.

The line above simply declares a form level variable as an integer. We will use this for our loop values.

Now let's finish this sub-procedure. Type the following lines below your variable declaration (Dim intSubtotal As Integer):

For intItem = 0 To 2
    If chkItem(intItem).Value = Unchecked Then lblTotalValue.Caption = intSubtotal
    If chkItem(intItem).Value = Checked Then
        intSubtotal = intSubtotal + Val(txtQty(intItem).Text)
        lblTotalValue.Caption = intSubtotal
    End If
Next intItem

Here is the explanation for this code:

Line 1: Begins the For loop, using value range of 0 to 2.
Line 2: Checks to see if all the checkboxes are unchecked. If they are, it displays 0, because the variable intSubtotal will equal zero under this condition. This line is not necessary, but if this line is not typed in, the label lblTotalValue will not show zero when nothing is checked.
Line 3: Checks to see which checkbox is checked.
Line 4: Adds the value of the corresponding quantity text box to the variable intSubtotal. This is needed in the loop so the value in the quantity text box has a place to go.
Line 5: Displays the value of the variable intSubtotal in the label lblTotalValue.
Line 6: Ends the If statement.
Line 7: Increments the For Next loop; goes to the next value of intItem.

I hope you enjoyed this rather long tutorial. Now that you have learned about loops, things are going to soon become much more interesting and complicated.

Now you can move onto Page 14 if you still have the energy.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30