SunQuest
 
       Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 7 - Programming Fundamentals Using VBA
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Programming Fundamentals Using VBA
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 19
    2005-07-28

    Table of Contents:
  • Programming Fundamentals Using VBA
  • Creating Procedures
  • Declaring Variables
  • Constants
  • Control Structures
  • The ElseIf Structure
  • Do Loop
  • Arrays
  • Dynamic Arrays

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Programming Fundamentals Using VBA - Do Loop


    (Page 7 of 9 )

    You use the Do loop as the sentinel-controlled loop. In other words, it will continue until a specific condition occurs (something equals something, or is greater than something, for example).

    There are two variations of the Do loop—the Do While and the Do Until. We will look at each one.

    Do While The Do While loop tests to see if a condition is true. If it is true, the statements in the loop execute. For instance, let’s look at the following subroutine:

    Sub doTest()
        Dim intCounter As Integer
        Dim intTest As Integer
       
    intTest = 1
        intCounter = 1
       
    Do While intTest = 1
            Debug.Print "This is loop number " & intCounter
           
    If intCounter >= 5 Then
                intTest = 0
            End If
           
    intCounter = intCounter + 1
        Loop
    End Sub

    You will notice that the Do While loop is not as self-contained as the For…Next loop discussed earlier. In this case, you have to set up two variables, one for the counter and one for the conditional test. In this particular subroutine, I wanted the loop to end after the fifth pass.

    Unlike the For loop from before, we are not running this based on a counter but, instead, until intTest changes to a value other than 1. I then nested an If…Then structure within the loop and tested the counter value. As soon as that value is equal to 5, the If structure will change the intTest value to 0 and will prevent the loop from running again.

    Since it is testing for a condition rather than the number of iterations, you would not normally use a counter in a Do loop. In this case, I forced one with a second variable and incremented it as the last line of the loop. There are a variety of ways to do this.

    Notice that this structure ends with the word Loop. Remember, we are not required to work with a counter here.

    If you try running the Do While code, your Immediate window gives you these results:

    However, here is a question: what happens if, for some reason, intTest never gets a value of 1? Will this Do loop ever run? (All right! Two questions!) The answer is no, it won’t. However, what happens if you need the loop to run at least once?

    Sub ifTest()
        Dim strMessage As String
       
    intNum = InputBox("Enter a number between 1 and 15", _
                          "Testing the If structure")
       
    If intNum >= 1 And intNum <= 15 Then
            iftest2
        Else
           MsgBox "Sorry, the number must be between 1 and 15"
        End If
    End Sub
    –––––––––––––––––––––––––––-
    Sub iftest2()
       
    If intNum > 10 Then
            MsgBox intNum & " is greater than 10"
        Else
            MsgBox intNum & " is less than 10"
        End If
    End Sub

    Notice, in the ifTest subroutine, there is a test to see if the user has entered a number between 1 and 15. However, if the user enters a number out of bounds, the program just stops, and you need to start it up again. We want to keep prompting the user until he or she enters a number in the correct range.

    Change ifTest to look as follows:

    Sub ifTest()
        Dim strMessage As String
        Dim intTest As Integer
       
    intTest = 1
       
    Do
            intNum = InputBox("Enter a number between 1 and 15",_
                              "Testing the If structure")
           
    If intNum >= 1 And intNum <= 15 Then
                intTest = 0
                iftest2
           
    Else
             MsgBox "Sorry, the number must be between 1 and 15"
            End If
        Loop While intTest = 1
    End Sub

    Notice that we are not using a counter of any sort here. Instead, what we are doing is testing for intTest. However, what is interesting here is where the test occurs. Unlike the previous example, we are testing at the end of the loop rather than the beginning. This has the effect of forcing the loop to run at least once, which can be very handy in situations such as this.

    Do Until This is a subtle variation of what we just looked at. In a Do While loop, you run the loop while a condition is true. However, in a Do Until loop, you run the loop until a condition becomes true.

    Using the first example of the Do While loop above, with a few changes, you can make it into a Do Until loop:

    Sub doTest()
        Dim intCounter As Integer
        Dim intTest As Integer
       
    intTest = 1
       
    intCounter = 1
       
    Do Until intTest <> 1
            Debug.Print "This is loop number " & intCounter
            
    If intCounter >= 5 Then
                intTest = 0
            End If
            
    intCounter = intCounter + 1
        Loop
    End Sub

    Notice that here we are saying to run the loop until intTest does not equal 1. From that point on, everything else in this example is identical. You should see the same result as with the Do While loop.

    Like the Do While loop, you can place the Until condition at the end of the structure after the word Loop. This forces the loop to run at least once.

    We will be revisiting loop structures frequently throughout the book. Like variables, they are an integral part of most programming situations today.

    More Visual Basic.NET Articles
    More By McGraw-Hill/Osborne


     

    Buy this book now. This article is excerpted from chapter six of the book Access VBA Programming, written by Charles E. Brown and Ron Petusha (McGraw-Hill/Osborne, 2004; ISBN: 0072231971). Check it out at your favorite bookstore today. Buy this book now.

    VISUAL BASIC.NET ARTICLES

    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway