Selecting Records With Specific Criteria

parry

MrExcel MVP
Joined
Aug 20, 2002
Messages
3,355
Hi all, Im not familiar enough with the recordset object to know how to achieve this properly. The purpose is to locate a random record in a form that has the Watched field = true. The following code works but is very unelegant as Im filtering the form, finding a random number and selecting the record, taking a note of the TitleIndex (a unique key field) then turning off filter and locating the record with the found TitleIndex. Because Im turning filter on and off I see the form being flashed a few times.

What I would like is preferably a way to determine the TitleIndex in the recordset clone without filtering the form or alternatively an equivalent of ScreenUpdating=False for Access.

Code:
Private Sub RandomRecord_Click()
Dim Rs As DAO.Recordset, RandRec As Long, RecID As Long
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'^ Purpose: Select a random record in the form.          ^
'^ Note: The record must have the watched column = true  ^
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

' Filter form so that only Watched records are displayed
Me.Filter = "Watched = True"
' Turn on filter
Me.FilterOn = True
' Create a clone of the filtered form recordset
Set Rs = Me.RecordsetClone
' Initialize random-number generator.
Randomize
' Generate random value between 1 and total records in the filtered form.
RandRec = Int((Rs.RecordCount * Rnd) + 1)
' select the record
DoCmd.GoToRecord acDataForm, Me.Name, acGoTo, RandRec
'Get TitleIndex to identify record when filter is removed
RecID = Me.TitleIndex
'Turn off filter
Me.FilterOn = False
'Create a recordset of the unfiltered form
Set Rs = Me.RecordsetClone
'Find the record in question
Rs.FindFirst "[TitleIndex]=" & RecID
'Display record
Me.Bookmark = Rs.Bookmark
'Clean up
Set Rs = Nothing

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Couldn't you use an SQL query for the recordset?
 
Upvote 0
Thanks for the links. Im happy enough with doing RND its just getting the recordset beforehand which the first link seems to have. It seems you have to use a connected recordset rather than a RecordsetClone. My understanding is that the RecordsetClone is for forms while Recordset is the actual table/s behind all my queries so not quite the same but I'll see what happens after I adjust some things.
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,381
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top