Goto Random record in forms (1 Viewer)

Gismo

Registered User.
Local time
Today, 09:30
Joined
Jun 12, 2017
Messages
1,298
Hi all,

Any reason why below code wont take me to a random record when opening a form?
Code:
Private Sub Form_Load()
DoCmd.OpenForm "Companyvalues"
DoCmd.GoToRecord acDataForm, "Companyvalues", acGoTo, Rnd
End Sub
 

jdraw

Super Moderator
Staff member
Local time
Today, 02:30
Joined
Jan 23, 2006
Messages
15,379
What does this code actually do? What is the result of executing this proc?
 

plog

Banishment Pending
Local time
Today, 01:30
Joined
May 11, 2011
Messages
11,646
What value does Rnd hold? That's a trick question--As you have it now its a variable that contains no value.

There is the Rnd() function (https://www.techonthenet.com/access/functions/numeric/rnd.php). Using that will get you closer to working. Check the link for an example of how to use it.

However, just replacing Rnd with Rnd() will not always work. If you check out the link it shows how to limit the output of Rnd(). You need to do that, because Rnd() might return a number outside the number of records you have and then the form won't be able to go to that record.
 

Gismo

Registered User.
Local time
Today, 09:30
Joined
Jun 12, 2017
Messages
1,298
I want to open a form with out company values but must go to a random record every time a user opens the DB
 

jdraw

Super Moderator
Staff member
Local time
Today, 02:30
Joined
Jan 23, 2006
Messages
15,379
I think Rnd will give you a random number between 0 and 1 (my guess).
Since Offset will be an integer/long integer you will need to Fix(rnd) to get a real record offset.
You might want to create a Function to get a random number between 1 and RecordCount of the underlying recordsource of the form.

I found the following to work on one of my forms.

Code:
Private Sub Form_Load()
DoCmd.OpenForm "frmMemo"
DoCmd.GoToRecord acDataForm, "frmMemo", acGoTo, randomNumber(1, DCount("*", "tblMemo"))
End Sub
where this is my Random number function.

Code:
'---------------------------------------------------------------------------------------
' Procedure : randomNumber
' Author    : Jack
' Created   : 11/18/2010
' Purpose   : To Generate Random numbers between and including a range of numbers.
'Lo and Hi are the lowest and highest random numbers you wish to generate.
'
'The Randomize keyword is critical to getting different results for each Access session.
'=======================================================================================
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'------------------------------------------------------------------------------
'
Function randomNumber(Lo As Long, Hi As Long) As Long
10       On Error GoTo randomNumber_Error

20    Randomize
30    randomNumber = Int((Hi - Lo + 1) * Rnd + Lo)

40       On Error GoTo 0
randomNumber_Exit:
50       Exit Function

randomNumber_Error:

60        MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure randomNumber of Module AccessMonster"
70        GoTo randomNumber_Exit
           
End Function
 
Last edited:

Gismo

Registered User.
Local time
Today, 09:30
Joined
Jun 12, 2017
Messages
1,298
Sorry if i sound silly but how do I create a new function?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:30
Joined
Feb 19, 2002
Messages
43,266
I want to open a form with out company values but must go to a random record every time a user opens the DB
The requirement doesn't make sense. Wouldn't it be better to open the form to an empty record and use a combo or search field to choose a specific record. I can't imagine an app where users actually work on random records but perhaps you can enlighten us.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:30
Joined
Feb 28, 2001
Messages
27,179
What's even more interesting is that you never know the order of records in a table anyway because that is fluid. The order of records in a query based on an ORDER BY clause is predictable, but for a raw table, this order can theoretically change from one minute to the next in a shared environment. Not to mention that a given random sequence COULD cough up the same number twice in a row. For a large number of records it is not likely to happen but for smaller table sizes, the odds of duplication of selection are reasonably high.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 07:30
Joined
Feb 19, 2013
Messages
16,610
Sorry if i sound silly but how do I create a new function?
you just write it, either in the form module or a general module if you want to use it in multiple forms/reports or queries. function name cannot have spaces and is usually given a sensible name when called

Just copy and paste JDraws randomnumber function, though you may have to delete the numbering
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:30
Joined
May 21, 2018
Messages
8,527
I written about this on this thread before. You need to use something like JDRAW posted.
If you do not use the Randomize before calling Rnd() you get a random number, but the sequence is not random. There is a reason for this because most real analysis requires you to repeat the random process. In other words you might get a sequence like 7,5,6,9,2,7,10...., but the next time you open the DB you will get the same sequence. Rnd gives a number between 0 and 1 so you goto code will not go to record .7226
 

Gismo

Registered User.
Local time
Today, 09:30
Joined
Jun 12, 2017
Messages
1,298
I have copied both codes but get an error in the below code.

Code:
Private Sub Form_Load()
DoCmd.OpenForm "CompanyValues"
DoCmd.GoToRecord acDataForm, "CompanyValues", acGoTo, randomNumber(1, DCount("*", "Values"))
End Sub
 

Gismo

Registered User.
Local time
Today, 09:30
Joined
Jun 12, 2017
Messages
1,298
just managed to get it working, tnx
 

Users who are viewing this thread

Top Bottom