473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Randomizing a filter

mshmyob
904 Expert 512MB
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?
Jan 15 '08 #1
16 2026
Rabbit
12,516 Expert Mod 8TB
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.
Jan 15 '08 #2
FishVal
2,653 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. Public Function GetRandomValue(varDummy As Variant) As Long
  2.     GetRandomValue = (Rnd - 0.5) * 4000000000#
  3. End Function
  4.  
Expand|Select|Wrap|Line Numbers
  1. UPDATE [Your table] SET [Your table].lngRand = GetRandomValue([Any table field]);
  2.  
Regards,
Fish
Jan 16 '08 #3
Rabbit
12,516 Expert Mod 8TB
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.
Jan 16 '08 #4
Rabbit
12,516 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. Public Function GetRandomValue(varDummy As Variant) As Long
  2. GetRandomValue = (Rnd - 0.5) * 4000000000#
  3. End Function
  4.  
Expand|Select|Wrap|Line Numbers
  1. UPDATE [Your table] SET [Your table].lngRand = GetRandomValue([Any table field]);
  2.  
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.
Jan 16 '08 #5
FishVal
2,653 Expert 2GB
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.
Jan 16 '08 #6
FishVal
2,653 Expert 2GB
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.
Jan 16 '08 #7
Rabbit
12,516 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT *, ReturnRnd() As RandomNbr
  2. FROM Table1;
  3.  
I kept getting the same numbers for all the records.
My ReturnRnd() Function was pretty much the same as yours.
Jan 16 '08 #8
Rabbit
12,516 Expert Mod 8TB
Pay attention to the special varDummy argument. LOL.
Oh, didn't see that. What's the varDummy do?
Jan 16 '08 #9
FishVal
2,653 Expert 2GB
Oh, didn't see that. What's the varDummy do?
Simply enforces the query to run the function for each record.
Jan 16 '08 #10
Rabbit
12,516 Expert Mod 8TB
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.
Jan 16 '08 #11
mshmyob
904 Expert 512MB
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.
Jan 16 '08 #12
FishVal
2,653 Expert 2GB
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].

Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tblWord SET tblWord.Word_Random = GetRandomValue([Word_Description]);"
  2. DoCmd.RunSQL (strSQL)
  3.  
See the difference?

Regards,
Fish
Jan 16 '08 #13
mshmyob
904 Expert 512MB
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].

Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tblWord SET tblWord.Word_Random = GetRandomValue([Word_Description]);"
  2. DoCmd.RunSQL (strSQL)
  3.  
See the difference?

Regards,
Fish
Jan 16 '08 #14
FishVal
2,653 Expert 2GB
Did you add the code of the function to code module (not form or report or any other class module)?

Anyway see attachment.
Attached Files
File Type: zip Rand.zip (14.2 KB, 77 views)
Jan 16 '08 #15
mshmyob
904 Expert 512MB
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.
Jan 16 '08 #16
FishVal
2,653 Expert 2GB
Not a problem.
Good luck.
Jan 16 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

9
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...
2
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...
1
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...
0
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...
3
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...
8
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...
2
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...
2
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...
8
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
Oralloy
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,...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.