473,385 Members | 1,400 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,385 software developers and data experts.

Count Number of Records of a RecordSet Meeting Certain Criteria

194 100+
Hi Everybody,

I am using the following code to get the recordset of an external database.
Expand|Select|Wrap|Line Numbers
  1. Dim dbPatients As Database
  2.  
  3.         Dim rsCountPatients As Recordset ' to count number of patients in a table        
  4.  
  5.         Const strDBPath = "C:\Patients.mdb" ' external database
  6.         Set dbPatients = OpenDatabase(strDBPath, False, True, ";PWD=abc")
  7.  
  8.         Set rsCountPatients= dbNavy.OpenRecordset("SELECT  * FROM Admissions", dbOpenSnapshot) 'Admissions is a table where records of admitted patients is entered
Now, how i can count the number of records in Patients table after meeting some criteria. for example i wana count the number of patients who admitted yesterday and belong to New York city and name is John etc. Can i use DCount function here? if yes then please tell me how i can use it. because i have to manipulate too many records based on criteria and then displaying them into a report. i can achieve this by making a sperate query (in external database) for each field of a report (where i wana show the results) but its very time taking and lenghty.

i will be thankful if anybody please tell me about this.

thanking you.

Regards
Mar 16 '08 #1
2 8956
nico5038
3,080 Expert 2GB
You can expand the opening of the recordset with a WHERE clause to get the filtered records like:
Expand|Select|Wrap|Line Numbers
  1. Set rsCountPatients= dbNavy.OpenRecordset("SELECT * FROM Admissions WHERE FirstName like 'John*'", dbOpenSnapshot)
  2.  
Now using:
Expand|Select|Wrap|Line Numbers
  1. IF rsCountPatients.eof and rsCountPatients.bof then
  2.    ' Nothing found action
  3. else
  4.   ' force reading all
  5.    rsCountPatients.movelast
  6.  ' show result
  7.   msgbox "Record found: " & rsCountPatients.recordcount
  8. endif
  9.  
Will give the number.

Nic;o)
Mar 17 '08 #2
mfaisalwarraich
194 100+
You can expand the opening of the recordset with a WHERE clause to get the filtered records like:
Expand|Select|Wrap|Line Numbers
  1. Set rsCountPatients= dbNavy.OpenRecordset("SELECT * FROM Admissions WHERE FirstName like 'John*'", dbOpenSnapshot)
  2.  
Now using:
Expand|Select|Wrap|Line Numbers
  1. IF rsCountPatients.eof and rsCountPatients.bof then
  2.    ' Nothing found action
  3. else
  4.   ' force reading all
  5.    rsCountPatients.movelast
  6.  ' show result
  7.   msgbox "Record found: " & rsCountPatients.recordcount
  8. endif
  9.  
Will give the number.

Nic;o)
This is absolutely great :) thank you so much Nico im really grateful to you. it really made my work easy and more precise. thank u again.
Mar 17 '08 #3

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

Similar topics

5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
0
by: DataFreakFromUtah | last post by:
Hello! No question here, just a procedure for the archive. Search critera: count records imported count data imported count number of rows imported count number of records imported record import...
1
by: RobMea | last post by:
I am trying to find a solution to the following problem. First off I'm new to Access and SQL but have a good back ground in other languages, thus at a new job this has just been dumped on me so...
4
by: Mark | last post by:
Hi All, I'm trying my hardest to learn VBA but have run into a problem which hopefully someone can guide me with. Basically what I am trying to achieve is to get the database to check the users...
0
by: Scott at Cedar Creek | last post by:
I have to SQL Tables...one called 'Providers' and one called 'Services'...they each contain, among many other things, a field called 'Region' What I would like to accomplish is a table that...
7
by: Killer42 | last post by:
Hi all. I dabble with Access, but haven't done anything in-depth for a number of years. What I want to do now is probably a fairly simple JOIN or something, but I just can't recall how to go about...
5
by: Soccer5 | last post by:
Trying to Count records on a report that meet a certain criteria. Have a text box in the Report Footer that has the following in the Control Source: =Count(="S") This does not work. It...
1
by: sparks | last post by:
I have always just got a count on records and put up a box at the top of the entry forms saying something like 1 of 500 and updating it as you got to 500 of 500. I have never thought of this as...
3
by: Miguel Valenzue | last post by:
I want to count the number of items that occur within a range of time throughout different days. My original data comes from a machine that logs the time a car passes a point in a road over...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.