Results 1 to 8 of 8
  1. #1
    acces6897 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Sep 2019
    Posts
    2

    How to do filtering for each field individually, but through one procedure?

    How to do filtering for each field individually, but through one procedure?
    There is:
    - tape form;
    - two fields for filtering.

    I made the code but it does not work.



    Code:
    Private Sub NameStudCritFltr_txt_Change()
    Code:
        Dim nameField
        nameField = "NameStud"
        FltrStart (nameField)
    End Sub
    
    Private Sub Property_2CritFltr_txt_Change()
        Dim nameField
        nameField = "Property_2"
        FltrStart (nameField)
    End Sub
    
    Public Sub FltrStart(nameField)
        Dim cntrl As Control
    
          Set cntrl = Screen.ActiveControl
    
        If cntrl.Text & "" <> "" Then
             ' Использование фильтра
             Me.Filter = nameField & " like '*" & cntrl.Text & "*'" '
             Me.FilterOn = True
         Else
            Me.FilterOn = False
         End If
            Me(cntrl.Name).SetFocus
            Me(cntrl.Name).SelStart = Len(Me(cntrl.Name).Text) + 1
     End Sub


    Click image for larger version. 

Name:	2019-09-03_12-53-43.png 
Views:	26 
Size:	102.5 KB 
ID:	39658Фильтр.zip

  2. #2
    Join Date
    Apr 2017
    Posts
    1,679
    1. Use Update event (is fired when control loses focus) instead of Change (is fired whenever anything n control changes - e.g. a character is entered);
    2. The event code for both textboxes will be identical:
    A string variable must be declared;
    If ... elseif ... else ... endif is used to compose the filter string in defined variable in following order
    a) if both textboxes are empty (no filter);
    b) if name texbox is empty (filtering by property_2;
    c) if property_2 texbox is empty (filtering by name);
    d) else filtering by name and property_2;
    The filter condition for form is applied.

  3. #3
    acces6897 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Sep 2019
    Posts
    2
    @ArviLaanemets
    1. I need the filtering to occur as you type in the filter.
    I could be wrong, but the Update event does not allow this.
    So leave the event "Change"?
    2. In connection with the above, paragraph 2 of your decision remains unchanged?
    3. I am worried about this part of the code.
    Code:
    Me(cntrl.Name).SetFocus
    Me(cntrl.Name).SelStart = Len(Me(cntrl.Name).Text) + 1
    Did I do the right thing?
    4. Or is the solution itself incorrect and do I need to create a separate filter for each field?

  4. #4
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Give a try to this code:
    Code:
    Private Sub txtFromCityName_Change()
        FltrStart "NameStud"
    End Sub
    
    Private Sub txtToCityName_Change()
        FltrStart "Property_2"
    End Sub
    
    Private Sub FltrStart(strField As String)
        Dim strText As String
    
        On Error Resume Next
        With Screen.ActiveControl
            strText = .Text
            Me.Filter = "[" & strField & "] LIKE ""*" & strText & "*"""
            Me.FilterOn = Len(strText) > 0
            .SetFocus
            .SelStart = Len(strText) + 1
        End With
    End Sub
    Edit:
    If you don't want to pass the name of the field as argument, you have to keep it in the Tag property of the search control.
    Then, the code above could be as follows:

    Code:
    Private Sub txtFromCityName_Change()
        FltrStart
    End Sub
    
    Private Sub txtToCityName_Change()
        FltrStart
    End Sub
    
    Private Sub FltrStart()
        Dim strText As String
    
        On Error Resume Next
        With Screen.ActiveControl
            strText = .Text
            Me.Filter = "[" & .Tag & "] LIKE ""*" & strText & "*"""
            Me.FilterOn = Len(strText) > 0
            .SetFocus
            .SelStart = Len(strText) + 1
        End With
    End Sub

  5. #5
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Hi

    Your table structure is wrong.

    1 Student has Many Properties.

    You need a table for Students and another table for StudentsProperties

  6. #6
    Join Date
    Apr 2017
    Posts
    1,679
    Quote Originally Posted by acces6897 View Post
    @ArviLaanemets
    1. I need the filtering to occur as you type in the filter.
    Why do you want this?

    E.g. you want to search for students with name John. You type "J", and Change event fires. Te form is filtered for students with name J. No one is found (or when yo use LIKE all students with "J" in name will returned. Probably some additional events are fired (BeforeUpdate, AfterUpdate, Current etc.). You type next letter, and the same chain is happening with "Jo" as search string. Etc. Btw. such thing is named "cascading events" and is considered to be avoided.

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    To reinforce what mike said, It is usually a tell tale sign of poor table design when you have fields like Property1,Property2,Property3.
    What do these properties represent?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,920
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 33
    Last Post: 09-25-2015, 08:39 AM
  2. Passing a Field Value to VBA Procedure
    By tonygg in forum Access
    Replies: 3
    Last Post: 03-10-2015, 04:32 PM
  3. Replies: 7
    Last Post: 07-15-2014, 11:36 PM
  4. Replies: 8
    Last Post: 08-13-2013, 04:58 PM
  5. Replies: 2
    Last Post: 04-16-2013, 01:34 PM

Tags for this Thread

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