|
-
Apr 3rd, 2000, 06:57 PM
#1
Thread Starter
Lively Member
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.
-
Apr 3rd, 2000, 07:50 PM
#2
Lively Member
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.
-
Apr 3rd, 2000, 07:56 PM
#3
Fanatic Member
Try,
Code:
Do Until Cint(Text1(iCounter).Text) <> 0
If Cint(Text1(iCounter).Text) > 0 Then
Text1(iCounter).Text = "+" & Int((23 * Rnd) - 10)
Loop
-
Apr 3rd, 2000, 07:57 PM
#4
Lively Member
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
-
Apr 3rd, 2000, 08:00 PM
#5
Addicted Member
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
-
Apr 3rd, 2000, 08:04 PM
#6
Addicted Member
Opps! Use bsmith's it's better!
PS.
Anybody know why we can't Edit a response anymore?
It kind of aggrevating.
-
Apr 3rd, 2000, 08:16 PM
#7
Fanatic Member
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
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
|