Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566

    Use a Toggle Button to Filter a Field

    Hi Everyone

    Is it possible to use a Toggle button to set a filter on a Field in a Split Form?


    The Toggle button has the following string as it's Caption "BNE"

    Then use a Command button's Click Event to filter for the specific String "BNE"

    If this works then would it be possible to use Multiple Toggle Buttons?

    Any help appreciated.
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Yes.
    You have already been shown how to filter.
    Use the buttons caption for criteria.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Code:
    Private Sub toggleButton_Click()
        If Me.toggleButton Then
            'me.toggleButton is True
        
            Me.Filter = "[some_field] = 'BNE'"
            Me.FilterOn = True
        Else
            'me.toggleButton is False
            Me.FilterOn = False
        End If
    End Sub

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

    I have 2 Toggle buttons togBNE and togADL

    I have never used Toggle Buttons before but I have the following on the Click Event of a Command Button:-


    Code:
    Private Sub cmdFilter_Click()
    
    
    10        On Error GoTo cmdFilter_Click_Error
    20    If Me.togBNE = -1 Then
    30    Me.Filter = "[Lane]='" & Me.togBNE.Caption
    40            Me.FilterOn = True
    50        Else
    60            Me.FilterOn = False
    70    End If
    
    
    80    If Me.togADL = -1 Then
    90    Me.Filter = "[Lane]='" & Me.togADL.Caption
    100           Me.FilterOn = True
    110       Else
    120          Me.FilterOn = False
    130   End If
              
    140       On Error GoTo 0
    150       Exit Sub
    
    
    cmdFilter_Click_Error:
    
    
    160       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdFilter_Click, line " & Erl & "."
    
    
    End Sub
    When I try to run this Code I get the following error:-
    Attached Thumbnails Attached Thumbnails error.JPG  
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  5. #5
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    You forgot the closing quotation marks
    Code:
    Me.Filter = "[Lane]='" & Me.togBNE.Caption & "'"
    What would you expect to happen if both toggles are true at the same time? As it is written togADL will always override whatever togBNE is doing.

    You may want to build your filter string before applying it an a manner similar to this: http://allenbrowne.com/ser-62.html

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

    I am not understanding why the following is happening.

    I was under the impression that if I Click a Toggle Button this sets the property to -1

    However, I set a breakpoint on line 20 :-

    Code:
    20    If Me.togBNE = -1 Then
    When I click the Filter Command Button it takes me to Line 20 and when I hover on Me.togBNE it shows = 0 ?

    Can anyone help with this?
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    @mike60smart
    What exactly is a toggle button to you.? I only have 2007 and was envisaging a normal button, click it once and you set the filter, click it again and the filter is cleared.?

    You filter syntax is incorrect, that is why you get the error message. Look back at the thread where theDBguy showed you the syntax.

    As mentioned, what is meant to happen if you click button A, then button B?

    Edit: Are we talking Option Button, or Option Button group ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Ok, found toggle buttons, never have used them.

    However a quick test (you should try this sometimes) does set it to -1, but the next click to 0, so how are you tracking which click is which. There is also a triple state for it as well?

    Try this code (amending to suit of course) to see what yours is doing.

    Code:
    Private Sub Toggle0_Click()
    If Me.Toggle0 Then
        Debug.Print "Toggle 0 " & Me.Toggle0 & " " & Me.Toggle0.Caption
    Else
        Debug.Print "Toggle 0 NE " & Me.Toggle0 & " " & Me.Toggle0.Caption
    
    End If
    End Sub
    
    Private Sub Toggle1_Click()
    If Me.Toggle1 Then
        Debug.Print "Toggle 1 " & Me.Toggle1 & " " & Me.Toggle1.Caption
        Else
        Debug.Print "Toggle 1 NE " & Me.Toggle1 & " " & Me.Toggle1.Caption
    
    End If
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by mike60smart View Post
    Hi kd2017

    I am not understanding why the following is happening.

    I was under the impression that if I Click a Toggle Button this sets the property to -1

    However, I set a breakpoint on line 20 :-

    Code:
    20    If Me.togBNE = -1 Then
    When I click the Filter Command Button it takes me to Line 20 and when I hover on Me.togBNE it shows = 0 ?

    Can anyone help with this?
    What happens when you flip the toggle button and try again?
    Just for reference in ms access: True = -1 and False = 0
    Also fyi for future reference, in my testing just now I noticed that when the form loads and before the toggle control has been clicked it's value is null instead of False (0).

  10. #10
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Well the Immediate Window displays this when Toggle0 is clicked:-

    Toggle 0 -1 BNE
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Yes, then click it again.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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






    OK the current Split Form displays all records available like this:-


    When I click Toggle0 (BNE) it displays 8 records as expected.

    When I click Toggle1 (ADL) it displays 2 records as expected.

    The Code now is as follows:-

    Code:
    Private Sub Toggle0_Click()
    
    
    10        On Error GoTo Toggle0_Click_Error
    20    If Me.Toggle0 Then
          Dim strFilter As String
    30    strFilter = "Left([Lane],3) Like '*" & Me.Toggle0.Caption & "'"
    40    Me.Filter = strFilter
    50    Me.FilterOn = True
    60    End If
              
    70        On Error GoTo 0
    80        Exit Sub
    
    
    Toggle0_Click_Error:
    
    
    90        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Toggle0_Click, line " & Erl & "."
    
    
    End Sub
    What would I need to do so that I am able to click Toggle0 to display all of the BNE Records and then Click Toggle1 to ALSO display the ADL Records?
    Attached Thumbnails Attached Thumbnails Lanes.JPG  
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    You would build the filter accordingly.

    You have to decide how it is meant to work?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #14
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Code for your filter button's click event (not the toggle button's click event)
    Code:
    Private Sub cmdFilter_Click()
        Dim fltr As String
        
        fltr = ""
    
        If IsNull(Me.Toggle0) Then Me.Toggle0 = False
        If IsNull(Me.Toggle1) Then Me.Toggle1 = False
        
        If Me.Toggle0 Then
            fltr = "[Lane] Like 'BNE*'"
        End If
        
        If Me.Toggle1 Then
            If fltr <> "" Then fltr = fltr & " OR "
            fltr = fltr & "[Lane] Like 'ADL*'"
        End If
        
        Debug.Print fltr
        
        If fltr <> "" Then
            Me.Filter = fltr
            Me.FilterOn = True
        Else
            Me.FilterOn = False
        End If
    End Sub
    You could just as easily put this code in a separate function and then just have the toggle buttons call said function on their click events.

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

    This is where I am lost.

    I want to be able to Filter for BNE values and then Also show ADL Values at the same time

    Would the filter be on these lines:-

    Me.Filter = ("Left([Lane],3) Like '*" & Me.Toggle0.Caption & "'") And ("Left([Lane],3) Like '*" & Me.Toggle1.Caption & "'")
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Toggle Button to Add Text to a Field
    By cohnhead in forum Forms
    Replies: 11
    Last Post: 11-19-2014, 06:29 PM
  2. Replies: 3
    Last Post: 06-27-2012, 10:57 AM
  3. remove toggle filter button!?
    By Wombat in forum Access
    Replies: 1
    Last Post: 05-16-2012, 01:50 PM
  4. Toggle Filter on and off
    By Paul H in forum Forms
    Replies: 1
    Last Post: 09-14-2011, 05:54 PM
  5. Replies: 1
    Last Post: 09-13-2010, 01: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