Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:46:15+00:00 2026-06-13T16:46:15+00:00

I have a Search form in my database that takes in a string, explodes

  • 0

I have a “Search” form in my database that takes in a string, explodes it into an array and builds an SQL string based on a selected join type (“Or”, “And”, or “Exact Phrase” radio buttons). My code works perfectly 95% of the time, but every once and while the database hangs when I switch between the different join types and requery. I have confirmed that the SQL is being created properly and I think that the problem stems from trying to change the subform’s recordsource while it is still loading.

The exact way that my search form works is as follows:

  1. The user puts a search term/phrase in a text box
  2. On the “After Update” event of the textbox, VBA creates an SQL string and stores it in a hidden text field (dubbed “ModifiedSearchValue”)
  3. If the user changes the join type (radiobuttons with options “Or”, “And”, Or “Exact Phrase”) the “After Update” event on the group evokes the VBA sub (as in #2) and VBA creates an SQL string which it stores in the hidden text field (dubbed “ModifiedSearchValue”)
  4. When the user hits the “Search” button, VBA sets the RecordSource of the subform to the value of “ModifiedSearchValue” by:

    Me!Results.Form.RecordSource = Me.ModifiedSearchValue

Again, this works perfectly most of the time, but if you enter the search term, click “Search”, then change the join type and hit “Search” again, it causes the database to hang approximately 5% of the time.

My main VBA code is as follows

    Private Sub SearchString()
Dim SearchString, SearchStringTitle, SearchStringName, SearchStringDescription, SearchStringInvestigator, JoinValue, j, SQLString As String, SearchArray, varValue As Variant

    SearchString = Trim(Me.SearchValue)

    If Not IsNull(SearchString) Then
        SearchArray = Split(SearchString, " ")

        SQLString = "SELECT tbl_Studies.StudyID, tbl_Studies.Study_Short_Title, tbl_Studies.Study_Name, tbl_Studies.Study_Description, [qry_General:FullName_FMLD].FullName AS Investigator, tbl_Studies.Project_Type, IIf([Project_Type]=1,[tbl_Studies:Status]![Status],[tbl_Studies:NR_Status]![NR_Status]) AS Overall_Status, tbl_Studies.Date_Submitted, tbl_Studies.Date_Updated, tbl_Studies.Results_Summary, tbl_Studies.Inactive " & _
                    "FROM ([tbl_Studies:NR_Status] RIGHT JOIN ([tbl_Studies:Status] RIGHT JOIN tbl_Studies ON [tbl_Studies:Status].StatusID = tbl_Studies.Status) ON [tbl_Studies:NR_Status].NR_StatusID = tbl_Studies.NR_Status) LEFT JOIN [qry_General:FullName_FMLD] ON tbl_Studies.Investigator = [qry_General:FullName_FMLD].PersonID " & _
                    "WHERE "

        If Me.Join_Type <> 3 Then

            If Me.Join_Type = 1 Then
                JoinValue = "OR"
            ElseIf Me.Join_Type = 2 Then
                JoinValue = "AND"
            Else
                JoinValue = " "
            End If

    '--
            SearchStringTitle = "(("
            For Each varValue In SearchArray
                j = Trim(varValue)
                SearchStringTitle = SearchStringTitle & "(tbl_Studies.Study_Short_Title) Like ""*" & j & "*"""

                If varValue <> SearchArray(UBound(SearchArray)) Then
                    SearchStringTitle = SearchStringTitle & " " & JoinValue & " "
                End If
            Next varValue
            SearchStringTitle = SearchStringTitle & "))"

    '--
            SearchStringName = "(("
            For Each varValue In SearchArray
                j = Trim(varValue)
                SearchStringName = SearchStringName & "(tbl_Studies.Study_Name) Like ""*" & j & "*"""

                If varValue <> SearchArray(UBound(SearchArray)) Then
                    SearchStringName = SearchStringName & " " & JoinValue & " "
                End If
            Next varValue
            SearchStringName = SearchStringName & "))"

    '--
            SearchStringDescription = "(("
            For Each varValue In SearchArray
                j = Trim(varValue)
                SearchStringDescription = SearchStringDescription & "(tbl_Studies.Study_Description) Like ""*" & j & "*"""

                If varValue <> SearchArray(UBound(SearchArray)) Then
                    SearchStringDescription = SearchStringDescription & " " & JoinValue & " "
                End If
            Next varValue
            SearchStringDescription = SearchStringDescription & "))"

    '--
            SearchStringInvestigator = "(("
            For Each varValue In SearchArray
                j = Trim(varValue)
                SearchStringInvestigator = SearchStringInvestigator & "([qry_General:FullName_FMLD].FullName) Like ""*" & j & "*"""

                If varValue <> SearchArray(UBound(SearchArray)) Then
                    SearchStringInvestigator = SearchStringInvestigator & " " & JoinValue & " "
                End If
            Next varValue
            SearchStringInvestigator = SearchStringInvestigator & "))"

            SearchString = SearchStringTitle & " OR " & SearchStringName & " OR " & SearchStringDescription & " OR " & SearchStringInvestigator
        Else
            SearchStringTitle = "(((tbl_Studies.Study_Short_Title) Like ""*" & SearchString & "*""))"
            SearchStringName = "(((tbl_Studies.Study_Name) Like ""*" & SearchString & "*""))"
            SearchStringInvestigator = "((([qry_General:FullName_FMLD].FullName) Like ""*" & SearchString & "*""))"
            SearchStringDescription = "(((tbl_Studies.Study_Description) Like ""*" & SearchString & "*""))"

            SearchString = SearchStringTitle & " OR " & SearchStringName & " OR " & SearchStringDescription & " OR " & SearchStringInvestigator
        End If

        SearchString = SQLString & SearchString & ";"

        Me.ModifiedSearchValue.Value = SearchString
    End If
End Sub

Again, my theory is that the hanging is caused by changing the RecordSource of the subform before it has finished loading from the previous search, but I can’t seem to determine any workaround.

Thanks in advance for any and all insight/help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:46:16+00:00Added an answer on June 13, 2026 at 4:46 pm

    As per Olivier’s suggestions, the true cause of the problem was VBA being called as part of the query [qry_General:FullName_FMLD]; switching to [qry_General:FullName_FML] (which doesn’t call any VBA) eliminated all problems. I am guessing that the root of the problem was that the form was attempting to apply the filter before the query had returned a result thereby creating a corrupt filter string.

    Here is the updated code using a filter method and replacing all bangs with dots:

    Private Sub Search_Click()
        On Error GoTo Err_Search_Click
    
        Dim SearchString, SearchStringTitle, SearchStringName, SearchStringDescription, SearchStringInvestigator, JoinValue, j, SQLString As String, SearchArray, varValue As Variant
    
            Me.Results.Form.FilterOn = True
            SearchString = Trim(Me.SearchValue)
    
            If Not IsNull(SearchString) Then
                SearchArray = Split(SearchString, " ")
    
                If Me.Join_Type <> 3 Then
    
                    If Me.Join_Type = 1 Then
                        JoinValue = "OR"
                    ElseIf Me.Join_Type = 2 Then
                        JoinValue = "AND"
                    Else
                        JoinValue = " "
                    End If
    
            '--
                    SearchStringTitle = "(("
                    For Each varValue In SearchArray
                        j = Trim(varValue)
                        SearchStringTitle = SearchStringTitle & "(tbl_Studies.Study_Short_Title) Like ""*" & j & "*"""
    
                        If varValue <> SearchArray(UBound(SearchArray)) Then
                            SearchStringTitle = SearchStringTitle & " " & JoinValue & " "
                        End If
                    Next varValue
                    SearchStringTitle = SearchStringTitle & "))"
    
            '--
                    SearchStringName = "(("
                    For Each varValue In SearchArray
                        j = Trim(varValue)
                        SearchStringName = SearchStringName & "(tbl_Studies.Study_Name) Like ""*" & j & "*"""
    
                        If varValue <> SearchArray(UBound(SearchArray)) Then
                            SearchStringName = SearchStringName & " " & JoinValue & " "
                        End If
                    Next varValue
                    SearchStringName = SearchStringName & "))"
    
            '--
                    SearchStringDescription = "(("
                    For Each varValue In SearchArray
                        j = Trim(varValue)
                        SearchStringDescription = SearchStringDescription & "(tbl_Studies.Study_Description) Like ""*" & j & "*"""
    
                        If varValue <> SearchArray(UBound(SearchArray)) Then
                            SearchStringDescription = SearchStringDescription & " " & JoinValue & " "
                        End If
                    Next varValue
                    SearchStringDescription = SearchStringDescription & "))"
    
            '--
                    SearchStringInvestigator = "(("
                    For Each varValue In SearchArray
                        j = Trim(varValue)
                        SearchStringInvestigator = SearchStringInvestigator & "([qry_General:FullName_FML].FullName) Like ""*" & j & "*"""
    
                        If varValue <> SearchArray(UBound(SearchArray)) Then
                            SearchStringInvestigator = SearchStringInvestigator & " " & JoinValue & " "
                        End If
                    Next varValue
                    SearchStringInvestigator = SearchStringInvestigator & "))"
    
                    SearchString = SearchStringTitle & " OR " & SearchStringName & " OR " & SearchStringDescription & " OR " & SearchStringInvestigator
                Else
                    SearchStringTitle = "(((tbl_Studies.Study_Short_Title) Like ""*" & SearchString & "*""))"
                    SearchStringName = "(((tbl_Studies.Study_Name) Like ""*" & SearchString & "*""))"
                    SearchStringInvestigator = "((([qry_General:FullName_FML].FullName) Like ""*" & SearchString & "*""))"
                    SearchStringDescription = "(((tbl_Studies.Study_Description) Like ""*" & SearchString & "*""))"
    
                    SearchString = SearchStringTitle & " OR " & SearchStringName & " OR " & SearchStringDescription & " OR " & SearchStringInvestigator
                End If
    
                Me.Results.Form.Filter = SearchString
            End If
    
        Exit_Search_Click:
            Exit Sub
    
        Err_Search_Click:
            MsgBox ("There are no active records to review.")
            Resume Exit_Search_Click
    End Sub
    

    Again, the credit for this solution belongs to Olivier Jacot-Descombes – thanks for all your help and suggestions!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a search form that searches a table in my database. The table
I have a search form that needs to query another system's SQL Server 2005
I have a search form on my site that submits the url in the
(Using MySQL and PHP) I have a search form that will allow my users
I have a page that takes a number of parameters on a form and
I have an SQL query that takes these parameters: @SearchFor nvarchar(200) = null ,@SearchInLat
I have a search page published in a cms that locates database driven pages
I have a search form field that I want to compare with multiple columns
I have a search form in my app that uses a jQuery autocomplete plugin.
In a Windows Form, I have a search box that fires an event to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.