Results 1 to 6 of 6
  1. #1
    HansH is offline Novice
    Windows 11 Office 365
    Join Date
    Aug 2023
    Posts
    1

    Any idea to get this VBA code more efficient?

    Dear all,

    I'm trying to get a range of Togglebuttons to be enabled and fire a filter. Is there a way more efficient than this? any idea is appreciated since the coding page gets very long in this way.

    Thank in advance.

    Hans.

    Private Sub Toggle282_AfterUpdate()
    If Me.Tekst302 = 0 Then
    Me.Toggle282 = False
    Else
    If Me.Toggle282 = True Then
    DoCmd.GoToControl "query1"
    DoCmd.SetFilter "", "[profiel type]=""vac""", ""
    Me.Toggle25.Enabled = False


    Me.Toggle23.Enabled = False
    Me.Toggle37.Enabled = False
    Me.Toggle31.Enabled = False
    Me.Toggle44.Enabled = False
    Me.Toggle41.Enabled = False
    Me.Toggle47.Enabled = False
    Else
    DoCmd.GoToControl "query1"
    DoCmd.RunCommand acCmdRemoveFilterSort
    Me.Toggle25.Enabled = True
    Me.Toggle23.Enabled = True
    Me.Toggle37.Enabled = True
    Me.Toggle31.Enabled = True
    Me.Toggle44.Enabled = True
    Me.Toggle41.Enabled = True
    Me.Toggle47.Enabled = True
    End If
    End If
    End Sub

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Use the tag property of the controls. You then iterate through the forms control collection and either set it enabled or dis-abled.

    Something along the lines of:

    Code:
    For each ctl in me.controls
    
    if ctl.tag = "XYZ" then
    
    ctl.enabled = true
    
    else
    
    ctl.enabled = False
    
    end if
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    For a start, please post code within code tags. That keeps the indentation which I hope you are using?

    Try
    Code:
    Private Sub Toggle282_AfterUpdate()
        If Me.Tekst302 = 0 Then
            Me.Toggle282 = False
        Else
            DoCmd.GoToControl "query1"
            Me.Toggle25.Enabled = Not Me.Toggle282
            Me.Toggle23.Enabled = Not Me.Toggle282
            Me.Toggle37.Enabled = Not Me.Toggle282
            Me.Toggle31.Enabled = Not Me.Toggle282
            Me.Toggle44.Enabled = Not Me.Toggle282
            Me.Toggle41.Enabled = Not Me.Toggle282
            Me.Toggle47.Enabled = Not Me.Toggle282
            
            If Me.Toggle282 = True Then
                DoCmd.SetFilter "", "[profiel type]=""vac""", ""
            Else
                DoCmd.RunCommand acCmdRemoveFilterSort
            End If
        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

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    A generic sub to handle the toggles

    Code:
    Public Sub SetControls(frm As Form, TorF As Boolean, MyTag As String)
    
        Dim ctl As Control
    
    For Each ctl In frm.Controls
        
            If ctl.Tag = MyTag Then
    
                ctl.Enabled = TorF
    
            End If
    
        Next
    
    End Sub
    You would call it like
    Code:
    SetControls Me, True, "XYZ"
    or

    Code:
    SetControls Me, False, "XYZ"
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    I use very similar code which you can find in my example app Set Controls (isladogs.co.uk)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Loop through all controls on form or limit loop to the toggles. Assign names with sequential number suffix: Toggle1, Toggle2, etc.
    Code:
    For x = 1 to 7
        Me("Toggle" & x).Enabled = Not Me.Toggle282
    End If
    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. Sharepoint messing up the code. Any idea why?
    By hari101213 in forum Programming
    Replies: 4
    Last Post: 04-19-2020, 12:24 PM
  2. Replies: 4
    Last Post: 06-02-2016, 08:13 AM
  3. Need help making my code more efficient
    By themidnitereign in forum Programming
    Replies: 3
    Last Post: 08-10-2012, 11:06 AM
  4. Replies: 1
    Last Post: 08-03-2012, 12:32 PM
  5. More efficient way of repeating code
    By karmacable in forum Programming
    Replies: 2
    Last Post: 09-12-2011, 12:43 AM

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