Results 1 to 13 of 13

Thread: .RecordCount is always -1

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    84

    .RecordCount is always -1

    my .recordcount is always -1 what will i do... im using an mysql database.. here's my code pls help me
    VB Code:
    1. Option Explicit
    2. Dim AY As String
    3. Dim ls As ListItem
    4.  
    5.  
    6. Private Sub cmdAdd_Click()
    7. frmAcademicYear.addstate = True
    8. frmAcademicYear.Show
    9.  
    10. End Sub
    11.  
    12. Private Sub cmdClose_Click()
    13. Unload Me
    14. End Sub
    15.  
    16. Private Sub cmdDelete_Click()
    17. On Error GoTo Err:
    18. With tbAcademicYear
    19.     '---------------------------------
    20.     'Check if there is no record
    21.     '---------------------------------
    22. '    If .RecordCount < 1 Then MsgBox "No school year in the list.Please check it!", vbExclamation, "CSRS version 1": Exit Sub
    23.     '---------------------------------
    24.     'Confirm deletion of record
    25.     '---------------------------------
    26.     Dim ans As Integer
    27.     Dim pos As Integer
    28.     ans = MsgBox("Are you sure you want to delete the selected record?", vbCritical + vbYesNo, "Confirm Record Delete")
    29.     If ans = vbYes Then
    30.         '-----------------------------
    31.         'Delete the record
    32.         '-----------------------------
    33.         pos = Val(LV1.SelectedItem)
    34.        '  tbAcademicYear.Open "Select * from tbacademicyear where AcademicYearTitle='" & LV1.SelectedItem.Text & "'", cn, adOpenDynamic, adLockOptimistic
    35.              If .EOF = False Then
    36.                 .Delete adAffectCurrent
    37.                 '.Requery
    38.              End If
    39.        
    40.         If .RecordCount > 0 Then
    41.             .AbsolutePosition = pos
    42.             If .EOF Then .MoveFirst
    43.             '---------------------------------
    44.             'Fill listview
    45.             '---------------------------------
    46.             pos = .AbsolutePosition
    47.             load_rec
    48.             LV1.ListItems.Item(pos).EnsureVisible
    49.             LV1.ListItems.Item(pos).Selected = True
    50.             .AbsolutePosition = LV1.SelectedItem
    51.             '---------------------------------
    52.             'End-fill listview
    53.             '---------------------------------
    54.         'Else
    55.            ' LV1.ListItems.Clear
    56.         End If
    57.                 MsgBox "Record has been successfully deleted.", vbInformation, "Confirm"
    58.     End If
    59.     ans = 0
    60.     pos = 0
    61.     Me.MousePointer = vbDefault
    62. End With
    63. Exit Sub
    64. Err:
    65.     MsgBox Err.Description, vbCritical, "Academic Year"
    66.  
    67. End Sub
    68.  
    69. Private Sub cmdEdit_Click()
    70.  
    71. With tbAcademicYear
    72. 'If .RecordCount < 1 Then MsgBox "No school year in the list.Please check it!", vbExclamation, "Titus School of Multimedia": Exit Sub
    73.  
    74. If Not LV1.SelectedItem = "" And Not tbAcademicYear.RecordCount < 1 Then .AbsolutePosition = LV1.SelectedItem
    75. frmAcademicYear.addstate = False
    76. frmAcademicYear.Show
    77. Me.Enabled = False
    78. 'End If
    79. End With
    80. End Sub
    81.  
    82. Private Sub Form_Load()
    83. tbAcademicYear.Open "Select * from tbacademicyear", cn, adOpenDynamic, adLockOptimistic
    84. CallFields
    85. End Sub
    86.  
    87. Private Sub CallFields()
    88. With tbAcademicYear
    89.     LV1.ListItems.Clear
    90.     While .EOF = False
    91.         Set ls = LV1.ListItems.Add(, , .Fields(0))
    92.         .MoveNext
    93.     Wend
    94. End With
    95. End Sub
    96.  
    97. Sub load_rec()
    98. Screen.MousePointer = vbHourglass
    99.  
    100. CallFields
    101. Screen.MousePointer = vbDefault
    102. End Sub
    103.  
    104. Private Sub Form_Unload(Cancel As Integer)
    105. tbAcademicYear.Close
    106. End Sub

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: .RecordCount is always -1

    Use either adOpenKeyset or adOpenStatic as the CursorType for server side cursors or use a client side cursor. Client side cursors use only adOpenStatic for CursorTypes regardless of which CursorType you select.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    84

    Re: .RecordCount is always -1

    still not working

  4. #4
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    Re: .RecordCount is always -1

    .recordcount can be problematic. If you are only using it though to see if there is a record then just use:

    VB Code:
    1. If .EOF = False Then
    2.       msgbox "There Is at least 1 Record"
    3. End If

    Edit: Not problematic any more after reading this thread.
    Last edited by ggettings; Feb 9th, 2007 at 12:06 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    84

    .AbosulutePosition is always = -3

    whta will i do if my .absoluteposition is always = -3

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    84

    Re: .RecordCount is always -1

    my .absoluteposition is always = -3

  7. #7
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: .RecordCount is always -1

    rs.CursorType = adOpenStatic

    should fix the problem.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: .RecordCount is always -1

    Quote Originally Posted by Besoup
    rs.CursorType = adOpenStatic

    should fix the problem.
    This WILL fix the problem. I use it all the time when I need recordcounts.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    84

    Re: .RecordCount is always -1

    then its running now but my .absoluteposition is always = -3

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: .RecordCount is always -1

    .RecordCount equal to -1 means that the client side does not yet know the number of rows in the recordset.

    This always means there are some rows - you will never get -1 when the true recordcount is 0.

    In order to get the recordcount when it's showing as -1 you need to "read" all the rows - MOVELAST or something to that affect.

    but - in my opinion - you never need to know the record count specifically...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    84

    Re: .RecordCount is always -1

    its running now but my .absoulteposition is always = -3

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: .RecordCount is always -1

    Immediately after you get the recordset do a MOVELAST followed by a MOVEFIRST - this should resolve the RECORDCOUNT immediately...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: .RecordCount is always -1

    Hehehe.... that's what we call "Jiggling the Recordset" around here....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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