How can I search two fields from one table at one time?

Posted on 2011-10-07
Last Modified: 2012-05-12
I need to search a table called tblsubjects, for either by the name (field subjectlast) or case number (field casenumber) I want to use a text box on my form to enter the search criteria. After the criteria has been entered I would like to populate a sub form with the results. i would like the search to be a close match, it does not have to be and exact match. Any ideas how to get started in the right direction?
Question by:seanlhall
    LVL 12

    Expert Comment

    you dont need a sub form, you can use a continuous form.
    on the form's header , put an unbound textbox (say you call it txtFilter) for the user to fill the search criteria, and a command button "Search"
    now write this code in the Click event of the button:
    if txtFilter<>"" then
         Me.Filter="SubjectLast Like '*" & txtFilter & "*'
    End If

    now, i didnt understand the other field's thing (CaseNumber), the same search term is for both at the same time or the user needs to choose wether to search for matching SubjectLast OR CaseNumbers?
    LVL 33

    Accepted Solution

    How will the field to be searched be determined?

    If you used 2 textboxes you could use something like this to filter by or as criteria.
    If IsNull(txtSubject) Then
          strFilter = "subjectlast Like '*'"
         strFilter = "subjectlast Like '*" & txtSubject.Value & "*'"
    End If
    strFilter = strFilter & " Or "
    If IsNull(txtCaseNo) Then
          strFilter = strFilter & " casenumber Like '*'"
         strFilter = strFilter & " casenumber Like '*" & txtCaseNo.Value & "*'"
    End If

    Open in new window

    LVL 30

    Expert Comment

    Try this:
    Code filters Folder names if either FolderFrom or FolderTo (a copy utility) contanis characters typed in Text1 box.
    Subform control name is B.
    The code is in the Command1_Click event on the main form.

    Private Sub Command3_Click()
        B.Form.FilterOn = False
        B.Form.Filter = "FolderFrom Like '*' & text1 & '*' OR FolderTo Like '*' & text1 & '*'"
         B.Form.FilterOn = True
    End Sub
    LVL 30

    Expert Comment

    Commad1=> Command3

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
    Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
    In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now