Link to home
Start Free TrialLog in
Avatar of Hares Fak
Hares Fak

asked on

How Do i Generate Random Numbers For A Access Column Using VB

I'd like each column to go through the loop once.

It's a currency column. I'd like the VB code, it's for a basic project.
Avatar of PatHartman
PatHartman
Flag of United States of America image

Here is a sub I use to draw a raffle winner.  The key is seed the randomizer before running the query.  People usually just use Now() since it changes quickly enough to come up with a number different from the one you used the last time.  If you keep using the Rnd() function without reseeding the rendomizer, you just generate the same set of random numbers over and over again.
Private Sub cmdDrawName_Click()
On Error GoTo Err_cmdDrawName_Click

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim qd As DAO.QueryDef
    Dim RecCount As Integer
    
    Set dbs = CurrentDb
    Randomize (Now())
    Set qd = dbs.QueryDefs!qRandNum
    qd.Parameters![Enter Date] = Me.txtToday
    Set rst = qd.OpenRecordset

    If Not rst.EOF Then
        Me.txtDrawName = rst!FullName
        rst.Edit
        rst!ChosenCount = rst!ChosenCount + 1
        rst.Update
    Else
        Me.txtDrawName = "Change Meeting Date"
    End If
    
    Set rst = Nothing
    Set qd = Nothing
Exit_cmdDrawName_Click:
    Exit Sub

Err_cmdDrawName_Click:
    MsgBox Err.Number & "-" & Err.Description
    Resume Exit_cmdDrawName_Click
    
End Sub

Open in new window


Here's the query.  It selects just the lowest random number.  You can remove the top 1 if you want all records returned.

SELECT TOP 1 M.MailingListID, M.FirstName & " " & M.LastName AS FullName, Rnd(M.MailingListID) AS RandNum, A.ChosenCount
FROM tblMailingList AS M INNER JOIN tblAttendance AS A ON M.MailingListID=A.MailingListID
WHERE (((A.ChosenCount)<1) AND ((A.AttendanceDate)=[Enter Date]))
ORDER BY Rnd(M.MailingListID);
I've written an analysis of the VB/VBA random functions.
https://www.experts-exchange.com/articles/11114/An-Examination-of-Visual-Basic's-Random-Number-Generation.html

each column to go through the loop once
You can create a simple function to return a random number and use this in a query.  The key is that you need to pass something (not static) into the function as a parameter.
Public Function GetRand(ByVal parmDummy) As Single
    GetRand = Rnd
End Function

Open in new window

You can invoke this function in a query.
Example:
Update mytable
Set colA = GetRand(mytable.ID),
      colB = GetRand(mytable.ID)

Open in new window

If you just want a recordset with a random number for each record, use a query like this where ID is the primary key of the table:

SELECT *, Rnd(-Timer()*[ID]) AS [Random Key] 
FROM YourTable;

Open in new window

And to generate a random sorting:

SELECT *, Rnd(-Timer()*[ID]) AS [Random Key] 
FROM YourTable
ORDER BY Rnd(-Timer()*[Random Key]);

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.