Results 1 to 4 of 4

Thread: SELECT COUNT(*)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Atlanta, GA
    Posts
    80

    Question

    What is the best way to return a Count of Records that match criteria in a DB?

    The only way I know of is to Open the Recordset, MoveLast, and use the RecordCount Property...

    Is there a way that I can do a Conn.Execute(SQLStatement) that just returns a Count? i.e. SELECT COUNT(*) FROM Table WHERE Active = 1, etc....

    Thanks,
    Kevin
    Kevin

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Here is an example for ADO.

    (Unfortunately you cannot use Execute because that is for what they call ACTION queries which mean they must perform an insert, update or delete)

    Code:
    Set rstCount = dbDatabase.OpenRecordset("SELECT COUNT(*) as NumRecs FROM TableName WHERE ........",adOpenSnapshot, adReadOnly, adReadOnly)
    
    If rstCount.recordCount <> 0 Then
      iNumRecords = rstCount!NumRecs
    Else
      iNumRecords = 0
    End if
    
    rstCount.close

  3. #3
    Junior Member
    Join Date
    May 2000
    Location
    Karachi,Pakistan
    Posts
    16

    Try this(It will work as well)

    Dear..
    Try this code..

    dim cn as new adodb.connection
    dim rs as new adodb.recordset

    cn.open(your_connect_string)
    set rs = cn.execute("SELECT num = COUNT(*) FROM TABLE WHERE XYX..",cn

    dim intNumRecords
    intNumRecords = rs.fields("Num")

    now intNumRecords will hold the number of records

  4. #4
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    intNumRecords = rs.fields(0) works as well, the advantage being you don't have to specify an abritrary name to SQL to make it work...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width