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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

randomly select names from list in table

Status
Not open for further replies.

notreconised

Technical User
Jul 21, 2002
14
0
0
GB
Hi
is it possible to select a name from a table randomly and have them update a text box on aform via a control button
if this is possible can some body provide me with the code to put behind botton.

Many Thanks in Advance
 
This Code was taken from PlanetSource Code, and I changed it around to select a random record from the RecordSource for a Form and then go to that record. You may get an idea from this.

Private Sub cmdRandom_Click()
'' Name: A Non-Repeating Random Number Generator
' Description: With this simple and very fast, routine you can generate a series of non-repeating
' random numbers. You can select a series of 10 numbers, or a series of a million...It doesn't matter.
' Can be useful for image fades, deck shuffling, random tip of the day, etc. - It even tells you how
' long it took to generate the series.

'By: Kevin Lawrence
'
' Inputs:None
' Returns:A popup message stating how many numbers had been mixed up and how long it took.
' Assumes:None
' Side Effects:The larger the series of numbers the more RAM required. Uses arrays.
''Code provided by Planet Source Code(tm) 'as is', without
' warranties as to performance, fitness, merchantability,
' and any other warranty (whether expressed or implied).
'-------------------------------------------------
' ------------
' Produces a series of X random numbers without repeating any
'-------------------------------------------------
' ------------
' 'Results can be used by using array B(X)

''''''''''''''''''Dim A(10000) ' Sets the maximum number to pick
'''''''''''''''''''Dim B(10000) ' Will be the list of new numbers (same as DIM above)
Dim A(100000)
Dim B(100000)
Dim Message, Message_Style, Message_Title, Response, MainLoop, ChosenNumber

' 'Set the original array
Dim MaxNumber As Long, seq As Long, StartTime As Date, EndTime As Date, TotalTime As Date
''''''''''''''MaxNumber = 10000 ' Must equal the DIM above
MaxNumber = Me.RecordsetClone.RecordCount
For seq = 0 To MaxNumber
A(seq) = seq
Next seq

' 'Main Loop (mix em all up)
StartTime = Timer
Randomize (Timer)

For MainLoop = MaxNumber To 0 Step -1
ChosenNumber = Int(MainLoop * Rnd)
B(MaxNumber - MainLoop) = A(ChosenNumber)
A(ChosenNumber) = A(MainLoop)
Next MainLoop
EndTime = Timer

TotalTime = EndTime - StartTime
Message = "The sequence of " + Format(MaxNumber, "#,###,###,###") + " numbers has been" + Chr$(10)
Message = Message + "mixed up in a total of " + Format(TotalTime, "##.######") + " seconds!"
''''''''''''''' I added this next line only
Message = Message + vbCrLf + "Selected Record # is " + CStr(B(200))
'''''''''''''''
Message_Style = vbInformation + vbDefaultButton2
Message_Title = "Sequence Generated"
Response = MsgBox(Message, Message_Style, Message_Title)
'''''''''''''''' I added this to go to the record requested
DoCmd.GoToRecord , , acGoTo, B(200)

End Sub


HTH

PaulF
 
The VBasic code for random numbers is rnd()

if you wished to move to a random number in a recordset on a bound form, you could use this code under a command button.

private sub command0_click()
dim rs as dao.recordset ' set ms dao 3.6 reference
dim nn as long

set rs = me.recordsetclone
rs.movelast
rs.movefirst

nn = rnd(rs.recordcount)
rs.move nn
me.bookmark = rs.bookmark
rs.close
set rs = nothing

Rollie E
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top