Yes TOP is the way to do it, but you have to have the recordset ordered correctly i.e.
strSQL = "SELECT TOP 4 tblPatients.ID, ...
... ORDER BY tblVisits.dateVisit DESC;"
Hope that helps
Rick
Main Topics
Browse All TopicsI have a SQL statement to pull all active patients' visits from a current month, but I only want the 4 most recent dates. I think TOP is the solution to my problem, but I'm not sure how to use it
Code below and tblPatients and tblVisits attached. Note Patients 2 and 5 have 5 visits in October 2008. The query should only return the 4 most recent.
Note: this file has URL parameter ?unit=3
Thanks in advance!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Won't it be more than that? I need the statement to give me a recordset that may have null values in the 3 tblVisits fields where it finds active patients that have not yet had a visit this month. they will not be null if the patient has had visits this month, but hold at most 4 tblVisits records based on most recent visits.
make sense?
but i have to get the 4 most recent visits from a ton of patients. how can i keep a counter for a dynamic number of patients? i figured sorting by lname, fname, dateVisit would put everything in the order I need it. Ideally, i want my table to look like:
name1 | <link to add visit because no visits this month>
name2 | visit 1 info | visit 2 info |
name3 | visit 1 info | visit 2 info | visit 3 info | visit 4 info |
name 4| <link to add visit because no visits this month>
i tried the following and received a (0x80020009) Exception occurred. line 32
line 32 is the second Response.Write() in the innermost nested loop
googling this error took me here:
http://classicasp.aspfaq.c
the second entry here appears to be a possible problem--recordset out of scope maybe???
Business Accounts
Answer for Membership
by: gr33dPosted on 2008-10-10 at 06:04:51ID: 22686414
ERRATA:
the recordset must include even those patients who are active but have NOT yet had a visit this month