Results 1 to 7 of 7
  1. #1
    Opid is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    24

    Null value in search

    Hello,



    Code:
    Private Sub searchTXT_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim rs As Object
    Dim lngBookmark As String
    Select Case KeyCode
    Case vbKeyReturn
    lngBookmark = searchTXT.Value
    DoCmd.OpenForm "updatedformTenant"
    
    
    Set rs = Forms!updatedformTenant.RecordsetClone
    rs.FindFirst "Last1 = '" & lngBookmark & "'"
    
    
    If rs.NoMatch Then
      DoCmd.GoToRecord acForm, "updatedformTenant", acNewRec    
    Else
      Forms!updatedformTenant.Bookmark = rs.Bookmark
    End If
    Set rs = Nothing
    KeyCode = 0
    End Select
    End Sub
    I am attempting to run this code when the user hits the enter key. For some reason lngBookMark = serachTXT.Value is giving me an Improper use of NULL exception. Any ideas?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You probably want the after update event. The key down event fires with every keystroke, so with the first one there's no value in the textbox yet.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Opid is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    24
    The code works perfectly fine in an on-click of a button. I was hoping to make it so the user wouldn't have to click on the button... Are you not able to access values in form fields in a KeyDown event?

  4. #4
    Opid is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    24
    Thank you that works perfectly. Put the code into after update and removed the switch case statement. Thank you so much.

    Code:
    Private Sub searchTXT_AfterUpdate()
    Dim rs As Object
    Dim lngBookmark As String
    lngBookmark = searchTXT
    DoCmd.OpenForm "updatedformTenant"
    'take it to the selected record
    Set rs = Forms!updatedformTenant.RecordsetClone
    rs.FindFirst "Last1 = '" & lngBookmark & "'"
    
    
    'the lines marked as optional can be included if there's a chance the record won't exist in the form being opened
    If rs.NoMatch Then   'optional - if no match, go to a new record
      DoCmd.GoToRecord acForm, "updatedformTenant", acNewRec   'optional
      'Forms!frmEmployeesDetail.txtEmpID = Me.txtEmpID  'optional - copy the employee ID from this form
    Else   'optional
      Forms!updatedformTenant.Bookmark = rs.Bookmark
    End If   'optional
    Set rs = Nothing
    KeyCode = 0
    End Sub

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    No problem. That code looks hauntingly familiar.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Opid is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    24
    Haha yeah you helped me out twice then

  7. #7
    Opid is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    24
    Found a little bug if the user erases whats in the the text fields and clicks out of the text field it throws a null exception again so add a null check:
    Code:
    Private Sub searchTXT_AfterUpdate()
    Dim rs As Object
    Dim lngBookmark As String
    'Check if the user has something in text field
    If (Not Nz(searchTXT, "") = "") Then
    lngBookmark = searchTXT
    DoCmd.OpenForm "updatedformTenant"
    'take it to the selected record
    Set rs = Forms!updatedformTenant.RecordsetClone
    rs.FindFirst "Last1 = '" & lngBookmark & "'"
    
    
    'the lines marked as optional can be included if there's a chance the record won't exist in the form being opened
    If rs.NoMatch Then
      rs.FindFirst "First1 = '" & lngBookmark & "'"
      If rs.NoMatch Then
      DoCmd.Close
      MsgBox "No tenant with that name!"
      Else
      Forms!updatedformTenant.Bookmark = rs.Bookmark
      End If
    Else
    Forms!updatedformTenant.Bookmark = rs.Bookmark
    End If
    Set rs = Nothing
    KeyCode = 0
    End If
    End Sub
    also thanks to pbaldy for original code

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

Similar Threads

  1. Replies: 7
    Last Post: 05-23-2012, 02:19 PM
  2. Replies: 1
    Last Post: 02-23-2012, 02:27 PM
  3. Search to ignore null fields
    By tommy93 in forum Queries
    Replies: 10
    Last Post: 02-07-2012, 10:58 AM
  4. Finding the Max Date and Null Values if Null
    By SpdRacerX in forum Queries
    Replies: 1
    Last Post: 02-03-2012, 06:29 AM
  5. Query search wont display null fields
    By Coffee in forum Queries
    Replies: 2
    Last Post: 08-08-2011, 09:00 AM

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