-
Hi,
I use the following code to load random numbers, from -12 to +12, into
an array of text boxes when my form loads. 0 is excluded - it never loads into a textbox.
Private Sub Form_Load()
Dim iCounter As Integer
For iCounter = 0 To 7
Text1(iCounter) = 0
Randomize
Do Until Text1(iCounter) <> 0
Text1(iCounter) = Int((23 * Rnd) - 10)
Loop
Next
End Sub
I've been trying for ages to adjust it a little bit. For numbers greater than 0 I want the "+" character to appear before those numbers. E.g: instead of "2" in a text box, it will be "+2".
I tried things like:
Do Until Text1(iCounter) <> 0
If Text1(iCounter) > 0 Then
Text1(iCounter) = "+" & Int((23 * Rnd) - 10)
Loop
Next
but had no luck. Any ideas?
Thanks.
-
VAL
Don't know if I can help but a text box value is a string. Perhaps if you use the val() function which converts to numeric you might get some desired results.
-
Try,
Code:
Do Until Cint(Text1(iCounter).Text) <> 0
If Cint(Text1(iCounter).Text) > 0 Then
Text1(iCounter).Text = "+" & Int((23 * Rnd) - 10)
Loop
-
Try this
Code:
Private Sub Form_Load()
Dim iCounter As Integer
Dim intNum as Integer
For iCounter = 0 To 7
intNum = 0
Randomize
Do Until intNum <> 0
intNum = Int((23 * Rnd) - 10)
Loop
Text1(iCounter) = IIF(intNum > 0,"+","-") & intNum
Next
End Sub
-
Try this instead:
Code:
Private Sub Form_Load()
Dim iCounter As Integer
Dim iValue As Integer
For iCounter = 0 To 7
iValue = 0
Randomize
Do Until iValue <> 0
iValue = Int((23 * Rnd) - 10)
Loop
If iValue > 0 Then
Text1(iCounter).Text = "+" & iValue
Else
Text1(iCounter).Text = iValue
End If
Next
End Sub
-
Opps! Use bsmith's it's better!
PS.
Anybody know why we can't Edit a response anymore?
It kind of aggrevating.
-
True
BSmiths is better.
But we are back on to the dodgy topic of IIF statements. Which take a lot of time.
Seeing as if it is a negative number you don't have to insert a sign it would just be better to test for > 0
Code:
Private Sub Form_Load()
Dim iCounter As Integer
Dim intNum as Integer
For iCounter = 0 To 7
intNum = 0
Randomize
Do Until intNum <> 0
intNum = Int((23 * Rnd) - 10)
Loop
If intNum > 0 Then
Text1(iCounter) = "+" & intNum
Else
Text1(iCounter) = intNum
End If
Next
End Sub