Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help more with Do-Loop

Status
Not open for further replies.

emcc

Programmer
Oct 12, 2001
124
US
In my effort to understand Do-Loops, I've written the following code. The form has two arrays txtInput and lblOutput. The objective is to have the user input five numbers and have code arrange the numbers in ascending order and display them in the output label array. I must be missing something because the only output I'm getting is the lowest number in the output array. Can anyone help?

Here's my code.......

Option Explicit
Dim TopIndex As Integer
Dim OtherTopIndex As Integer
Dim NextItem As Integer
Dim LoopCount As Integer


Private Sub cmdArrange_Click()
NextItem = 0

Do While NextItem < 4
TopIndex = 4

Do While TopIndex > NextItem

If Val(txtInput(TopIndex)) < Val(txtInput(TopIndex - 1)) Then
lblOutput(OtherTopIndex) = Val(txtInput(TopIndex))
Else: lblOutput(OtherTopIndex) = Val(txtInput(TopIndex - 1))
End If

TopIndex = TopIndex - 1
Loop

NextItem = NextItem + 1
Loop



End Sub


 
When I did this a few weeks ago in my Beginning VB class, I first used 3 If statements...
Private Sub Ascend_Click()
Form2.N1 = N1
Form2.N2 = N2
Form2.N3 = N3
Form2.N4 = N4

For X = 1 To 3
If Val(N1) > (N2) Then
temp = N2
N2 = N1
N1 = temp
End If
If Val(N2) > (N3) Then
temp = N2
N2 = N3
N3 = temp
End If
If Val(N3) > (N4) Then
temp = N4
N4 = N3
N3 = temp
End If
Next X

Form2.TA = &quot;Sorts Ascending to:&quot;

Form2.A1 = N1
Form2.A2 = N2
Form2.A3 = N3
Form2.A4 = N4

Form1.Hide
Form2.Show
End Sub
Now, with this, there are two forms. The first has 1 row of for labels, along with the command button which this code goes behind. On the second form, there are two rows of 4 labels. The first row being N1-N4 (not an array) and the second being A1-A4. If you need more explanation, please ask...
And for all of those experienced people out there reading my post, I am just a beginner who actually saw someone I could sort-of help out...
 
This is somewhat like other examples I have seen but I never fully understood it. The one part I don't understand is in your If statements- can you explain the third line of this bit of your code (N2 = N1). What exactly is this doing?

If Val(N1) > (N2) Then
temp = N2
N2 = N1
N1 = temp

Thanks!
 
Hmmmmmmmmmmmm,

Some &quot;response&quot;? But - they do not address your 'issue' of loops? DO[/u] they?


Well, Mine is NOT the complete answer, but a bit of a teaser. It does SHOW you the Error of your DO's, but you have additional problems -if you want to be better than 'pretty good'.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Sigh ... not in the budget ... water, water everwhere ... not even cheap wine for the time.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Sorry I didn't get back to you yesterday, life you know...Anyways, to answer your question about what that line is doing...it's switching the numbers inputed by the user ONLY if the number to the left of it is greater than it. And, I kow I didn't answer the Do Loop question, but just thought that here is a different solution to the same problem. Also, here's another piece of code that does the same thing, only the code is a little shorter. Another thing about what I am bout to put up, it only displays(rearranges) the inputed numbers inside of the text boxes that the user used, but that would be easy to manipulate to get them to display in Labels elsewhere. Here it is:

Private Sub Ascending_Click()
For y = 1 To 3
For x = 0 To 2
If Val(N(x)) > Val(N(x + 1)) Then
temp = N(x)
N(x) = N(x + 1)
N(x + 1) = temp
End If
Next x
Next y
End Sub

If you have anymore questions, please ask...and for all of you experts out there, please understand that I'm just a beginner here trying to help someone out! :)
 
Actually......I continued to work on my project and ended up with code almost exactly like yours! Isn't there a saying, something about great minds...... :)

Thanks for all of your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top