Results 1 to 4 of 4

Thread: Check if a record with a specific ID exists

  1. #1
    Lively Member Phantom1's Avatar
    Join Date
    Nov 11
    Posts
    64

    Check if a record with a specific ID exists

    I want to check if a record with ID 1 exists.

    Code:
    rs.Open "SELECT * FROM tblTransaction WHERE TransactionID = 1", DBConn
    There are no records with ID 1. rs.Fields.Count returns 9. How can it?
    Learning to Program on Earth until I go into Outer Space...

  2. #2
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,156

    Re: Check if a record with a specific ID exists

    rs.Fields.Count will return the number of Columns in the Table.

    rs.RecordCount may return the number of rows selected.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,564

    Re: Check if a record with a specific ID exists

    Thread moved to the 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

    In addition to what Doogle said, if you just want to check if any records exist then rs.RecordCount is a bad idea (it is slow, and as implied above may not return the amount), instead you should check rs.EOF (if it is True there are record(s), if it is False there aren't).

  4. #4
    Junior Member
    Join Date
    Dec 11
    Location
    Tullahoma, TN
    Posts
    20

    Re: Check if a record with a specific ID exists

    Checking both .EOF and .BOF is a little more thorough, because there is never a situation in which that can arise except an empty recordset. So:
    Code:
    With rs
       .Open 'blah blah
       If .EOF And .BOF Then
           'It's empty
       Else
           'It isn't
       End If
    End With
    Also, .RecordCount isn't always accurate, depending on the CursorType you use.

Posting Permissions

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