Results 1 to 3 of 3
  1. #1
    Batraul is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2014
    Posts
    7

    Show in a listbox a query filtered by 2 or more textboxes

    Hi all!

    I'm a newbie in access a wanted to do some start with programming.
    I've created a form with two textboxes and a listbox. What I want to do is to show in the listbox the registers filtered by the 2 textboxes. But I would like that the listbox refreshes while typping in the textboxes.

    It was no problem to do filtering with only one textbox. The rowsource of the listbox is a query in which I set in the criteria Like "*" & [Forms]![Form1]![Textbox1].[Text] & "*"
    Then I write the code for the change event of the Textbox1:


    Private Sub Textbox1_Change()
    Listbox1.Requery
    End Sub


    With this I have no problem. The problem is when using the 2 textboxes. I write the same in the criteria, Like "*" & [Forms]![Form1]![Textbox2].[Text] & "*", on another field but it doesn't work.
    I also write the code for the second textbox:
    Private Sub Textbox2_Change()
    Listbox1.Requery
    End Sub

    The listbox doesn't filter with both textboxes. When you type in one of them, the listbox filters from it, but when you type in the other textbox, the listbox shows the whole table filtered with the characters of the second textbox.

    What am I doing wrong?

    Thank you in advance.

    Regards.
    Last edited by Batraul; 12-14-2014 at 04:10 AM.

  2. #2
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    When you are applying filter, you are using the criteria in text box you are typing in but ignoring the other text box. So, to filter the listbox based on criteria in both the textboxes, the filter should be build using both textboxes. Write a sub routine that will create filter using both the text boxes and call it in the text box change events.
    Code:
    Sub ApplyFilter()
    'Apply filter using both the text boxes
    Me.ListBox1.Requery
    End Sub
    Private Sub Textbox1_Change()
    Call ApplyFilter
    End Sub
    Private Sub Textbox2_Change()
    Call ApplyFilter
    End Sub

  3. #3
    Batraul is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2014
    Posts
    7
    Quote Originally Posted by amrut View Post
    When you are applying filter, you are using the criteria in text box you are typing in but ignoring the other text box. So, to filter the listbox based on criteria in both the textboxes, the filter should be build using both textboxes. Write a sub routine that will create filter using both the text boxes and call it in the text box change events.
    Code:
    Sub ApplyFilter()
    'Apply filter using both the text boxes
    Me.ListBox1.Requery
    End Sub
    Private Sub Textbox1_Change()
    Call ApplyFilter
    End Sub
    Private Sub Textbox2_Change()
    Call ApplyFilter
    End Sub
    Hi amrut,

    Thank you very much for your answer.
    At first I though it worked well, but then I realized that it only respect the characters you're typing in the current textbox but it doesn't respect the characters already typed of the other textbox.

    Regards.

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

Similar Threads

  1. Replies: 8
    Last Post: 10-09-2013, 02:09 AM
  2. Replies: 4
    Last Post: 11-26-2012, 07:17 AM
  3. add data from listbox to textboxes
    By sireesha in forum Access
    Replies: 3
    Last Post: 10-16-2012, 11:45 AM
  4. Set Listbox Filter w/ Multiple Textboxes
    By Ganymede in forum Forms
    Replies: 1
    Last Post: 01-13-2012, 04:02 AM
  5. Sub form filtered by listbox
    By BorisGomel in forum Forms
    Replies: 3
    Last Post: 05-04-2011, 09:20 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