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.
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
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
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.
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.
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.
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
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
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.
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:
Do
wage = wage + 1
Loop
You will need to tell the loop when to stop otherwise it will go on
forever. E.g.:
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:
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 - 2011. Last updated
Monday 08 August 2011 07:21:51 PM +0100.