jbeasley
08-03-2001, 11:00 AM
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
08-04-2001, 08:22 AM
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
08-04-2001, 08:44 AM
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