Results 1 to 9 of 9
  1. #1
    Adish is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    15

    search code for vba

    Hi All.

    I have a search form as per the image attached. I want to strings for the unit no and suffix into my string as below but get an error message. Any help would be appreciated. Also when I select a street name from my drop down combo box. nothing gets populated in the bottom fields. the house number, locality and postcode gets populated but not the selected street from the combo box. I have set the data source of my combo box to the street table with street number and street name fields and set the bound column to 1 which is the street no as the primary key in the property sheet.
    Also when I comment out the code in red then my immediate window result is ...(SELECT dbo_NUCADDRESS.*, dbo_NUCSTREET.STREET_NAME FROM dbo_NUCADDRESS INNER JOIN dbo_NUCSTREET ON dbo_NUCADDRESS.STREET_NO = dbo_NUCSTREET.STREET_NO WHERE HOUSE_NO=112 AND dbo_NUCSTREET.STREET_NO=6206) But this does return the street number but does not populate the form with selected streetname as it should


    If OptionGroup.Value = 1 Then
    Dim strSQL As String

    If Not IsNull(Me.HouseNo) Then
    strSQL = "HOUSE_NO=" & Me.HouseNo


    End If

    If Not IsNull(Me.StreetID) Then
    If Len(strSQL) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "dbo_NUCSTREET.STREET_NO=" & Me.StreetID
    End If


    If Not IsNull(Me.Suffix) Then strSQL = strSQL & " AND "
    strSQL = strSQL & "HOUSE_NO_SUFFIX=" & Me.Suffix
    End If

    If Not IsNull(Me.UnitNo) Then strSQL = strSQL & " AND "
    strSQL = strSQL & "UNIT_NO=" & Me.UnitNo
    End If


    strSQL = "SELECT dbo_NUCADDRESS.*, dbo_NUCSTREET.STREET_NAME " _
    & "FROM dbo_NUCADDRESS INNER JOIN dbo_NUCSTREET ON dbo_NUCADDRESS.STREET_NO = dbo_NUCSTREET.STREET_NO " _
    & " WHERE " & strSQL
    ' Debug.Print strSQL

    Me.Property_Information.Form.RecordSource = strSQL
    Attached Thumbnails Attached Thumbnails search_image.png  

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,901
    strSQL = strSQL & " AND " must be on another line or you need another If Then line.

    Post lengthy code with [CODE] tags and indentation will be retained and code is more readable.

    Why set the RecordSource? Why not just filter the records by setting Filter and FilterOn properties with strWHERE?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Adish is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    15
    Hi , sorry are you able to do a quick e.g. I don't understand. Just started Access and VBA still in early stages

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,901
    See your code:

    If Not IsNull(Me.Suffix) Then strSQL = strSQL & " AND "
    strSQL = strSQL & "HOUSE_NO_SUFFIX=" & Me.Suffix
    End If


    This is lacking an If Then to go with the End If. The following will not compile error.

    If Not IsNull(Me.Suffix) Then
    strSQL = strSQL & " AND "
    strSQL = strSQL & "HOUSE_NO_SUFFIX=" & Me.Suffix
    End If

    But maybe you really want something like was done for the StreetID:

    If Not IsNull(Me.Suffix) Then
    If Len(strSQL) > 0 Then strSQL = strSQL & " AND "
    strSQL = strSQL & "HOUSE_NO_SUFFIX=" & Me.Suffix
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Adish is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    15

    search code for vba

    Now I have the combo box returning the string but the 2 codes in red font still gives an error as in the attachment. My immediate window has this result now........ SELECT dbo_NUCADDRESS.*, dbo_NUCSTREET.STREET_NAME FROM dbo_NUCADDRESS INNER JOIN dbo_NUCSTREET ON dbo_NUCADDRESS.STREET_NO = dbo_NUCSTREET.STREET_NO WHERE HOUSE_NO=211 AND dbo_NUCSTREET.STREET_NO=6206 AND UNIT_NO= AND HOUSE_NO_SUFFIX=

    I don't want to be checking for unit_no and suffix if it is null and if the address does not contain that at all. Only check and search if it is in address. But error comes up

    Code:
    Private Sub SearchButton_Click()
    If OptionGroup.Value = 1 Then
    Dim strSQL As String
    
        If Not IsNull(Me.HouseNo) Then
            strSQL = "HOUSE_NO=" & Me.HouseNo
        End If
        If Not IsNull(Me.StreetID) Then
            If Len(strSQL) > 0 Then strSQL = strSQL & " AND "
            strSQL = strSQL & "dbo_NUCSTREET.STREET_NO=" & Me.StreetID
        End If
        
        If Not IsNull(Me.UnitNo) Then
            strSQL = strSQL & " AND " & "UNIT_NO=" & Me.UnitNo
        End If
        If Not IsNull(Me.Suffix) Then
            strSQL = strSQL & " AND " & "HOUSE_NO_SUFFIX=" & Me.Suffix
        End If
        
        strSQL = "SELECT dbo_NUCADDRESS.*, dbo_NUCSTREET.STREET_NAME  " _
                & "FROM dbo_NUCADDRESS INNER JOIN dbo_NUCSTREET ON dbo_NUCADDRESS.STREET_NO = dbo_NUCSTREET.STREET_NO " _
                & " WHERE " & strSQL
        Debug.Print strSQL
        
        Me.Property_Information.Form.RecordSource = strSQL    
    End If
    End Sub
    Attached Thumbnails Attached Thumbnails error.png  

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,901
    Suffix is probably a text field. Text field parameters must be within apostrophe delimiters.

    strSQL = strSQL & " AND HOUSE_NO_SUFFIX='" & Me.Suffix & "'"

    Date parameters would use #.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Adish is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    15
    used the line with apostrophe delimiters but still same error. my immediate window result is ...SELECT dbo_NUCADDRESS.*, dbo_NUCSTREET.STREET_NAME FROM dbo_NUCADDRESS INNER JOIN dbo_NUCSTREET ON dbo_NUCADDRESS.STREET_NO = dbo_NUCSTREET.STREET_NO WHERE HOUSE_NO=211 AND dbo_NUCSTREET.STREET_NO=6206 AND UNIT_NO= AND HOUSE_NO_SUFFIX=''

    It should not be assigning unit no and suffix to string if it is null. Only assign if not null which is what I am checking in my code. ...If Not IsNull(Me.UnitNo) Then

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,901
    Then UnitNo and Suffix are not actually Null. Could there be empty string in the controls? To handle possibility of Null or empty string:

    If Me.UnitNo & "" <> "" Then

    Step debug the code. Check that values are what you expect and code runs as expected. See where it deviates and why. Refer to link at bottom of my post for debugging guidelines.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Adish is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2015
    Posts
    15
    Hi, Thanks. It worked and didn't give error

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

Similar Threads

  1. Replies: 3
    Last Post: 01-04-2015, 06:09 PM
  2. VBA Search code not working
    By rmrha21 in forum Programming
    Replies: 3
    Last Post: 12-02-2014, 10:59 AM
  3. Help me in my search code
    By lyndonguitar in forum Queries
    Replies: 1
    Last Post: 03-23-2013, 11:04 AM
  4. Search for a String in VBA code.
    By dandoescode in forum Access
    Replies: 3
    Last Post: 06-21-2012, 11:00 AM
  5. VBA search code
    By Duncan in forum Access
    Replies: 6
    Last Post: 04-07-2012, 11:30 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