Looping / Loop-Lookup Example
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this movie I will use a demonstration program to illustrate the use of a Do Loop along with some If statements. The user will be expected to enter an item into the text box and that item will be compared to the items in the List Box. As you can see in this example, there are simply three items in this List Box. Of course, in actuality, the number of items in the list could be any number at all. There is a button to allow the user to perform the lookup and this is where our code is associated. In our code, we have two dim statements that you will see the purpose of as we work our way through. One, however, is a Boolean variable which will basically tell us when the item has been found. The key here is the use of the Do Loop. This Do Loop uses the pre-test technique, which will test to see if the item has been found or the item index is equal to three. And the item index is simply a variable that has been declared to keep track of each item in the list. The significance of the number three is simply the fact that there are three items in the list. If there are more items in the list, of course this number would have to be adjusted. To make the program even more dynamic, you could also tie this number to a property associated with the List Box itself, such as the items count property. Within the Do Loop there is an IF statement that is comparing the user inputted data in the item text box and comparing it to each item in the List Box. The secret to this program, in my opinion, is the Subscript. The fact that the Subscript, in this case item index integer, is being used to manipulate and be able to compare each item in the List Box one at a time. By declaring it up above, the item index integer defaults to zero. So therefore, the first item that's being examined is the first item, recognizing that our first item in the list is indeed zero, zero Subscript. If the item that's being searched for happens to be the first item, then we set the item found Boolean variable to true and the Do Loop will then stop by virtue of this do statement at the top. If the statement is not found, if the item is not found, then the loop will continue but not before the item index has been incremented by one. This way each time through, item index will look at the next item in the list. So this Do Loop will continue until one of two things happens. Either the item is found or the end of the list has been reached. Once one of those two things has happened, a second IF statement is used to compare to see if the item found Boolean is equal to true, which if the item was found, would be true by virtue of the statement in the first IF statement. And if that's the case, then the message box displaying Item Found in List will display. If the item is not found, then we will see the Item Not in the List. We now take a look at the run of this program. So as I enter an item in the text box, for example, desk which, as I can see is indeed in the list, I'll click Lookup and yes, the message box correctly tells me the item found is in the list. If I click OK and I enter another item, such as computer and click Lookup, once again it tells me the item is found in the list. If, on the other hand, I enter an item which is not in the list, such as chair and click Lookup, I receive the message Item Not in the List, which is correct because the item is indeed not in the list. Once again, this is happening by virtue of the code associated with the Lookup Button. This demonstration program is included with your tutorials and I would suggest that you examine this code more closely and experiment with it so you can understand exactly how this code does work. The key, however, is the use of the Do Loops along with the If statements.
Tutorial Information
| Course: | Microsoft Visual Basic 2005 |
| Author: | Arthur Lee |
| SKU: | 33940 |
| ISBN: | 1-935320-10-6 |
| Release Date: | 2008-11-19 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 