Solved

Access database - number of records which match select

Posted on 2000-05-01
2
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

710 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