-
Jun 10th, 2017, 09:34 AM
#1
Thread Starter
Banned
[RESOLVED] plz help, how can i loop this.
Hi vb masters,
I need to change the value of text2 as per following condition :-
Code:
If Text1.Text = 0.05 Then Text2.Text = 1
If Text1.Text = 1.05 Then Text2.Text = 1
If Text1.Text = 2.05 Then Text2.Text = 1
If Text1.Text = 3.05 Then Text2.Text = 1
I want to loop this condition from 0.05 to 999.05, how can I do it. Plz help me on the way.
Regards.
-
Jun 10th, 2017, 09:38 AM
#2
Re: plz help, how can i loop this.
Simple
Code:
Dim X As Currency
For X = .05 To 999.05
If text1.Text = X Then text2.Text = "1"
Next
-
Jun 10th, 2017, 09:48 AM
#3
Thread Starter
Banned
Re: plz help, how can i loop this.
Thanks a lot DataMiser. It works fine. [Resolved]
-
Jun 10th, 2017, 03:33 PM
#4
Re: [RESOLVED] plz help, how can i loop this.
It is better to add Exit For to speed up the program, because no need to continue the loop after found that Text1.Text = X
Code:
Dim X As Currency
For X = 0.05 To 999.05
If Text1.Text = X Then
Text2.Text = "1"
Exit For
End If
Next
-
Jun 10th, 2017, 04:02 PM
#5
Re: [RESOLVED] plz help, how can i loop this.
True. Exit for would be highly recommended in such a case.
I would have mentioned that as well had I been thinking. Instead I just duplicated the code shown but in a loop
You could also add a little code to reduce the length of the loop more for high values by changing the starting value of x.
For example if text1.text>500 then start x at 500.05 which would relieve the first 500 iterations through the loop.
-
Jun 10th, 2017, 04:35 PM
#6
Re: [RESOLVED] plz help, how can i loop this.
Originally Posted by DataMiser
You could also add a little code to reduce the length of the loop more for high values by changing the starting value of x.
For example if text1.text>500 then start x at 500.05 which would relieve the first 500 iterations through the loop.
Good point, but i suggest starting the loop with CInt(Text1.Text) + 0.05
Code:
For X = CInt(Text1.Text) + 0.05 To 999.05
by this way the loop will iterate only 1 if Text1.Text = X, and (999 - Text1.Text) if not
-
Jun 10th, 2017, 08:08 PM
#7
Re: [RESOLVED] plz help, how can i loop this.
How about this instead of a loop:
Code:
Select Case CDbl(Text1.Text)
Case 0.05 To 999.05
If Right(Text1.Text, 2) = "05" Then
Text2.Text = 1
else
Text2.Text = 0
End If
Case Else
Text2.Text = 0
End Select
Last edited by SamOscarBrown; Jun 10th, 2017 at 08:13 PM.
-
Jun 10th, 2017, 09:05 PM
#8
Re: [RESOLVED] plz help, how can i loop this.
Originally Posted by SamOscarBrown
How about this instead of a loop:
Code:
Select Case CDbl(Text1.Text)
Case 0.05 To 999.05
If Right(Text1.Text, 2) = "05" Then
Text2.Text = 1
else
Text2.Text = 0
End If
Case Else
Text2.Text = 0
End Select
Your code will validate many incorrect values, e.g. 123.45605, 123.999905 etc...
Try the following instead
Code:
Text2.Text = 0
Select Case CDbl(Text1.Text)
Case 0.05 To 999.05
If Int(Text1.Text) + 0.05 = CDbl(Text1.Text) Then
Text2.Text = 1
End If
End Select
-
Jun 10th, 2017, 09:33 PM
#9
Re: [RESOLVED] plz help, how can i loop this.
Yup...that's one way to avoid incorrect currency entries...of course there are others (which should be done at actual entry time, whether it is from an external file/db/etc or input with the keyboard). OP Might want to check to see if the entry is a valid currency value (like using FormatCurrency() function, limiting number of numbers after decimal to 2. Or can use the Istr() function to find where the decimal resides and make sure only two values have been entered after that.
My whole point was it seemed strange to want to loop through anything (even though those above suggestions could work in some situations). OP doesn't give up much to go on, so I guess any guidance is good.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|