-
Can someone explain to me what are loops used for. I have tried to read several articles but could not understand. For some strange reasons,materials produces by Karl Moore (VB World) I tend to understand.
I am new to programming.Usually I use the If ...then sratements.
thanks
.....
bon
-
example:
You have an array of text boxes called txtBox and a string array to hold the info. There's is 4 text boxes and you need to store the text in the string.
dim strText() as string, intI as integer
redim strText(4)
for intI = 0 to 3
strText(inti) = txtText(inti).text
next intI
This would go through each text box putting the text into the string array. You wouldn't have to use a string array or even the text array, it's best for when you have to repeat code over again. It's better to type it once and loop through it then typing it over and over again. Hope this clarifies a bit.
-
A loop is simply a way to do the same thing many times. There are a number of different loop types, each one has a specific purpose;
1. FOR..NEXT loops
Use if you know exactly how many times you need to do something;
eg;
FOR i=1 TO 10
MSGBOX "I've looped " & i & " time(s)"
NEXT
2. WHILE..WEND loops
Use if you want to loop until a certain condition is met;
Dim MyName as String
WHILE MyName<>"Fred"
MyName=InputBox("Enter your name")
WEND
The program will not continue until "Fred" is entered into the inputbox.
Example 2;
..open a recordset...
recordset.MOVEFIRST
WHILE NOT recordset.EOF
..do something to the recordset..
recordset.MOVENEXT
WEND
Because you may not know how many records there are in a recordset when you open it it is better to use a WHILE..WEND loop to work your way through it. This program will not continue until it gets to the end (EOF) of the recordset.
I know these examples aren't actually very useful in the 'real world' but you may find them easier to understand than some of the exmaples in help files.
There are other loops (like REPEAT..UNTIL, DO..WHILE etc etc etc) but I think a combination of FOR..NEXT and WHILE..WEND should suit most purposes)
Let me know if you want more 'real' examples.
------------------
Mark "Buzby" Beeton
VB Developer
[email protected]
-
A loop is simply a way to do the same thing many times. There are a number of different loop types, each one has a specific purpose;
1. FOR..NEXT loops
Use if you know exactly how many times you need to do something;
eg;
FOR i=1 TO 10
MSGBOX "I've looped " & i & " time(s)"
NEXT
2. WHILE..WEND loops
Use if you want to loop until a certain condition is met;
Dim MyName as String
WHILE MyName<>"Fred"
MyName=InputBox("Enter your name")
WEND
The program will not continue until "Fred" is entered into the inputbox.
Example 2;
..open a recordset...
recordset.MOVEFIRST
WHILE NOT recordset.EOF
..do something to the recordset..
recordset.MOVENEXT
WEND
Because you may not know how many records there are in a recordset when you open it it is better to use a WHILE..WEND loop to work your way through it. This program will not continue until it gets to the end (EOF) of the recordset.
I know these examples aren't actually very useful in the 'real world' but you may find them easier to understand than some of the exmaples in help files.
There are other loops (like REPEAT..UNTIL, DO..WHILE etc etc etc) but I think a combination of FOR..NEXT and WHILE..WEND should suit most purposes)
Let me know if you want more 'real' examples.
------------------
Mark "Buzby" Beeton
VB Developer
[email protected]