Results 1 to 3 of 3
  1. #1
    Fionfion is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Oct 2017
    Posts
    25

    Red face Null value error

    Hi all, I have been working on a database with search function, been looking everywhere and came up with the below code:


    Code:
    Private Sub searchbutton_Click()
    
    
    
    
    Dim strsearch As String
    Dim strText As String
    
    
    strText = Me.[txtsearch].Value
    
    
    
    
    If searchoption = 1 Then 
    strsearch = "SELECT * from [customer] where ([acc#] like ""*" & strText & "*"")"
    
    
    ElseIf searchoption = 2 Then 
    strsearch = "SELECT * from [customer] where ([customername] like ""*" & strText & "*"")"
    
    
    Else 
    strsearch = "SELECT * from [customer] where ([inv] like ""*" & strText & "*"")"
    
    
    
    
    End If
    
    
    
    
    Me.RecordSource = strsearch
    End Sub
    It works great, except when someone didn't type in the text box, it will return an error message with debug option due to null value - I tried the code On error goto 0 but it didn't work (probably because I do not know how), is there anyway to not show the error message? I just want it to do nothing when the value is null.



    Thanks!!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you cant search on nothing.
    you have to validate it before you run it.



    Code:
    '----------------
    sub btnFilter_click()
    '----------------
    dim sWhere as string 
    
    
    sWhere = "1=1"
    if not IsNUll(cboST) then sWhere = sWhere & " and [State]='" & cboST & "'"
    if not IsNUll(cboCity) then sWhere = sWhere & " and [city]='" & cboCity & "'"
    if not IsNUll(cboZip) then sWhere = sWhere & " and [ZipCode]='" & cboZip & "'"
    
    
    If sWhere = "1=1" Then
      Me.FilterOn = False
    Else
      Me.Filter = sWhere
      Me.FilterOn = True
    End If
    end sub

  3. #3
    Fionfion is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Oct 2017
    Posts
    25
    Quote Originally Posted by ranman256 View Post
    you cant search on nothing.
    you have to validate it before you run it.



    Code:
    '----------------
    sub btnFilter_click()
    '----------------
    dim sWhere as string 
    
    
    sWhere = "1=1"
    if not IsNUll(cboST) then sWhere = sWhere & " and [State]='" & cboST & "'"
    if not IsNUll(cboCity) then sWhere = sWhere & " and [city]='" & cboCity & "'"
    if not IsNUll(cboZip) then sWhere = sWhere & " and [ZipCode]='" & cboZip & "'"
    
    
    If sWhere = "1=1" Then
      Me.FilterOn = False
    Else
      Me.Filter = sWhere
      Me.FilterOn = True
    End If
    end sub

    It worked!! Thanks so much for your help!!

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

Similar Threads

  1. Invalid use of Null in Module error
    By Tom123456 in forum Modules
    Replies: 3
    Last Post: 10-13-2017, 07:10 AM
  2. DMAX Error when Null
    By grant99 in forum Programming
    Replies: 3
    Last Post: 04-27-2016, 01:10 PM
  3. Invalid use of NULL error
    By CHEECO in forum Access
    Replies: 13
    Last Post: 03-16-2016, 07:20 PM
  4. Preventing a Null error
    By tylerg11 in forum Forms
    Replies: 15
    Last Post: 06-23-2012, 11:18 PM
  5. Error 94: Invalid Use of Null
    By athomas8251 in forum Forms
    Replies: 3
    Last Post: 11-09-2011, 11:46 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