|






|
Beginner Tip
You will now learn how to use the Loop statements. These
will run commands continually until the condition is false or while a condition
is true.
1. Open Visual Basic 6.0 and start a new Standard EXE project.
Click here if you don't know how to do this.
2. On the General combo box of the code window, choose Form and the following text
will appear:
|
Private
Sub Form_Load()
End Sub
|
3. Here are some simple examples of using the Loop statement
(use all of these examples in between the above two lines):
|
Dim
i As Integer
Do Until i = 10
i = i + 1
Loop
Me.Caption = i
|
The above code creates an integer called 'i', which has a
starting value of 0. The Loop
statement, which starts with Do Until and ends with Loop adds 1 to 'i' and when
'i' reaches 10, it is displayed in the form's caption.
|
Dim
i As Integer
Do While i < 100
i = i + 1
Loop
Me.Caption = i
|
The above code also creates an integer called 'i'. The
statement does not exit while 'i' is less than the value of 100. It keeps
adding 1 to 'i' and when 'i' reaches 100, it is displayed in the form's caption.
|
Dim
i As Integer
Do
i = i + 1
Loop While i < 100
Me.Caption = i
|
You can also include Until and While after Loop.
Comments
| From: |
jyoti |
| Date: |
Friday, March 30, 2007 at 11:55:45 |
| Comments: |
I like your site
|
| From: |
Ben macharia |
| Date: |
Friday, March 9, 2007 at 09:53:54 |
| Comments: |
I enjoy your service.
|
| From: |
THUNDER_TOAD |
| Date: |
Thursday, November 16, 2006 at 20:07:59 |
| Comments: |
HEY EVERYONE
JUST READING THROUGH
THANKS FOR ALL THE COOL TIPS
HELPED ME A LOT
*HUGS EVERYONE*
I WILL BE ON MY WAY NOW.........
|
| From: |
Abdul Aziz Rajput |
| Date: |
Tuesday, October 17, 2006 at 17:40:04 |
| Comments: |
Thanks for good info:
|
| From: |
ralph |
| Date: |
Wednesday, August 30, 2006 at 07:36:07 |
| Comments: |
It's a very good site it helped me with the delay statement(syntax How to difine a timer) which i'm using in my project
|
| From: |
Ahmad |
| Date: |
Friday, April 14, 2006 at 20:11:14 |
| Comments: |
hi thanks for nice coding i want u to be my close coding friend bye
|
| From: |
ace |
| Date: |
Thursday, July 8, 2004 at 03:39:53 |
| Comments: |
i need to do a program that showing asterisk like this
between the asterisk is the addition sign..using the if then statement.
*
**
***
++++
*****
******
i really dont have an idea to do this progam..
PLS...HELP ME SOON..
|
| Reply: |
This can be achieved with the following code:
Private Sub Command1_Click()
Dim p As Integer, i As Integer
p = 1
For i = 1 To 50
p = p + 1
If p = 5 Then
p = 1
Print String(i, "+")
Else
Print String(i, "*")
End If
Next i
End Sub
|
|
| From: |
Alejandro Amaya |
| Date: |
Monday, June 21, 2004 at 21:09:07 |
| Comments: |
Well I used your code:
Sub Delay(t As
Integer)
Timer1.Interval = t * 1000
Timer1.Enabled = True
Do While Timer1.Enabled
DoEvents
Loop
End Sub
Private Sub Timer1_Timer()
Timer1.Enabled = False
End Sub |
But it seems timer never gets disabled.
I am trying to read the parallel port every milisecond to count pulses, and then every second I bring the number of pulses to have the frequency.
Please help me!!
|
| Reply: |
You need to call the Delay procedure to make this work. E.g.:
MsgBox "Wait 5 seconds..."
Delay 5
MsgBox "5 seconds has gone" |
|
| From: |
Fellow Programmer |
| Date: |
Thursday, March 25, 2004 at 09:58:42 |
| Comments: |
I would like to thank you for all the help your site gave me. Your site saved me alot of time and sleep. some times the smallest mistake can make your program just break down. thank you
|
| From: |
David |
| Date: |
Monday, February 9, 2004 at 22:29:25 |
| Comments: |
I need to make a program for school that displays the alpha belt like this-
A
AB
ABC
ABCD
ABCDE
Ans so on until it gets to z.
So far i have
Dim alpha As String
picOutput.Cls
alpha = Chr(65)
Do While alpha < Chr(92)
alpha = alpha + Chr(1)
picOutput.Print alpha
Loop |
I have no idea what to do
plz help soon.
|
| Reply: |
You can do this with the following code:
Dim letters As Integer, i As
Integer, alphabet As String
letters = 1
Do
For i = 1 To letters
alphabet = alphabet & Chr(64 + i)
Next i
picOutput.Print alphabet
alphabet = vbNullString
letters = letters + 1
Loop While letters < 27
|
|
| From: |
jagdish |
| Date: |
Tuesday, January 27, 2004 at 06:44:30 |
| Comments: |
hi
|
| From: |
Diszyunvme |
| Date: |
Saturday, January 17, 2004 at 18:07:49 |
| Comments: |
I need to create a loop that selects multiple files from a specific folder in the hard drive and runs this commmand:
| DoCmd.TransferText acImportDelim, "MCRDataImportSpecWithDate",
"tblTempMCRData", AFC(Me) , 0 |
for all the files selected.
Please help me.
|
| From: |
Katie |
| Date: |
Friday, January 16, 2004 at 20:52:36 |
| Comments: |
your loop examples helped a lot and allowed me to finish my project.
I haven't used vb so I'm rusty Thanks a lot!!
|
| From: |
elbren |
| Date: |
Thursday, January 8, 2004 at 11:54:12 |
| Comments: |
my problem is i dont know how to create a program using vb6 the time delay or the counting number counting backward from the given time or given number, please help....
|
| Reply: |
To count down numbers using the Do...Loop statements, try this code:
|
Dim
i As Integer
i = 10
Do Until i = 0
i = i - 1
Loop
Me.Caption = i
|
It might be easier to use the For statement with Step -1 to countdown
numbers.
A Delay function can be found further down this page.
|
| From: |
shahnawaz |
| Date: |
Sunday, November 16, 2003 at 23:15:34 |
| Comments: |
thnx for ....
|
| From: |
erum |
| Date: |
Wednesday, October 15, 2003 at 20:26:14 |
| Comments: |
when to use which loop?
set with manipulator
|
| From: |
Kate |
| Date: |
Sunday, October 5, 2003 at 12:58:17 |
| Comments: |
I have no idea what's wrong with my code but my counter isn't working... PLease help
Dim name As String
Dim Counter As Integer
Dim blnSixCands As Boolean
blnSixCands = True
Counter = 0
name = InputBox("Please enter candidate name or 'zzzz' to exit")
Do While blnSixCands
Or name <> "zzzz"
lstCandidates.AddItem name
Counter = Counter + 1
If Counter > 6 Then
blnSixCands = False
End If
name = InputBox("Please enter candidate name or 'zzzz' to exit")
Loop
|
|
| Reply: |
The problem is with the Do statement. The loop will only exit
if both blnSixCands equals True or name doesn't equal 'zzzz'. I am
not sure why this is - does anyone else know?
Anyway, the following code will work in place of the current Do
statement:
| Do Until blnSixCands = False Or name = "zzzz"
|
Also, you shouldn't really use 'name' as a variable because it is
already used by Visual Basic as the 'Name' property, which every control
has.
|
| From: |
ray alegria |
| Date: |
Friday, September 26, 2003 at 15:53:13 |
| Comments: |
How to keep looping in a date format.................................
If CALC.Text = "S" And txtCategoryTwo = "1"
And NTESTH < txtAdd Then
Do While NTESTH < txtAdd
txtNextSchedDat = DateAdd("d", 731, CDate(NTESTH))
NTESTH = txtNextSchedDat
Loop
txtTimeR = txtTimeSpent + ROUTTIME
txtTimesRout = ROUTTIMES + 1
txtDateEvent = txtAdd
lblEventLast.Visible = False
lblDateUpDate.Visible = True
txtDateEvent = txtAdd
iMonth = DatePart("m", CDate(NTESTH))
iYear = DatePart("yyyy", CDate(NTESTH))
txtTest = iMonth & "/15/" & iYear
Else
If CALC.Text = "S" And txtCategoryTwo = "1"
And NTESTH > txtAdd Then
' Do While NTESTH < txtAdd
txtNextSchedDat = DateAdd("d", 731, CDate(NTESTH))
'NTESTH = txtNextSchedDat
'Loop
txtTimeR = txtTimeSpent + ROUTTIME
txtTimesRout = ROUTTIMES + 1
txtDateEvent = txtAdd
lblEventLast.Visible = False
lblDateUpDate.Visible = True
txtDateEvent = txtAdd
iMonth = DatePart("m", CDate(txtNextSchedDat))
iYear = DatePart("yyyy", CDate(txtNextSchedDat))
txtTest = iMonth & "/15/" & iYear
End If |
|
| From: |
Jaryd |
| Date: |
Thursday, September 11, 2003 at 04:12:37 |
| Comments: |
Hi
I'm making a runescape experience calculator based on the levels a user inputs.
I am stuck with trying to fetch a users level input value, then run it through a do until formula to show them their experience for the level they enter.
The formula can be found at:
http://www.maddogcarter.com/runescape/xpformula.html
That site has the drawn out version of the calculation, along with javascript and python versions of the loop.
If anyone can figure this one out and post some code for me to use that might do this looping formula in visual basic i'd appreciate it a lot :)
|
| From: |
Sarah |
| Date: |
Thursday, September 11, 2003 at 01:52:40 |
| Comments: |
Your examples are easy to understand
You make it look a lot easier than they do in a class I am taking.
Thanks,
|
| From: |
Jeffrey |
| Date: |
Wednesday, August 20, 2003 at 19:05:25 |
| Comments: |
I would like to give a messagebox for every letter in the textbox...
Example: Text1 = "Hello"
Messagebox should be given for the "H" - "e" "l" "l" "o"
|
| Reply: |
You can do this with the following code:
| Dim i As Integer
For i = 1 To Len(Text1.Text)
MsgBox Mid(Text1.Text, i, 1)
Next i
|
|
| From: |
Josh |
| Date: |
Saturday, July 12, 2003 at 17:11:32 |
| Comments: |
How do I do a loop using information obtained from different TextBoxs?
Basically, I need a method for naming TextBoxs with the (i) format.
My TextBoxs are currently called TextBox1 TextBox2 TextBox3 etc. Is there anyway I can name them all the same name and just add (i) to the end of that name?
Thanks for your help...
|
| Reply: |
You need to create a control array. Rename TextBox2 to
TextBox1. Visual Basic 6.0 will ask you if you want to create a
control array - choose Yes. Then rename TextBox3 and all other
TextBoxes you want to include. You will see the Index property of
the TextBoxes now contains a unique number for each TextBox in the
array. To work with the TextBoxes using a Loop, try this code:
Private Sub Command1_Click()
For i = 0 To 3
MsgBox TextBox1(i).Text
Next i
End Sub |
|
| From: |
Moonss |
| Date: |
Saturday, July 5, 2003 at 19:02:03 |
| Comments: |
how do you use a loop statement which will allow users to enter many values into a textbox and keeps adding the value to a variable called COUNTER until the user types the value of -1 and hit OK only will the loop stop.
|
| Reply: |
You do not need to use a Loop statement for this, you just need a
TextBox and a Button and the following code:
Dim COUNTER As
Long
Private Sub Command1_Click()
If Val(Text1.Text) <> -1 Then
COUNTER = COUNTER + Val(Text1.Text)
End Sub |
|
| From: |
Steve |
| Date: |
Thursday, June 5, 2003 at 22:00:10 |
| Comments: |
How do I create a time delay using For...Next?, bit weird I know.
|
| Reply: |
Use Do...Loop statements to make a time delay. Firstly, put a
Timer control onto the Form and then add this code:
Sub Delay(t As
Integer)
Timer1.Interval = t * 1000
Timer1.Enabled = True
Do While Timer1.Enabled
DoEvents
Loop
End Sub
Private Sub Timer1_Timer()
Timer1.Enabled = False
End Sub |
|
| From: |
Jamshid |
| Date: |
Friday, May 23, 2003 at 22:43:37 |
| Comments: |
Can you give me an example of how I can find the EOF
(End of the file)in the loop
Thanks
|
| From: |
Jason |
| Date: |
Sunday, May 18, 2003 at 17:42:02 |
| Comments: |
How would you write a loop statement that would validate at phone number?
I would guess it would be a Do Until statement,
Any help would be greatly appreciated
|
| Reply: |
It might be easier to do this by using a series of Replace functions
e.g.:
|
Dim tn As String
tn = Text1.Text 'TextBox containing phone
number
tn = Trim(tn) 'remove leading and trailing spaces
tn = Replace(tn, "+", "") 'get rid of unwanted characters
tn = Replace(tn, "(", "")
tn = Replace(tn, ")", "")
tn = Replace(tn, " ", "")
tn = Replace(tn, "-", "")
If IsNumeric(tn) Then MsgBox "Valid phone number: " & tn
|
You might also want to check the length of the number using the Len
function.
|
| From: |
karki |
| Date: |
Thursday, May 1, 2003 at 11:54:21 |
| Comments: |
cant understand how to loop
|
| From: |
nash |
| Date: |
Tuesday, April 22, 2003 at 19:47:35 |
| Comments: |
The tutorial is good but do you have a downloadable file for those who are not connected to the net?
|
| Reply: |
You can either copy and paste the above information into an offline
file or another option is to print it out.
|
| From: |
Mint |
| Date: |
Friday, March 28, 2003 at 16:14:19 |
| Comments: |
May i now how to code if i want the code to compare 4 integers till they are in descending order??
|
| From: |
peter |
| Date: |
Sunday, March 23, 2003 at 13:59:59 |
| Comments: |
Could you give me an example of how to use each LOOP
EX:
The DO Loop
Do While Loop
For Next Loop
Also the IF Statement PLEASE and thanks so much.
|
| Reply: |
Do...Loop information can be found on this page (see above).
For...Next information can be found by clicking here.
Information on the If statement can be found by clicking here.
If you need any specific help, please let me know.
|
| From: |
kervin |
| Date: |
Thursday, March 13, 2003 at 11:45:29 |
| Comments: |
i just wanted to know how to write the codes for a loopin visual basic. if for example, i say that if employees earn more than £1, he receives £1 and so an so...
thanks in advance |
| Reply: |
This code should help:
You will need to tell the loop when to stop otherwise it will go on
forever. E.g.:
|
Do Until wage = 50
wage = wage + 1
Loop |
|
| From: |
Thursday, February 27, 2003 at 13:20:01 |
| Date: |
Ahmed |
| Comments: |
hi
i want to know how to do loop form 1 until 10 after that it will start from the begining it like counter
thank you by the way do you now how to do randoms number for math game |
| Reply: |
I think this is the code you want:
|
Dim i As Integer
For i = 1 To 10
If i = 10 Then i = 1
Next i
|
But this code will go on forever?
Here is a way to generate a random number from 1 to 10:
|
Randomize
i = Int(Rnd() * 10) + 1 |
|
| From: |
gee |
| Date: |
Thursday, February 6, 2003 at 08:55:54 |
| Comments: |
hi. thanks |
| From: |
Ha |
| Date: |
Friday, November 29, 2002 at 11:59:39 |
| Comments: |
How can I use Do..Loop Statement to solve this problem? Calculate S when N is given S= 2+4+6+...+N |
| From: |
ali |
| Date: |
Sunday, November 17, 2002 at 20:17:37 |
| Comments: |
thanks for this nice info. |
| From: |
preshan |
| Date: |
Tuesday, November 5, 2002 at 13:28:43 |
| Comments: |
hi,
i'd like to know if i can repeat a command, using the number of times as specified in a textbox, with a loop??? e.g. to add a data 4 times into a database with only one click!!!
Thanks to reply!!! |
| Reply: |
This can be done with the following code:
|
Dim i As
Integer
For i = 1 To
4
'Code in here will loop 4 times
Next i
|
|
Martin Allen 1999 - 2007. Last updated Sunday 20 May 2007 02:00:04 PM +0100.
|