Copy Recordset to another Recordset (1 Viewer)

JohnPapa

Registered User.
Local time
Today, 11:46
Joined
Aug 15, 2010
Messages
954
yes, use the rs.filter property

Code:
Dim db As DAO.Database
    Dim rs  As DAO.Recordset
    Dim rs2  As DAO.Recordset


    Dim SQL_Select  As String


    SQL_Select = "Select * from tbl1"


    Set db = CurrentDb()
    Set rs = db.OpenRecordset(SQL_Select)


    rs.Filter = "date1 < #1/1/24# "


    Set rs2 = rs.OpenRecordset


    rs.MoveLast
    rs2.MoveLast
         
    Debug.Print rs.RecordCount, rs2.RecordCount


MyExit:


    rs.Close
    Set rs = Nothing
    rs2.Close
    Set rs2 = Nothing
    Set db = Nothing
   
End Sub

Suppose you could also set the 1st recordset as global.
Actually both recordsets are global. Will try it out. Thanks.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:46
Joined
May 21, 2018
Messages
8,529
The Dao filter is probably worse performance the just a new query
In many cases, it's faster to open a new Recordset object by using an SQL statement that includes a WHERE clause.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:46
Joined
May 21, 2018
Messages
8,529
However there are bigger problems with this dB if that query is slow to populate the RS. I would assume a handful of appointments are returned?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 09:46
Joined
Feb 19, 2013
Messages
16,612
It says Run-time error 5, Invalid procedure call or argument.
since that code is not what I supplied, would need to see the full code, including declarations
The reason why I do not want to rerun the query is that it can take a lot of time.

on 1/2m records, should not take long - implies fields used in queries for joins and criteria are not indexed
 

moke123

AWF VIP
Local time
Today, 04:46
Joined
Jan 11, 2013
Messages
3,920
What's the purpose of this exercise? Is it to just avoid running the slow query more than once?
 

JohnPapa

Registered User.
Local time
Today, 11:46
Joined
Aug 15, 2010
Messages
954
yes, use the rs.filter property

Code:
Dim db As DAO.Database
    Dim rs  As DAO.Recordset
    Dim rs2  As DAO.Recordset


    Dim SQL_Select  As String


    SQL_Select = "Select * from tbl1"


    Set db = CurrentDb()
    Set rs = db.OpenRecordset(SQL_Select)


    rs.Filter = "date1 < #1/1/24# "


    Set rs2 = rs.OpenRecordset


    rs.MoveLast
    rs2.MoveLast
         
    Debug.Print rs.RecordCount, rs2.RecordCount


MyExit:


    rs.Close
    Set rs = Nothing
    rs2.Close
    Set rs2 = Nothing
    Set db = Nothing
   
End Sub

Suppose you could also set the 1st recordset as global.
I made the changes you suggested and it works fine. Thanks.

Once I find some time I will do it the way it was suggested.
1) Get only one recordset, the one with all the appointments
2) Maybe pass it as a parameter to the form or use a public recordset
3) Use a filter on the form to show "All Patient Appointments" or "Future Patient Appointments"

The appointments associated with a patient may range from 1 to 20, in any case not many.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:46
Joined
May 21, 2018
Messages
8,529
I would be interested if the filter recordset provides any efficiency. In my experience it is faster to simply open the new filtered recordset as MS states.
 

JohnPapa

Registered User.
Local time
Today, 11:46
Joined
Aug 15, 2010
Messages
954
I would be interested if the filter recordset provides any efficiency. In my experience it is faster to simply open the new filtered recordset as MS states.
I have tested the Filter option with the large db on my pc and it is very fast. I need to test it on the actual network and will let you know.

As you commented a patient's appointments is a very small number, so at first sight,if I had a choice of filtering say 10 records Vs querying 500000 records, I would choose the former.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 09:46
Joined
Feb 19, 2013
Messages
16,612
So you want to bring 500,000 records across the network to then filter? Bringing 500,000 records across is what takes the time
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:46
Joined
May 21, 2018
Messages
8,529
I had a choice of filtering say 10 records Vs querying 500000 records, I would choose the former.
My understanding is it is not that simple. In that It does not directly filter the recordset but just creates a new filtered connection. Maybe I am incorrect in that interpretation.
@Galaxiom reports good performance.
 

cheekybuddha

AWF VIP
Local time
Today, 09:46
Joined
Jul 21, 2014
Messages
2,280
In Post #1 @JohnPapa uses a recordset already filtered by patient as the basis.

He seems to just want to toggle between all that patient's appointments and just future appointments without hitting the backend again.
 

Users who are viewing this thread

Top Bottom