No Current Record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bard777
    New Member
    • Jan 2008
    • 23

    No Current Record

    I am running some queries on form load to get some summary info on a database.

    My query is summing records based on a priority field value (1, 2, or 3) and may find none. When there are no records that meet the criteria for a given priority the line that updates the textbox with the value errs out (below).

    here is my code for the textbox:

    Code:
    Me.txt_low_cnt = Nz((db.OpenRecordset(strSQL)(0)) & " ", "0")
    I tried an IF db.EOF, but that doesn't work.

    Thanks for any ideas.
  • Megalog
    Recognized Expert Contributor
    • Sep 2007
    • 378

    #2
    This NZ statement will never work correctly. You are adding " " to something, which in all cases will never return a null.

    This would be a proper example that would return "0" on a null value, but if you need that space in there after a value, you'll have to break this out a bit more to handle both scenarios.

    Code:
    Me.txt_low_cnt = Nz((db.OpenRecordset(strSQL)(0)), "0")

    Comment

    • bard777
      New Member
      • Jan 2008
      • 23

      #3
      Originally posted by Megalog
      This NZ statement will never work correctly. You are adding " " to something, which in all cases will never return a null.

      This would be a proper example that would return "0" on a null value, but if you need that space in there after a value, you'll have to break this out a bit more to handle both scenarios.

      Code:
      Me.txt_low_cnt = Nz((db.OpenRecordset(strSQL)(0)), "0")
      I do not need the space in there, I added that after what you suggested didn't work. I tried again in case I missed something the first time, but it still gives the "No Current Record" error.

      It seemed like "no current record" is other than a Null value, so Nz can't handle it. That is why I tried to catch it with EOF, but I had no recordset defined.

      Here is what worked:

      Code:
      Dim rsSum As Recordset
      
      
      Set db = CurrentDb
      
      strSQL = "<SQL HERE>"
      Set rsSum = db.OpenRecordset(strSQL)
      If Not rsSum.EOF Then
          Me.txt_high_cnt = rsSum(0)
      Else
          Me.txt_high_cnt = "0"
      End If

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32634

        #4
        A quicker and easier approach would be to use DCount().

        Comment

        Working...