Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need adjustments to Module and Advice

Status
Not open for further replies.

nancier

MIS
Dec 27, 2004
50
US
The code below generates a seed value and 3 random numbers that appear in the immediate window when the code is run. Instead of having the number 3 hard coded, I'd like to have a form where I could select the number random numbers I'd like to choose and then create a table or query which would hold the Seed number in one column and the random numbers in another column. Then I would create a report so I could have a printout of the results. Anyone know how to do this?

Thanks

Option Compare Database

Function GetRandomBetween(ByVal lngMin As Long, ByVal lngMax As Long) As Long
On Error GoTo ErrHandler

GetRandomBetween = Fix((lngMax - lngMin + 1) * Rnd + lngMin)

ExitHere:
Exit Function
ErrHandler:
Debug.Print Err, Err.Description
Resume ExitHere
End Function

Function SeedRandomizer() As Long
Dim lngSeed As Long
Const MIN = 1
Const MAX = 2147483647

'this should do it
Randomize Timer()

'get a random seed value between min and max
lngSeed = GetRandomBetween(MIN, MAX)

'get a random sign value
If GetRandomBetween(1, 2) = 1 Then
lngSeed = -lngSeed
End If

'randomize using the new seed
Randomize lngSeed

'return the seed value
SeedRandomizer = lngSeed
End Function

'CODE
Sub Get3Randoms()
Dim i As Integer

Debug.Print "Seed Value: " & SeedRandomizer

For i = 1 To 3
Debug.Print "Random " & i & ": " & GetRandomBetween(1, 9999999)
Next i
End Sub

 
Thanks, I changed it to this. Not sure if it works but now need to know how to go about getting the results into a table or query. Anyone know how?

Thanks

Option Compare Database

Function GetRandomBetween(ByVal lngMin As Long, ByVal lngMax As Long) As Long
On Error GoTo ErrHandler

GetRandomBetween = Fix((lngMax - lngMin + 1) * Rnd + lngMin)

ExitHere:
Exit Function
ErrHandler:
Debug.Print Err, Err.Description
Resume ExitHere
End Function

Function SeedRandomizer() As Long
Dim lngSeed As Long
Const MIN = 1
Const MAX = 2147483647

'this should do it
Randomize Timer()

'get a random seed value between min and max
lngSeed = GetRandomBetween(MIN, MAX)

'get a random sign value
If GetRandomBetween(1, 2) = 1 Then
lngSeed = -lngSeed
End If

'randomize using the new seed
Randomize lngSeed

'return the seed value
SeedRandomizer = lngSeed
End Function

'CODE
Sub GetRandoms(lngRandomNumber As Long)
Dim i As Integer
For i = 1 To lngRandomNumber & i & GetRandomBetween(1, 9999999)
Next i
End Sub

 
Do a search on how to add results to a table.

It works this way:

You have a function, you have a variable you want to pass to the function, and you have the result of the function

fn(varbiable) = result or
where fn = "SeadRandmomizer", variable = "lngRandomNumber" and result = "Long"

Function SeedRandomizer(lngRandomNumber as long) As Long

Mark P.
Providing Low Cost Powerful Point of Sale Solutions.
 
nancier, do you want a new record for each random number, or a column?
Here's a column, you'll probably be able tyo modify accordingly, if it's the other way around...

Code:
Sub GetRandoms(lngRandomNumber As Long)
Dim x As Integer

DoCmd.RunSQL "CREATE TABLE tblRandoms (txtRandom1 INTEGER)"

For x = 2 To lngRandomNumber 
DoCmd.RunSQL "ALTER TABLE tblRandoms ADD(txtRandom & x INTEGER)"
Next x

For x = 1 To lngRandomNumber 
If x = 1 Then
SQL = "INSERT INTO tblRANDOMS (txtRandom & x) VALUES(GetRandomBetween(1, 9999999))
Else
SQL = "UPDATE tblRandom SET txtRandom & x =  GetRandomBetween(1, 9999999)"
End If
 

DoCmd.RunSQL SQL

Next x

This code was not thoroughly checked for syntax & logic, but the principle should give you a viable option.

Good luck either way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top