I am trying to randomize my filter. I have a table with a record that has a possible value of "Easy", "Medium", or "Hard".
The user makes a choice by a combo box. After it is chosen I activate the Filter property like so
Private Sub cboDifficulty_Click()
Dim strFilterDifficulty As Variant
strFilterDifficulty = Forms!frmGuess.cboDifficulty
Me.Filter = "Word_Difficulty = '" & strFilterDifficulty & "'"
Me.FilterOn = True
Me.txtAnswer = Word_Description
' get the length of the word
Call WordLength(Me.txtAnswer)
End Sub
This filters my recordset based on the combo box value and starts at the 1st occurance. It always appears in the same order obviously. What I would like to do is randomize the order each time.
Any ideas?
16 2026
Hmm... what about a query that creates a new column that just returns a random number from 1 to... let's say 32,767 and then sort by that? Then you can just use that query as the recordsource for the form.
Hi, there.
The most obvious idea is to add a field to the source table and fill it with random values before applying filter. Like the following. -
Public Function GetRandomValue(varDummy As Variant) As Long
-
GetRandomValue = (Rnd - 0.5) * 4000000000#
-
End Function
-
-
UPDATE [Your table] SET [Your table].lngRand = GetRandomValue([Any table field]);
-
Regards,
Fish
Query doesn't seem to work with randomizing numbers. Well, you could always create the column in the table and then use a recordset loop to randomize the numbers.
Hi, there.
The most obvious idea is to add a field to the source table and fill it with random values before applying filter. Like the following. -
Public Function GetRandomValue(varDummy As Variant) As Long
-
GetRandomValue = (Rnd - 0.5) * 4000000000#
-
End Function
-
-
UPDATE [Your table] SET [Your table].lngRand = GetRandomValue([Any table field]);
-
Regards,
Fish
I tried something similar to this, it just kept returning the same number. I think you have to go with a recordset loop.
Query doesn't seem to work with randomizing numbers. Well, you could always create the column in the table and then use a recordset loop to randomize the numbers.
Actually one can use Rnd() in a query to get random numbers, but Access frequently refreshes recordset of opened query, so the numbers will continuously change.
I tried something similar to this, it just kept returning the same number. I think you have to go with a recordset loop.
Pay attention to the special varDummy argument. LOL.
Actually one can use Rnd() in a query to get random numbers, but Access frequently refreshes recordset of opened query, so the numbers will continuously change.
Really? I tried to do it with a small recordset, only 4 records.
Instead of an update, I used: -
SELECT *, ReturnRnd() As RandomNbr
-
FROM Table1;
-
I kept getting the same numbers for all the records.
My ReturnRnd() Function was pretty much the same as yours.
Pay attention to the special varDummy argument. LOL.
Oh, didn't see that. What's the varDummy do?
Oh, didn't see that. What's the varDummy do?
Simply enforces the query to run the function for each record.
Simply enforce the query to run the function for each record.
I see, so that's why mines didn't work. I didn't realize the query wouldn't run the function for each record unless you supplied an argument. Makes sense.
I tried what you said.
I have the Public function like you indicated. Then in the ON Load event for the form I put the rest of your code like so
strSQL = "UPDATE tblWord SET tblWord.Word_Random = " & GetRandomValue(Word_Description) & ";"
DoCmd.RunSQL (strSQL)
(Note the Update statement is all on 1 line)
It updates 21 records but with the same number.
What am I doing wrong. Should I create a loop. According to your post the varDummy variable should run once for each record.
I tried what you said.
I have the Public function like you indicated. Then in the ON Load event for the form I put the rest of your code like so
strSQL = "UPDATE tblWord SET tblWord.Word_Random = " & GetRandomValue(Word_Description) & ";"
DoCmd.RunSQL (strSQL)
(Note the Update statement is all on 1 line)
It updates 21 records but with the same number.
What am I doing wrong. Should I create a loop. According to your post the varDummy variable should run once for each record.
I guess [Word_Description] is a field in table [tblWord]. -
strSQL = "UPDATE tblWord SET tblWord.Word_Random = GetRandomValue([Word_Description]);"
-
DoCmd.RunSQL (strSQL)
-
See the difference?
Regards,
Fish
I see the difference. The problem is that when I try it that way I get an "undefined function GetRandomValue in expression" error. That is why I did it the other way.
So maybe my setting up of the public function is wrong.
I just put it at the top of my code. Does it have to be somewhere specific? Is there a special syntax for the public function? I copied it from your post and just placed it at the top of the code outside of all subs.
I guess [Word_Description] is a field in table [tblWord]. -
strSQL = "UPDATE tblWord SET tblWord.Word_Random = GetRandomValue([Word_Description]);"
-
DoCmd.RunSQL (strSQL)
-
See the difference?
Regards,
Fish
Did you add the code of the function to code module (not form or report or any other class module)?
Anyway see attachment.
Thanks Fish. Got it now. I put the function in the wrong place.
You are a saviour (lol).
Did you add the code of the function to code module (not form or report or any other class module)?
Anyway see attachment.
Not a problem.
Good luck.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Robin Cull |
last post by:
Imagine I have a dict looking something like this:
myDict = {"key 1": , "key
2": , "key 3": ,
"key 4": }
That is, a set of keys which have a variable length list of associated
values after...
|
by: Susanna |
last post by:
Hi all,
I'm using the following slideshow script that I found on the web to
display changing images with a crossfade effect. Eventually I will be
adding many more images to the slideshow. The...
|
by: Robert Neville |
last post by:
I would like to add filter functionality to my database whether
through the Main form or the subform. This question may be
rudimentary, yet I have not less experience with filtering data
outside...
|
by: CSDunn |
last post by:
Hello,
I have a problem with field filtering between an Access 2000 Project form
(the application is called CELDT), and the report that shows the results of
the filter. Both the form and the...
|
by: Richard |
last post by:
Hi,
I have a form based on a table. When I filter the form I want to run a
report based on the same table with the same filter as the form. No problem
until I want to filter a combo box where...
|
by: dick |
last post by:
I am just trying to print/report the results of a "filter by
selection" which is done by right-clicking a form, filling in values,
and "applying the filter."
I have searched the newsgroups, and...
|
by: Salad |
last post by:
I have a log file with a list of records. The log file can be
unfiltered or filtered.
I have a command button to call a data entry form from the log. At
first I was only going to present the...
|
by: Mike Sweetman |
last post by:
I have a form Form1 which when the Advanced Filter/Sort is used
creates a form(maybe) with a title 'Form1Filter1 : Filter'. When I
apply the filter to Form1 it is applied, but the value of...
|
by: marcus.kwok |
last post by:
I am having a weird problem and I have can't figure out why it is
happening. I create an OpenFileDialog and set a filename filter. When
the dialog first opens, the filter works correctly, and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |