Results 1 to 7 of 7
  1. #1
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64

    how to re-run a search if nothing was found

    I have a button on a form that gives the user an opportunity to search for a record that contains the address that the user types into an InputBox.

    If nothing is found, a MsgBox notifies the user and then closes which returns the user back to the form

    What I would like to do is give the user the option to search again if nothing is found instead of them having to click the search button on the form. What would be the best way to accomplish this?



    Here's the current code:

    Code:
    Private Sub SearchByAddress()
        Dim S, LN As String
        S = InputBox("Enter the address of the property" & vbCrLf & "(partial address search works as well)", "Find A Property by address")
        If S = "" Then Exit Sub
        LN = Nz(DLookup("PropertyAddress1", "Property_T", "[PropertyAddress1] LIKE ""*" & S & "*"""), "")
        If LN = "" Then
            MsgBox "That address was not found.", vbInformation, "Search Result"
            Exit Sub
        Else
            DoCmd.OpenForm "ViewProperty_F", , , "[PropertyAddress1] = " & " '" & LN & "'"
            Forms!ViewProperty_F.Filter = "PropertyAddress1 LIKE ""*" & S & "*"""
            Forms!ViewProperty_F.FilterOn = True
            Forms!ViewProperty_F.SearchFilter = "ON"
            Forms!ViewProperty_F.SearchFilter.BackColor = vbRed
        End If
    
    End Sub

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    This should work. I VERY seldom use goto, but in this case it's simple and appropriate. Others might favor a Do While...Loop construct.

    Code:
    Private Sub SearchByAddress()
        Dim S as String, LN As String
    TryAgain:
        S = InputBox("Enter the address of the property" & vbCrLf & "(partial address search works as well)", "Find A Property by address")
        If S = "" Then Exit Sub
        LN = Nz(DLookup("PropertyAddress1", "Property_T", "[PropertyAddress1] LIKE ""*" & S & "*"""), "")
        If LN = "" Then
            If MsgBox ("That address was not found." & vbCrlF _
                & "Would you like another Search?", vbQuestion+vbYesNo, "Search Result") = vbYes then
                goto TryAgain
            else    
                Exit Sub
            End If    
        Else
            DoCmd.OpenForm "ViewProperty_F", , , "[PropertyAddress1] = " & " '" & LN & "'"
            Forms!ViewProperty_F.Filter = "PropertyAddress1 LIKE ""*" & S & "*"""
            Forms!ViewProperty_F.FilterOn = True
            Forms!ViewProperty_F.SearchFilter = "ON"
            Forms!ViewProperty_F.SearchFilter.BackColor = vbRed
        End If
    End Sub

  3. #3
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    @davegri I thought about using a while loop for this, but I kinda like your goto solution. Is there an issue or problem with GoTo?

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    In the very early days of coding, especially in COBOL, looping structures were not available, and GOTO was used extensively. Coding was done with pencil and paper, then transferred to punched cards for input to the computer. The resulting program was printed out (with or without errors) on that 20 inch wide folding printer paper (nobody had video terminals yet). With all those GOTOs you had to constantly flip back and forth thru all those pages to try and find the target code. The target code could issue another GOTO and on and on. No code automatically returned to the line after the GOTO. This was defined as 'spaghetti code', and nobody liked it. So when better coding procedures became available, GOTO was cast into Hell, and anyone who continued to use it was deemed the Devil's disciple.

  5. #5
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    I remember GoTo from the Basic programming days. Thanks for the solution (works like a charm) and the trip down memory lane!

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,559
    Yet we have to use it for any error procedures?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    What would be the best way to accomplish this?
    IMO, don't use input box. Create your own form and improve the power and flexibility of the process. Not only could a form contain as many controls as you need to assist in the process it can remain open if nothing is found. Additionally, you could use FAYT to show results related to the input as it is typed in.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 6
    Last Post: 04-20-2018, 04:45 AM
  2. VBA Run Time Error - Table Not Found
    By lzook88 in forum Programming
    Replies: 0
    Last Post: 02-27-2016, 12:26 PM
  3. Run-Time Error '3265': Item Not Found In This Collection
    By Voodeux2014 in forum Programming
    Replies: 3
    Last Post: 01-29-2016, 09:04 AM
  4. All Access Objects shows nothing
    By Viwadd in forum Access
    Replies: 5
    Last Post: 05-29-2009, 02:23 PM
  5. Criteria added to Query - Nothing showing
    By eabtx in forum Queries
    Replies: 1
    Last Post: 03-02-2009, 10:06 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