how do you make a number between 10000 and 99999 apear in a text box when you press a button?
Printable View
how do you make a number between 10000 and 99999 apear in a text box when you press a button?
Here you go:
(Sorry about that Gen-X)Code:Public Function GenerateRandom(minVal As Long, maxVal As Long) As Long
intr = -1
maxVal = maxVal + 1
If maxVal > 0 Then
If minVal >= maxVal Then
minVal = 0
End If
Else
minVal = 0
maxVal = 10
End If
Randomize (DatePart("s", Now) + DatePart("m", Now))
Do While (intr < minVal Or intr = maxVal)
intr = CLng(Rnd() * maxVal)
Loop
GenerateRandom = intr
End Function
Usage:
Private Sub Command1_Click()
x = GenerateRandom(10000, 99999)
Text1.text = x
End Sub
[Edited by Matthew Gates on 08-23-2000 at 11:39 PM]
You can't set the datatype of the function to "Long" and then pass it strings
ie GenerateRandom("10000","99999")
It should be
GenerateRandom(10000,99999)
This might save a few CPU cycles...
Code:Function GenRndmStr(minVal As Long, maxVal As Long) As String
Dim Rng As Long, Res as Long
Rng = maxVal - minVal
If Rng <= 0 Then
GenRndmStr = IIF(Rng < 0, "N/A", "0")
Exit Function
End If
Res = Rnd() * Rng
GenRndmStr = Format((minVal + Res), "#,###,###,##0")
End Function
Usage:
Private Sub Command1_Click()
Text1.text = GenRndmStr(10000, 99999)
End Sub
I thought I sent a message last night, saying that I had gotten it, and thank you so much for all the people that helped out, but, all of that code isn't needed, it's just 1 line of code it's
Code:Text1.text = Int( (10000 * Rnd) + 99999) ' Generate random value between 1000 and 99999.
DON'T FORGET Randomize
Put Randomize before you use Rnd otherwise it'll generate the same numbers whenever restarted.
Code:The line you show cannot give you a number between 1000 and 99999 unless the number generated is 0 since you are adding 99999 to the generated number...
There is also a syntax error(100 00 * rnd)
Text1.text = Int( (100 00 * Rnd) + 99999) ' Generate random value between 1000 and 99999
'<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'produce a number between 0 and 98,999 and add 1000 to
'the number produced
Randomize
Text1.Text = Int((98999 * Rnd) + 1000) ' Generate random value between 1000 and 99999