• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

Create a random number with now as the seed

hello,
below is the prob plz help.

Dim temp As String
Dim pos As Long
Dim seed As Double

temp = CStr(CDbl(Now))
pos = InStr(1, temp, ".")
seed = CLng(Mid(temp, (pos + 1)))'//i get an error of
                    '//over flow here..can u pls help
                    '//help me i think there is somehting
                    '//wrong with seed variable
                    '//should it be long o string
                    '//can u try the ques on vb6 and
                    '//and correct the error?
Randomize (seed)
'why am i doin this?i need to generate a random number for my access database so that i have a autonumber generated wheneven a record is generated..i need a huge auto number..can u pls help me?
0
R_Thomas
Asked:
R_Thomas
1 Solution
 
mlmccCommented:
Try this

temp = CStr(CDbl(Now))
msgbox temp
pos = InStr(1, temp, ".")
msgbox pos


What values are you getting?

mlmcc
0
 
mlmccCommented:
I just tried it I get

37680.9174421296

Position is 6
so you are trying to load
9174421296

This my also be a rounded versioin since the first time I tried I got

37675.9176666666667

I suspect what you really want is to grab  6 or 7 digits

seed = CLng(Mid(temp, (pos + 1), 6))

mlmcc
0
 
sazhagianambiCommented:
Hi,


    Dim temp As String
    Dim pos As Long
    Dim seed As Double

temp = CStr(CDbl(Now))
pos = InStr(1, temp, ".")
seed = CDbl(Mid(temp, (pos + 1)))

'Here U Use CLng Conversion. So U Get Overflow Error.
' Now it Will Work finely.

Randomize (seed)
MsgBox seed


Regards,
Nambi
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
applayerCommented:
the code below worked fine for me using vb6...

if you need a huge autonumber you might try generating a dummy records yourself and then deleting them.

-----------------------------------------------
Private Sub Form_Load()
Dim temp As String
Dim pos As Long
Dim seed As Double

temp = CStr(CDbl(Now))
pos = InStr(1, temp, ".")
seed = CLng(Mid(temp, (pos + 1))) '//i get an error of
                   '//over flow here..can u pls help
                   '//help me i think there is somehting
                   '//wrong with seed variable
                   '//should it be long o string
                   '//can u try the ques on vb6 and
                   '//and correct the error?
Randomize (seed)
MsgBox seed 'this works find for me using vb6
End Sub
0
 
R_ThomasAuthor Commented:
actually
Dim temp As String
   Dim pos As Long
   Dim seed As Double

temp = CStr(CDbl(Now))
pos = InStr(1, temp, ".")
seed = CDbl(Mid(temp, (pos + 1)))

'Here U Use CLng Conversion. So U Get Overflow Error.
' Now it Will Work finely.

this is the solotion..
which was provided by another guy..but actually u tried harder..ppl like u are the good type cause they try their best to help.
thanks for ur help
thanks for not giving up..
god bless
i also wanan be a man who doesnt give up
0
 
R_ThomasAuthor Commented:
good job nambi but i dont intend to cast it into a dbl
0
 
mlmccCommented:
Good luck.  Glad to help

mlmcc
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now