Results 1 to 2 of 2
  1. #1
    Ganymede is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    64

    Set Listbox Filter w/ Multiple Textboxes

    I'm trying to create a search form for my database with the following three fields: Claimant (name), Claimant ID and Claim ID. Ideally, I'd like it to look something like this:





    (I put a 3 in the claimant field so no names would appear on the image).

    Essentially, I'd like the listbox to display search results based on the criteria of all three textboxes (or less if some are left blank). Thus far, I can only get the listbox to display criteria from one of the textboxes.

    The claimant textbox = txtPropertyClaimant
    The listbox = lstSearchResults

    Change event for txtProperty (when it works):
    Code:
    Private Sub txtPropertyClaimant_Change()
    On Error GoTo Err_txtPropertyClaimant_Change
    Dim strSource As String
    strSource = "SELECT Claimant" & _
    "FROM ADRSearchQ " & _
    "Where Claimant Like '*" & Me.txtPropertyClaimant.Text & "*' " 
    Me.lstSearchResults.RowSource = strSource
    
    Exit_txtPropertyClaimant_Change: Exit Sub
    Err_txtPropertyClaimant_Change: MsgBox Err.Number & " " & Err.Description
    Resume Exit_txtPropertClaimanty_Change
    End Sub
    I'm aware that I could program txtPropertyClaimant to recognize data from either of Claimant, ClaimantID and/or ClaimID and consolidate all three into a single textbox. However, I really want to be able to search by more than one criteria at a time. What I attempted was something like this:

    The claimant textbox = txtPropertyClaimant
    The claimantID textbox = txtPropertyClaimantID
    The claimID textbox = txtPropertyClaimID
    The listbox = lstSearchResults

    And I would put the following code as a change event for txtPropertyClaimant, txtPropertyClaimantID and txtPropertyClaimID:

    Code:
    On Error GoTo Err_<NameofTextbox>_Change
    Dim strSource As String
    strSource = "SELECT Claimant, ClaimantID, ClaimID " & _
    "FROM ADRSearchQ " & _
    "Where Claimant Like '*" & Me.txtPropertyClaimant.Text & "*' " _
    & "Or ClaimantID Like '*" & Me.txtPropertyClaimantID.Text & "*' " _
    & "Or ClaimID Like '*" & Me.txtPropertyClaimID.Text & "*' " _
    Me.lstSearchResults.RowSource = strSource
    
    Exit_<NameofTextbox>_Change: Exit Sub
    Err_<NameofTextbox>_Change: MsgBox Err.Number & " " & Err.Description
    Resume Exit_<NameofTextbox>_Change
    When I try this, I get error code 2185 "You can't reference a property or method for a control unless the control has the focus."

    I assume that this is appearing because I reference the "property or method" for all three textboxes in the macro. Is there any way around this?

  2. #2
    pdebaets is offline Competent Performer
    Windows Vista Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Los Angeles
    Posts
    235
    Try this when setting the value of strSource:

    strSource = "SELECT Claimant, ClaimantID, ClaimID " & _
    "FROM ADRSearchQ " & _
    "Where Claimant Like '*" & Me.txtPropertyClaimant & "*' " _
    & "Or ClaimantID Like '*" & Me.txtPropertyClaimantID & "*' " _
    & "Or ClaimID Like '*" & Me.txtPropertyClaimID & "*' "

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

Similar Threads

  1. Deleting multiple records selected in a Listbox
    By ankitmehtta in forum Access
    Replies: 4
    Last Post: 11-08-2011, 01:20 PM
  2. Replies: 3
    Last Post: 06-22-2011, 08:51 AM
  3. Multiple default values in listbox
    By rickscr in forum Forms
    Replies: 3
    Last Post: 04-07-2011, 09:49 AM
  4. Multiple filter problem.
    By ducecoop in forum Access
    Replies: 2
    Last Post: 10-28-2010, 11:14 AM
  5. Replies: 0
    Last Post: 12-14-2009, 09:57 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