Getting the correct count from a DAO recordset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kcdoell
    New Member
    • Dec 2007
    • 230

    Getting the correct count from a DAO recordset

    Good Morning World:

    I have written the following code:

    [code=vb]
    'Gives the user to delete and replace all records for a specified parameter

    LockSQL = "SELECT * FROM tblStaticAllFor ecast WHERE" & _
    " DivisionIDFK = " & Val(Me.cboDivis ion.Value)

    Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenR ecordset(LockSQ L)
    recordexists = rst.RecordCount

    MsgBox "The number of records you are about to delete is " & recordexists & "." & _
    " Click the ok button to proceed", vbOKCancel, vbDefaultButton 2


    If Nz(recordexists , 0) > 0 Then

    'code will delete the records that the user has selected....... .......

    End If
    End If
    End Sub
    [/code]

    Before I proceed with a code to delete the selected records from my "LockSQL" I wanted to make sure the count was correct. I know that I have 14 records in my tblStaticAllFor ecast that are all tagged with division set to value 2. So I should of recieved a count of 14 when I set the parameter in my form and ran my code, instead I got a count in my message box for 1. Does anybody know why that would happened? I ran a separate query via Access looking at the same parameters on the form and it got the correct count (14). Is somehow the count different when using a DAO recordset or is something wrong in my code??

    Thanks for any thoughts,

    Keith.
  • Scott Price
    Recognized Expert Top Contributor
    • Jul 2007
    • 1384

    #2
    On Line 4, why are you using the Val() function?

    Regards,
    Scott
    Last edited by Scott Price; Mar 27 '08, 03:00 PM. Reason: clearer wording

    Comment

    • kcdoell
      New Member
      • Dec 2007
      • 230

      #3
      I am a Newbie at VBA, so that is something I got out of a book I have been reading:

      Microsoft Access VBA Programming by Michael Vine

      Comment

      • kcdoell
        New Member
        • Dec 2007
        • 230

        #4
        I solve it:

        [Code=vb]
        rst.MoveLast 'Move to last record
        recordexists = rst.RecordCount
        [/code]

        Thanks,

        Keith.

        Comment

        • dbpros
          New Member
          • Mar 2008
          • 15

          #5
          I always do a "rs.movelas t" followed by an "rs.movefir st" before executing the rs.recordcount.

          It always gives the correct number of records in the recordset.

          good luck

          Comment

          • kcdoell
            New Member
            • Dec 2007
            • 230

            #6
            Thanks, I just received an error message "No current record. (Error 3021)" when the record count is zero. How would I handle that scenario??

            Keith.

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by kcdoell
              Good Morning World:

              I have written the following code:

              [code=vb]
              'Gives the user to delete and replace all records for a specified parameter

              LockSQL = "SELECT * FROM tblStaticAllFor ecast WHERE" & _
              " DivisionIDFK = " & Val(Me.cboDivis ion.Value)

              Dim rst As DAO.Recordset
              Set rst = CurrentDb.OpenR ecordset(LockSQ L)
              recordexists = rst.RecordCount

              MsgBox "The number of records you are about to delete is " & recordexists & "." & _
              " Click the ok button to proceed", vbOKCancel, vbDefaultButton 2


              If Nz(recordexists , 0) > 0 Then

              'code will delete the records that the user has selected....... .......

              End If
              End If
              End Sub
              [/code]

              Before I proceed with a code to delete the selected records from my "LockSQL" I wanted to make sure the count was correct. I know that I have 14 records in my tblStaticAllFor ecast that are all tagged with division set to value 2. So I should of recieved a count of 14 when I set the parameter in my form and ran my code, instead I got a count in my message box for 1. Does anybody know why that would happened? I ran a separate query via Access looking at the same parameters on the form and it got the correct count (14). Is somehow the count different when using a DAO recordset or is something wrong in my code??

              Thanks for any thoughts,

              Keith.
              Many times, in order to get an accurate count of Records in a Recordset, you have to 'Traverse' the Recordset as in:
              [CODE=vb]
              Dim rst As DAO.Recordset

              Set rst = CurrentDb.OpenR ecordset(LockSQ L)

              rst.MoveLast
              rst.MoveFirst

              'Now, will return the correct Record Cound
              recordexists = rst.RecordCount[/CODE]

              Comment

              • kcdoell
                New Member
                • Dec 2007
                • 230

                #8
                Thanks:

                I did that and solved my error issue on a zero account:

                [code=vb]
                Dim rst As DAO.Recordset
                Set rst = CurrentDb.OpenR ecordset(LockSQ L)
                recordexists = rst.RecordCount

                'If no records are found
                If recordexists = 0 Then
                MsgBox "There are no records to delete."
                Else

                rst.MoveLast 'Move to last record
                rst.MoveFirst 'Move to First record

                If MsgBox("The number of records you are about to delete is " & recordexists & "." & _
                " Click the ok button to proceed", vbOKCancel, vbDefaultButton 2) = vbOK Then

                If Nz(recordexists , 0) > 0 Then

                'code will delete the records that the user has selected.
                [/code]

                Thanks to all.

                Keith.

                Comment

                Working...