Choose a Name at Random

e_loveall

New Member
Joined
Aug 29, 2002
Messages
35
I found what I was looking for in Excel on this topic, but I wonder if it transfers to Access...

We keep track of all our program participants in Access tables. I was wondering if there is code that will randomly choose a name from the table, like picking a name out of a hat. It would have to choose both the first name and corresponding last name, which are in separate fields.

Thanks!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
OK, here is one way. I recreated a example.

I assume that you have a table named "tblNames" with two fields:
1. ID
2. Name

The "ID" field is a counter, so the first record is 1, the second is 2, etc. with no gaps.

The "Name" field is the name of each person

Then create a new query named "qryPickOne". Add the "tblNames" table and the fields to this query. This is the query that we will use select a random person.

Then create a form and add a command button to the form. Name the command button "cmdPickRandom". To the OnClick property of this button, add the following VBA code:
Code:
Private Sub cmdPickRandom_Click()

    Dim myCount As Long
    Dim myRandomNumber As Long
    Dim mySQL As String
    
'   Enter number of records
    myCount = 5
'   Calculate random number
    myRandomNumber = Int(Rnd * myCount) + 1
'   Build SQL string to pull number
    mySQL = "SELECT tblNames.* FROM [tblNames] WHERE (((tblNames.ID)=" & myRandomNumber & "));"
'   Attach SQL code to query
    CurrentDb.QueryDefs("qryPickOne").SQL = mySQL
'   Open Query
    DoCmd.OpenQuery "qryPickOne", acViewNormal, acEdit
   
End Sub
Now, whenever you click this command button it will select a random person via the query, open the query, and display your result.

Note that manually hard-coded the number of records in the VBA code above ("5"). If you like, we can also get this number via VBA code also.

Modify as needed.
 
Upvote 0

Forum statistics

Threads
1,221,819
Messages
6,162,155
Members
451,749
Latest member
zack_ken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top