-
Hi there,
I'm sure there's a funkier, shorter, more professional way of doing my quest below, so I'd be grateful if you could let me in on the secret.
Basically, there are 6 textboxes. In textboxes 1 to 5, random numbers from 0 to 20 are loaded on form load event.
Then, when command1 is clicked, textbox6 will say, 'textbox1 is the highest number' (or whichever textbox happens to be the highest).
I'd be very grateful for any help!
Private Sub Form_Load()
Randomize
Text1 = Int(Rnd * 20)
Text2 = Int(Rnd * 20)
Text3 = Int(Rnd * 20)
Text4 = Int(Rnd * 20)
Text5 = Int(Rnd * 20)
End Sub
Private Sub Command1_Click()
If Val(Text1) > Val(Text2) Then
If Val(Text1) > Val(Text3) Then
If Val(Text1) > Val(Text4) Then
If Val(Text1) > Val(Text5) Then
Text6 = "textbox1 is the highest"
End If
End If
End If
End If
If Val(Text2) > Val(Text1) Then
If Val(Text2) > Val(Text3) Then
If Val(Text2) > Val(Text4) Then
If Val(Text2) > Val(Text5) Then
Text6 = "textbox2 is the highest"
End If
End If
End If
End If
If Val(Text3) > Val(Text1) Then
If Val(Text3) > Val(Text2) Then
If Val(Text3) > Val(Text4) Then
If Val(Text3) > Val(Text5) Then
Text6 = "textbox3 is the highest"
End If
End If
End If
End If
If Val(Text4) > Val(Text1) Then
If Val(Text4) > Val(Text2) Then
If Val(Text4) > Val(Text3) Then
If Val(Text4) > Val(Text5) Then
Text6 = "textbox4 is the highest"
End If
End If
End If
End If
If Val(Text5) > Val(Text1) Then
If Val(Text5) > Val(Text2) Then
If Val(Text5) > Val(Text3) Then
If Val(Text5) > Val(Text4) Then
Text6 = "textbox5 is the highest"
End If
End If
End If
End If
End Sub
-
For a start, change all those textboxes to a control array, by changeing their names to:
Text(1), Text(2)...etc
and then you can use loops to go through them one by one
for example the first 'paragraph' of code would look like this
Code:
Private Sub Form_Load()
Randomize
Dim I as integer
for I = 1 to 5
Text(I) = Int(rnd * 20)
next i
End Sub
Its up to you to do the rest :)
-
Keeping with the control array you can shortan your code to this
Code:
Private Sub Comand1_Click()
Dim i As Integer
Dim Retval as Integer
Retval = 1
For i = 2 To 5
If Cint(Text(i).Text) > CInt(Text(Retval).Text) Then
Retval = i
Next i
MsgBox "TextBox " & Retval & " Is The Highest."
End Sub
hope this helps
-
///
Sam:
Seems you forgot about ties..ie..if box 1,3,4
all have 19 and 2, 5, have 10, 13 ????
Which is the greatest?
Weird stuff...looked at it for a bit but it's beyond
me...I think you would have to load the random numbers
into a collection and if similar you would have to reject
them and thus load the text boxes with unique values thus never allowing duplicate values.
-
My Code Just Does what the code up the Top Does, It Doesn't take into account numbers may be the same, if they are it just takes the lowest numbered textbox.
the best way to avoid that would be to if you wanted to take into account repeats then you could modify it to
[code]Private Sub Comand1_Click()
Dim i As Integer
Dim Retval as Integer
Dim collRepeats As New Collection
Dim strTemp As String
Dim varTemp as Variant
Retval = 1
For i = 2 To 5
Select Case Cint(Text(i).Text)
Case Is > CInt(Text(Retval).Text)
Retval = i
Set collRepeats = New Collection
Case Is = CInt(Text(Retval).Text)
collRepeats.Add i
End Select
Next i
If collRepeats.Count Then
For Each varTemp In collRepeats
StrTemp = strTemp & ", TextBox " & CStr(VarTemp)
Next varTemp
Msgbox "There Was a Tie" & strTemp & " and TextBox " & CStr(Retval) & " Are the Highest"
Else
MsgBox "TextBox " & Retval & " Is The Highest."
End If
End Sub