CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Location
    Mid-West, USA
    Posts
    47

    ADORecordset.RecordCount = -1

    Hi everyone,

    Would you please tell me why this is happening? I use ADO with MS Access 2002 and here is what I do:

    'Declarations
    Private adoConn As New ADODB.Connection
    Private adoRS As New ADODB.Recordset

    Private StringCnn As String
    Private AccessCnn As String
    .......
    'make the connection for the DB
    AccessCnn = "DRIVER={Microsoft Access Driver (*.mdb)};" & "DBQ=EmailData.mdb;" & "DefaultDir=" & App.Path + "\" & ";" & "UID=admin;PWD=;"
    StringCnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\EmailData.mdb;Persist Security Info=False;"

    '''These are the codes from by Sub
    sSource = "SELECT * FROM Server" 'Server is the table name
    adoConn.Open StringCnn & App.Path & "\EmailData.mdb" 'Emaildata is the DB file name
    adoRS.Open sSource, adoConn, adOpenDynamic, adLockOptimistic

    With adoRS
    If .RecordCount Then
    .MoveFirst
    Do While Not .EOF
    ...........I do lots of things in here
    Loop
    End If

    .Close
    Set adoRS = Nothing
    adoConn.Close
    Set adoConn = Nothing
    End With

    I have 2 records in table "Server" but .RecordCount gives me -1, this is NOT right. Then I deleted ALL records from table "Server" but now .RecordCount still gives me -1.

    What is wrong with it? How to fix it?

    Thanks,

  2. #2
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: ADORecordset.RecordCount = -1

    Before you access the recordcount property, do this:
    Code:
    adoRS.MoveLast
    adoRS.MoveFirst
    That should get it working properly
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  3. #3
    Join Date
    Jul 2005
    Location
    Mid-West, USA
    Posts
    47

    Re: ADORecordset.RecordCount = -1

    [QUOTE=HairyMonkeyMan]Before you access the recordcount property, do this:
    Code:
    adoRS.MoveLast
    adoRS.MoveFirst
    Then when it is a "Brand NEW" table with zero (0) record, the adoRS.MoveLast will create an Error.

    I used the statement "If adoRS.RecordCount" to check to see if there is any record in the DB, if there is no record, the loop will never get executed.

    Thanks for your input. Is there something wrong with Access or ADO????

    Thanks,

  4. #4
    Join Date
    May 2005
    Location
    Melbourne
    Posts
    12

    Lightbulb Re: ADORecordset.RecordCount = -1

    hi,

    The property returns -1 when ADO cannot determine the number of records. Reading the RecordCount property on a closed Recordset causes an error.

    try closing the recordset,before opening,
    try this sample


    Private sub CmdSample_Click()

    Dim sql
    sql = "Select * from Table2"

    On Error GoTo ErrMessage

    If rs.State = 1 Then
    rs.Close
    End If



    With rs
    .ActiveConnection = cn
    .CursorLocation = adUseServer
    .CursorType = adOpenDynamic
    .LockType = adLockOptimistic
    .Open sql

    If .EOF Or .BOF Then
    MsgBox "No Records"
    Exit Sub
    Else
    .AddNew
    .Fields!Name = "Murali"
    .Fields!Age = "28"
    .Update
    End If

    Set rs = Nothing

    End With

    Exit Sub
    ErrMessage:
    If Err.Number <> 0 Then
    MsgBox Err.Description
    Exit Sub
    End If

    end sub

    As per the above example...i didnt felt any necessay to use RecordCount property, for just checking whether there are any records or not.

    I've found myterious activity when i used Recordcount property using ADO, when trying to delete one or more records having a smilar primarykey in a table.

    Ill try using Recordcount property, and try the above solution, if it fits ur problem..and im sorry, if the above sloution didnt answer ur question.

  5. #5
    Join Date
    May 2001
    Posts
    91

    Re: ADORecordset.RecordCount = -1

    Hi,

    you have to set the .cursorlocation property of the recordset to aduseclient.
    It is quite clear that .recordcount cannot work without this, as the recordset object "assumes" that the cursor may be server-sided.
    You should - additonally - set the .cursortype property to adopenstatic.
    have a nice day

    Patzer
    _____________________________
    Philo will never be forgotten

  6. #6
    Join Date
    Jul 2005
    Location
    Mid-West, USA
    Posts
    47

    Re: ADORecordset.RecordCount = -1

    Quote Originally Posted by Patzer
    Hi,

    you have to set the .cursorlocation property of the recordset to aduseclient.
    It is quite clear that .recordcount cannot work without this, as the recordset object "assumes" that the cursor may be server-sided.
    You should - additonally - set the .cursortype property to adopenstatic.

    Yes, when I set the .cursortype to adOpenStatic or adOpenKeyset, the .RecordCount give me the correct values.

    Thanks guys.

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

Featured