Solved

# Create a random number with now as the seed

Posted on 2003-02-28
Medium Priority
439 Views
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
Question by:R_Thomas
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 101

Expert Comment

ID: 8046243
Try this

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

What values are you getting?

mlmcc
0

LVL 101

Accepted Solution

mlmcc earned 60 total points
ID: 8046263
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

LVL 1

Expert Comment

ID: 8046685
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

Expert Comment

ID: 8046789
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.

-----------------------------------------------
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

Author Comment

ID: 8047080
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

Author Comment

ID: 8047085
good job nambi but i dont intend to cast it into a dbl
0

LVL 101

Expert Comment

ID: 8047823

mlmcc
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month13 days, 10 hours left to enroll