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.
It's a currency column. I'd like the VB code, it's for a basic project.
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
Example:
https://www.experts-exchange.com/articles/11114/An-Examination-of-Visual-Basic's-Random-Number-Generation.html
each column to go through the loop onceYou 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
You can invoke this function in a query.Example:
Update mytable
Set colA = GetRand(mytable.ID),
colB = GetRand(mytable.ID)
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;
And to generate a random sorting:SELECT *, Rnd(-Timer()*[ID]) AS [Random Key]
FROM YourTable
ORDER BY Rnd(-Timer()*[Random Key]);
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers 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.
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.MailingL
WHERE (((A.ChosenCount)<1) AND ((A.AttendanceDate)=[Enter
ORDER BY Rnd(M.MailingListID);