Solved

DataGrid + ador.recordset is showing 0 recordcount

Posted on 2003-11-17
19
467 Views
Last Modified: 2007-12-19
I have a project that uses a datagrid to display two fields.  The datasource is a disconnected ADOR recordset that I create at runtime.  The user selects two items from a Treeview and clicks a button.  When the button is fired I place the items into the recordset which are displayed in the datgrid.  This all works fine untill the user deletes all of the records in the datagrid(This is allowed).  When the user goes to add another two items to the now empty recordset the items are added but it is never reflected in the recordsets recordcount.  Here is what I am doing, rdis is the datagrids datasource:

    rDis.AddNew
    rDis.Fields(0).Value = Trim(Left(txtMapS.Text, (InStr(txtMapS, "(") - 1)))
    rDis.Fields(1).Value = Trim(Left(txtMapA.Text, (InStr(txtMapA, "(") - 1)))
    rDis.Fields(2).Value = (Left(sStageKeyACT, (InStr(sStageKeyACT, "%") - 1)))
    rDis.Update
    Debug.Print rDis.RecordCount
The user can click the button all day and the only way the recordcount is updated is if I go to the datagrid and manualy delete a record, but the records are added and visible in the datagrid.

If I step through the code when the datagrid already has records the recordcount is incremented as soon I call addnew but after the record count has been brought to zero this is not the case.  I have tried updating, refreshing and requerying both the datagrid and the recordset with no luck. If I even highlight one of the rows in the datagrid this also seems to somehow sync the data so that I get an accurate record count.

Can anyone help?  I would even do something clunky like programaticaly select a row but have not figured out how.

Thanks for your time.
0
Comment
Question by:grgl44
19 Comments
 
LVL 4

Expert Comment

by:TomLaw1999
ID: 9769919
Have you tried
rDis.Movefirst (placed immediatly after rDis.Update?
0
 

Author Comment

by:grgl44
ID: 9772207
Thanks for the reply.  Yes I have tried all variations of moving through the recordset including this:

While Not rDis.EOF     '=FALSE or true with the negate
        Debug.Print rDis.Fields(0).Value   '=Shows a Value
        rDis.MoveNext
Wend
debug.print rDis.recordCount    '=Still shows 0 even though I watched it move through.

Thanks for your time
0
 
LVL 4

Expert Comment

by:TomLaw1999
ID: 9772876
Is this a server side recordset or client. What type of cursor are you using

Extracted from the VB help file

Use the RecordCount property to find out how many records are in a Recordset object. The property returns -1 when ADO cannot determine the number of records or if the provider or cursor type does not support RecordCount. Reading the RecordCount property on a closed Recordset causes an error.

If the Recordset object supports approximate positioning or bookmarks—that is, Supports (adApproxPosition) or Supports (adBookmark), respectively, return True—this value will be the exact number of records in the Recordset regardless of whether it has been fully populated. If the Recordset object does not support approximate positioning, this property may be a significant drain on resources because all records will have to be retrieved and counted to return an accurate RecordCount value.

The cursor type of the Recordset object affects whether the number of records can be determined. The RecordCount property will return -1 for a forward-only cursor, the actual count for a static or keyset cursor, and either -1 or the actual count, depending on the data source, for a dynamic cursor.
0
 

Author Comment

by:grgl44
ID: 9773088
The cursor is set up like this:

    rDis.CursorType = adOpenDynamic
    rDis.CursorLocation = adUseClient

The issue is the first time a user adds a record everything works fine (RecordCount goes to 1).  If the user has deleted all of the records through the datagrid the RecordCount goes to 0.  At this point everything changes.  When adding a record the same way that it is added in the beginning the recordCount is never incremented.

Thanks for your time.
0
 
LVL 4

Expert Comment

by:TomLaw1999
ID: 9773129
The adUseClient setting should be OK. Have you tried changing adOpenDynamic to either static or keyset, suggest trying that first.
0
 
LVL 4

Expert Comment

by:TomLaw1999
ID: 9773142
Failing that could you show me the entire sub routine that picks up the recordcount value.
0
 

Author Comment

by:grgl44
ID: 9773313
Tried different settings with no sucess.

When the project loads the recordset is set like this:
    Set rDis = New ADOR.Recordset
    rDis.Fields.Append "MySQL", adVarChar, 100
    rDis.Fields.Append "ACT!", adVarChar, 100
    rDis.Fields.Append "aID", adInteger
    rDis.CursorType = adOpenStatic
    rDis.CursorLocation = adUseClient
    rDis.Open
    Set Start.dgMaps.DataSource = rDis        'START IS THE NAME OF THE FORM
    Start.dgMaps.Columns(2).Visible = False

This is the sub that is picking the recordcount value.  The idea is to not allow duplicate entries in the datagrid/recordset:

Private Sub cmdMap_Click()
 Dim bgood As Boolean
 Dim sSQL As String
 bgood = False
 Debug.Print rDis.RecordCount
 If bHaveMaps Then
    If rDis.RecordCount > 0 Then
   
        rDis.MoveFirst
        While Not rDis.EOF
            If rDis.Fields(0).Value = Trim(Left(txtMapS.Text, (InStr(txtMapS, "(") - 1))) And _
                rDis.Fields(1).Value = Trim(Left(txtMapA.Text, (InStr(txtMapA, "(") - 1))) Then
                cmdMap.Enabled = False
                Exit Sub
            End If
        rDis.MoveNext
        Wend
    End If
    If checkTable Then
        bgood = True
    Else
        MsgBox "Please Choose fields from a single table only", vbInformation, "Table Conflict"
        bgood = False
    End If
Else
    bgood = True
End If
If bgood Then
    rDis.AddNew
    rDis.Fields(0).Value = Trim(Left(txtMapS.Text, (InStr(txtMapS, "(") - 1)))
    rDis.Fields(1).Value = Trim(Left(txtMapA.Text, (InStr(txtMapA, "(") - 1)))
    rDis.Fields(2).Value = (Left(sStageKeyACT, (InStr(sStageKeyACT, "%") - 1)))
    rDis.Update
    Debug.Print rDis.RecordCount
    sCurrentTableSQL = sStageTableSQl
    sCurrentTableACT = sStageTableACT
    If Not bHaveMaps Then bHaveMaps = True
    If Not bDirty Then bDirty = True
End If

End Sub

Thanks for your time.

0
 
LVL 4

Expert Comment

by:TomLaw1999
ID: 9773506
Just a suggestion - try placing a Debug.Print rDis.RecordCount after  rDis.MoveFirst in the Private Sub cmdMap_Click().
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:grgl44
ID: 9774057
It makes no difference.  I can add mutiple records but the recordcount is never changed until I manualy delete a row from the datagrid.

Thanks for your time.
0
 
LVL 4

Expert Comment

by:TomLaw1999
ID: 9777326
Have you checked for an error condition whilst running this routine? ie from within VB

Select Tools
Select Options
Select General
Set error trapping to Break on all errors

Then run the routine and see if you get an error generated.

I'm just trying to determine if a null field value is causing your problem.
0
 

Author Comment

by:grgl44
ID: 9780255
Thanks for the suggestion.  It does not generate an error.  It is like this: When the program is first run the recordcount = 0 as the user adds items it goes up and as the user deletes items it goes down.  If the user takes out all of the items it goes to zero.  At that point if the user adds an item the item is processed and shows in the grid but the recordcount does not reflect it. No matter how many items are added the recordcount stays at zero.  If the user deletes one of the rows the recordcount magically is restored.

Thanks for your time.
0
 

Author Comment

by:grgl44
ID: 9780610
Thanks for your time and help here is how I am dealing with the bug:
   
    I add the real record then update
    rDis.Update
    If rDis.RecordCount = 0 Then 'clunky fix for bug
        With rDis
            .AddNew
            .Fields(0) = "squib"
            .Update
            .Delete adAffectCurrent
        End With
    End If
    Debug.Print rDis.RecordCount  'This will now read 1

This seems to work fine although it is funky.
0
 
LVL 4

Expert Comment

by:TomLaw1999
ID: 9781337
Not ideal - but if it works .....
0
 
LVL 5

Expert Comment

by:RainUK
ID: 9787058
Hi

What version of ADO are you using 2.6?
Anyway you could try this one liner before querying record count:

adoRs.Filter = ADODB.FilterGroupEnum.adFilterNone

Then query the record count, this will update the record count and also move the cursor to the either BOF or the first row.

Also define the recordset as adUseClient and KeySet.

Give it a whizz
0
 

Author Comment

by:grgl44
ID: 9789062
Thanks

I tried your solution but it had no effect.
0
 

Author Comment

by:grgl44
ID: 10197319
I have asked for a refund of points and that this question be closed.  If you read the above you will see that I found a funky solution that worked for me and used it to solve my problem.  

Thank you to everyone that offered sugestions.

0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 10213761
PAQed, with points refunded (125)

Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2013 combo box not working 3 38
vb6 connector to SQL Server 2 34
Modifying Conditional Format from VBA code 3 49
Passing a Text Box name to a Sub 6 74
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now