Random generated query (1 Viewer)

jbeasley

Registered User.
Local time
Today, 14:00
Joined
Jan 11, 2001
Messages
14
Is there a way to use a query to build a random generator from a list of things in a table. Example: Have a button that is a 6 sided dice. Use a button to randomly roll the dice and come up with a value of 1 to 6?

Any ideas please help?
Thanks
 

Fornatian

Dim Person
Local time
Today, 14:00
Joined
Sep 1, 2000
Messages
1,396
Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value

Private Sub btnRollTheDice_Click()
Randomize
Me.YourFieldName = Int((6 - 1 + 1) * Rnd + 1)
End Sub

For more info on the Rnd function put the cursor on the word rnd and hit F1.

Ian
 

Fornatian

Dim Person
Local time
Today, 14:00
Joined
Sep 1, 2000
Messages
1,396
If you're looking to extrapolate a random field value from a table then use the same principle like:

Randomize

Dim lngSearchNum As Long
Dim i As Integer
Dim MyDb As Database
Dim rstMyTable As Recordset
Set MyDb = CurrentDb()
Set rstMyTable = MyDb.OpenRecordset("TableName")
lngSearchNum = Int((rstMyTable.RecordCount - 1 + 1) * Rnd + 1) - 1
With rstMyTable
.MoveLast
.MoveFirst
.Move (lngSearchNum)
End With
Me!DestinationField = rstMyTable("FieldName")
MyDb.Close
Set rstMyTable = Nothing
Set MyDb = Nothing

worked on my PC anyway, however as I recall there is a problem with generating random numbers in Access so this may not be entirely reliable - sorry I can;t be more specific, maybe others knwo more.

Ian
 

Users who are viewing this thread

Top Bottom