Number comparison from a list

George Too

Registered User.
Local time
Today, 11:05
Joined
Aug 12, 2002
Messages
198
Hi all,

The code below works some times but not others. Is there anything wrong with the sintax?

For Each control In subfrmRepeats.Controls
If control.ControlType = acTextBox Then
Num = control.OldValue
Smallest = Num
If Num < Smallest Then
Smallest = Num
End If
End If
Next control

My numbers are small decimals (12.9508; 11.2500, etc)

Thanks for your input,

George
 
Step through your logic. Unless there's something additional that we aren't seeing, it would seem that the code will never work as you envision.
Code:
For Each control In subfrmRepeats.Controls 
   If control.ControlType = acTextBox Then 
      Num = control.OldValue
      'This line assigns value of Num to Smallest
      Smallest = Num
      'So there's no way that Num will be < Smallest
      If Num < Smallest Then 
         Smallest = Num 
      End 
   End If 
Next control
 
Hi raskew,

Like I said before, the code does work most of the time. I just haven't figure out what are the conditions when it doesn't.

I modified the code from the code below which does work beautifully:

****************************
Private Sub cmdSmallest_Click()
Dim iSmallest As Integer ' Smallest number so far
Dim iNum As Integer ' Number just read in

' Get first number from user
iNum = InputBox("Enter a number", "Input window")
iSmallest = iNum

' As long as the user does not enter -1, continue reading numbers
Do While iNum <> -1

' If number just read is smaller, update smallest
If iNum < iSmallest Then
iSmallest = iNum
End If

' Read next number from user
iNum = InputBox("Enter next number", "Input window")
Loop

' Display smallest number on lblDisplay.
lblDisplay.Caption = "Smallest Number entered is: " & iSmallest
End Sub
****************************

Now, what is it that you think is not logical, maybe I'm not seeing it.

Thanks for your time.

George.
 
George,
Your last code is fine,

What raskew was pointing out is that in your original code
you have a logic error:

For Each control In subfrmRepeats.Controls
If control.ControlType = acTextBox Then
Num = control.OldValue
' You ALWAYS set Smallest = Num, get rid of next line
Smallest = Num
'So there's no way that Num will be < Smallest
If Num < Smallest Then
Smallest = Num
End
End If
Next control

Wayne
 
Hi all,

WayneRyan, regarding your post, I realize what you're saying, but I do need that line of code to hold the value I am testing against the new value. The only thing is that it is in the wrong place. If you take a look at the code for Private Sub cmdSmallest_Click(), that line of code is outside the loop. Needless to say, I have not figure out how to imitate that loop. The Private Sub cmdSmallest_Click() code works fine because the user is providing the numbers as the code asks for them but how do I create that loop whith "For Each control In subfrmRepeats.Controls"? I have tried several ways and still can't get it to work.

Can you help me restructure the whole thing?

Thanks for your replies,

George
 
Try this:

Set variable Smallest to a ridiculously high number so that it will be replaced by the first textbox value, then you do not need to make Smallest = Num for the first instance.



Smallest = 1000000000
For Each control In subfrmRepeats.Controls
If control.ControlType = acTextBox Then
Num = control.OldValue
If Num < Smallest Then
Smallest = Num
End If
End If
Next control


HTH
 
Thanks Harry, my problem was that I had my variable = 0 (that was part of the problem). Thank you, code is working just fine now.

Thank you all for your replies.

George
 

Users who are viewing this thread

Back
Top Bottom