Solved

Access database - number of records which match select

Posted on 2000-05-01
2
157 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now