Results 1 to 3 of 3
  1. #1
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82

    rs.NoMatch Not behaving the way it should

    Hi All



    I am trying to return information from a record to an unbound text box based on information in a bound text box. I have the procedure working perfectly in an Form_Current() when it is retrieving information from a table. When I try and replicate the code and base it on a query I get an error when it fails the "If rs.NoMatch = True" criteria.

    The error says Invalid use of Null

    Anyone have any ideas??

    See the below code.

    Code:
    Dim rs As DAO.Recordset
        Dim LastSerial As String
        
        Set rs = CurrentDb.OpenRecordset("qry11OrderHistory", dbOpenSnapshot, dbReadOnly)
        
        rs.FindLast "AccNo='" & Me.AccNo & "'"
        
        LastSerial = DLookup("EndSerialNo", "qry11OrderHistory", "AccNo ='" & Me.AccNo.Value & "'")
        
        If rs.NoMatch = True Then
            MsgBox "No Details with that account no on file", vbInformation, "Oops"
            Exit Sub
        Else
            Forms![frmApprove11]![txtSerialNo] = LastSerial
        End If

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    The error may be here:

    LastSerial = DLookup("EndSerialNo", "qry11OrderHistory", "AccNo ='" & Me.AccNo.Value & "'")

    You have declared LastSerial as a string, but if the dllokup finds no match, it returns a Null, which causes an error when you attempt to assign the value to the string variable.

    Move that line to the Else part of your If and it should work.

    Actually, you can do what you want without using a recordset by using the Dlookup and the Nz function:

    Code:
     
        Dim LastSerial As String
        LastSerial = nz(DLookup("EndSerialNo", "qry11OrderHistory", "AccNo ='" & Me.AccNo.Value & "'"),"N/A")
        
        If lastSerial = "N/A"  then
            MsgBox "No Details with that account no on file", vbInformation, "Oops"
            Exit Sub
        Else
            Forms![frmApprove11]![txtSerialNo] = LastSerial
        End If
    The Nz function assigns a value of "N/A" to LastSerial instead of an actual serial number.

  3. #3
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82
    John_G

    All Makes perfect sense... (blushes that he should have got this)

    Worked perfectly, and it will come in handy further down the line.

    Thank you!!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2014, 01:20 PM
  2. Recordset not behaving as expected
    By jgelpi16 in forum Programming
    Replies: 4
    Last Post: 02-15-2011, 04:25 PM
  3. intellisense not behaving
    By avianrand in forum Programming
    Replies: 3
    Last Post: 06-18-2010, 04:59 PM
  4. Replies: 21
    Last Post: 12-27-2009, 09:19 PM
  5. Replies: 0
    Last Post: 10-21-2008, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums