Results 1 to 7 of 7

Thread: recordcount problem .

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Location
    United
    Posts
    202

    recordcount problem .

    Code:
    Dim SDaT As String
    Dim CTiM As String
     SDaT = Format(Date, "dd/mm/yy")
     CTiM = Format(Time, "hh:mm")
         conn.connectionsettings="AW"
     Dim rs As New ADODB.Recordset
     Set rs = New ADODB.Recordset
     rs.Open "SELECT * FROM Attendance where RIC='rnames(fi)' & DateIn='SDaT' ", conn, adOpenStatic, adLockOptimistic, adCmdTable
     msgbox rs.RecordCount
    i run the above code , but somehow i wasn't able to get any recordcount even though the record exist in the database but the code just by pass the msgbox rs.recordcount and execute the next line ? is any thing wrong with mine rs.open statement ?

  2. #2
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    In your Open statement you need to replace adOpenStatic with adOpenKeyset or adOpenDynamic
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Location
    United
    Posts
    202
    but i could get -1 froom the record count , anyway parameter still need to be changed ?

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The RecordCount property will always be -1 when using a Server Side cursor. Change the CursorLocation property to adUseClient.

    Do you use On Error Resume Next? I always thought using a SQL statement and adCmdTable in the rs.Open statement would raise an error. Maybe it depends on the provider.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    I cannot even see how that SELECT statement is working - you are not concatenating the two VB variables into the string...

    But at any rate you could use a trick...

    Code:
    SELECT (SELECT COUNT(*) FROM ...),* FROM ...
    Here I am adding a SUBQUERY to the start of the SELECT list that is the exact same WHERE clause as the SELECT itself, but does a COUNT(*) - which will be the record count.

    Here you get a RECORDCOUNT in each ROW of the total number of rows in the recordset.

    Sloppy, ugly, not elegant - but it will work.

  6. #6
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by szlamany
    I cannot even see how that SELECT statement is working - you are not concatenating the two VB variables into the string...

    But at any rate you could use a trick...

    Code:
    SELECT (SELECT COUNT(*) FROM ...),* FROM ...
    Here I am adding a SUBQUERY to the start of the SELECT list that is the exact same WHERE clause as the SELECT itself, but does a COUNT(*) - which will be the record count.

    Here you get a RECORDCOUNT in each ROW of the total number of rows in the recordset.

    Sloppy, ugly, not elegant - but it will work.
    szlamany is absolutely correct. You use two conditions in your where clause but you forgot to use the AND operator and you call external functions from within the sql string. Try it like this . . .

    VB Code:
    1. Dim SDaT As String
    2. Dim CTiM As String
    3.  SDaT = Format(Date, "dd/mm/yy")
    4.  CTiM = Format(Time, "hh:mm")
    5.      conn.connectionsettings = "AW"
    6.  Dim rs As New ADODB.Recordset
    7.  Set rs = New ADODB.Recordset
    8.  rs.Open "SELECT * FROM Attendance WHERE RIC='" & rnames(fi) & "' AND DateIn='SDaT'", conn, adOpenStatic, adLockOptimistic, adCmdTable
    9.  MsgBox rs.RecordCount
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  7. #7
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Sometimes, it's also easier to build the sql string first. Makes the code a little cleaner . . . .

    VB Code:
    1. Dim SDaT As String
    2. Dim CTiM As String
    3. Dim strSQL As String
    4.  SDaT = Format(Date, "dd/mm/yy")
    5.  CTiM = Format(Time, "hh:mm")
    6.      conn.connectionsettings = "AW"
    7.  Dim rs As New ADODB.Recordset
    8.  Set rs = New ADODB.Recordset
    9.  strSQL = "SELECT * FROM Attendance WHERE RIC='" & rnames(fi) & "' AND DateIn='SDaT'"
    10.  rs.Open strSQL, conn, adOpenStatic, adLockOptimistic, adCmdTable
    11.  MsgBox rs.RecordCount
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

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