Solved

Access database - number of records which match select

Posted on 2000-05-01
2
159 Views
Last Modified: 2010-05-02
Hi,

I am writing a shopping cart and need to find out how many items in the table match the given criteria.

I can search through the table and get each record until there are no more matches and increment a counter using code like the code below - but there must be a better way - all I need to know is the number of records which match the select criteria.

Maybe there is some kind of select 'count' where cookieid='002' or something like that - if someone could let me know this I would be greatfull.

I could achive what I want using the following code but it's going to be pretty sloppy to have to step through each matching record when I probably don't need to.

        Set lMyDB = DBEngine.OpenDatabase("c:\ecommerce\eshop.mdb")
        Set lMYRs = lMyDB.OpenRecordset("cart", dbOpenDynaset)

        lMYRs.FindFirst "cookieid=" + cookieid$
       
        If lMYRs.NoMatch = False Then
            ' First match has been found
            ' Start a loop for counting additional matches
            Do
            recordcount = recordcount + 1
                lMYRs.FindNext "cookieid=" + cookieid$

               
            Loop While lMYRs.NoMatch = False

        Else
            ' No items found
       
        End If


        lMYRs.Close
        lMyDB.Close
        Set lMYRs = Nothing
        Set lMyDB = Nothing


Thanks
0
Comment
Question by:asd987
2 Comments
 
LVL 1

Accepted Solution

by:
SpyMaster earned 100 total points
ID: 2765829
I cannot think of any other better way then this below.

Dim lMyDB As Database, lMYRs As Recordset
Set lMyDB = DBEngine.OpenDatabase("c:\ecommerce\eshop.mdb")
Set lMYRs = lMyDB.OpenRecordset("Select * from cart where cookieid=" & cookieid$)
If lMYRs.RecordCount > 0 Then lMYRs.MoveLast
MsgBox lMYRs.RecordCount & " records have been found."
0
 

Author Comment

by:asd987
ID: 2765851
Thanks - I thought it's be something pretty easy like that !

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question