Results 1 to 8 of 8
  1. #1
    Dazza666 is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2013
    Posts
    8

    Better search and filter VBA

    Hi,

    I'm trying to create a txt box that allows searching a subform of client records by either firstname surname or national insurance number, so the user could type the first name, surname and national insurance number or any combination of them and it would filter the sub form appropriately. I succeeded to a degree with the below code but it's very crude code and users must specify the in a specific order, first name, surname, national insurance number (or any combination in that order) I wondered if anyone could provide a better solution or point me in the right direction?



    Private Sub btnSearch_Click()
    If IsNull(Me.txtSearch) Then
    Else

    Dim strTxtArray() As String
    strTxtArray() = Split(Me.txtSearch)



    Dim strtxt1 As String
    strtxt1 = strTxtArray(0)

    If UBound(strTxtArray) = 1 Then
    Dim strtxt2 As String
    strtxt2 = strTxtArray(1)
    Else
    End If
    If UBound(strTxtArray) = 2 Then
    Dim strtxt3 As String
    strtxt3 = strTxtArray(2)

    Else
    End If

    Select Case UBound(strTxtArray)
    Case 0

    Me.frmClientDatasheet.Form.RecordSource = "SELECT Client.[First Name], Client.Surname, Client.NationalInsuranceNumber, Client.DateOfBirth, Client.ID FROM Client WHERE (((Client.[First Name]) Like '*'+[Forms]![InitialReferral]![txtSearch]+'*') OR (Client.[Surname]) like '*' + [FORMS]![InitialReferral]![TxtSearch] + '*' OR (Client.[NationalInsuranceNumber]) like '*' + [FORMS]![InitialReferral]![TxtSearch] + '*' )"
    Case 1

    Me.frmClientDatasheet.Form.RecordSource = "SELECT Client.[First Name], Client.Surname, Client.NationalInsuranceNumber, Client.DateOfBirth, Client.ID FROM Client WHERE (((Client.[First Name]) Like '*'+" & "'" & strtxt1 & "'" & "+ '*') and (Client.[Surname]) like '*' +" & "'" & strtxt2 & "'" & " + '*') "
    Case 2

    Me.frmClientDatasheet.Form.RecordSource = "SELECT Client.[First Name], Client.Surname, Client.NationalInsuranceNumber, Client.DateOfBirth, Client.ID FROM Client WHERE (((Client.[First Name]) Like '*'+" & "'" & strtxt1 & "'" & "+ '*') and (Client.[Surname]) like '*' +" & "'" & strtxt2 & "'" & " + '*' And (Client.[NationalInsuranceNumber]) like '*' +" & "'" & strtxt3 & "'" & " + '*' )"
    End Select

    End If

    End Sub

  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,521
    I would probably have a separate textbox for each possible input and build the SQL dynamically. The sample db here demonstrates this:

    http://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Dazza666 is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2013
    Posts
    8
    Quote Originally Posted by pbaldy View Post
    I would probably have a separate textbox for each possible input and build the SQL dynamically. The sample db here demonstrates this:

    http://www.baldyweb.com/BuildSQL.htm
    Hey,

    thanks for your response,

    I was trying to avoid multiple text boxes, I deal with a lot of people who do not really understand computers at all and if possible i'd like to keep the application as simple as possible (even if it drives up development time) I appreciate there may be a training issue there as well but i'm curious to see if it can be done?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    To me it's simpler (or more intuitive for the user) to have multiple textboxes, each with a clear purpose, than a single textbox that requires a specific format and order of entry. That said, it's your program, so you're free to design it any way you believe is best for your users.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Dazza666 is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2013
    Posts
    8
    Yeah I mean part of the point was I don't want them to have to enter it in any particular order that's kind of the basis for my question, to help improve upon what I have already done. but thanks anyway

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If you had 3 different types of data you might be able to, but programmatically how can you tell if they've entered "firstname, surname" or "surname, firstname"? I don't think you can, because it's all text.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Dazza666 is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2013
    Posts
    8
    Quote Originally Posted by pbaldy View Post
    If you had 3 different types of data you might be able to, but programmatically how can you tell if they've entered "firstname, surname" or "surname, firstname"? I don't think you can, because it's all text.
    Unless there is a better way to write the SQL string? Is it possible to run a "like" clause across multiple fields? ... WHERE client.firstname or client.surname or client.NationalInsuranceNumber LIKE strtxt1 or strtxt2 or strtxt3? Something like that, I obviously don't mind if the DB checks for 'Dave' in the NationalInsuranceNumber field so really i'm trying to ask it to check all words against all fields.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The syntax would be more like this pseudo-SQL:

    WHERE FirstName Like str1 OR FirstName Like str2 OR FirstName Like str3 OR LastName Like str1 OR LastName Like str2 OR LastName Like str3
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Another Search Filter Question
    By r0v3rT3N in forum Programming
    Replies: 4
    Last Post: 07-23-2012, 12:20 PM
  2. report filter search
    By accesshelpme in forum Access
    Replies: 1
    Last Post: 05-14-2012, 04:06 PM
  3. How to filter/search using a forum?
    By MediaCo in forum Access
    Replies: 3
    Last Post: 07-04-2011, 03:30 AM
  4. Query filter search
    By mseeker22 in forum Queries
    Replies: 6
    Last Post: 06-22-2011, 03:35 PM
  5. Database Search filter
    By dada in forum Programming
    Replies: 7
    Last Post: 08-19-2010, 12:42 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