Results 1 to 14 of 14
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479

    Changing multiple command buttons

    If changing the same attributes like enabled or caption on many buttons, must it be done individually or is there some way that one command will do all?


    For example setting some value in the tag, then using only those with that value. Or is it not worth it ?

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Yes you can use the tag property and loop through all the controls and test for the tag property.
    I'll often use instr() so I can use more than one entry in the tag.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    If it's one form and the same value, you can multi select all required controls and edit the property value for all selected at the same time. Otherwise as stated, you can code to loop over them and weed out the control types you don't want by specifying, for example, to only edit the property for textboxes.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    Right thanks. The changes occur at run time so. Is there any advantage to looping Vs a pile of similar lines of code setting say Caption text?

  5. #5
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Do you need to check conditions on them? Maybe if you name them all similar like:
    button1
    button2
    button3

    For i = 1 to 3
    if button(i) =enabled
    button(i) = disabled
    button(i).color= "blue"
    etc.
    end If
    next i

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I thought this was about design changes, but it's not. If you want to temporarily change a property of something, then you need a way to distinguish it/those from everything else. Often, the Tag property is used for that. Hopefully you realize that run time changes don't stick when the form is closed. Maybe you should post your question in a less obscure way; as in explain what control type this is about, the when and the what changes... stuff like that.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    Ok here's an example of the code. They all all command buttons.
    Code:
    Select Case Me.Grid
            Case "1", "2", "3"
                    .mp3ASide.BackColor = RGB(192, 192, 192)
                    .mp3BSide.BackColor = RGB(192, 192, 192)
                    .flacASide.BackColor = RGB(192, 192, 192)
                    .flacBSide.BackColor = RGB(192, 192, 192)
                    .mp3Mono.BackColor = RGB(192, 192, 192)
                    .mp3Stereo.BackColor = RGB(192, 192, 192)
                     .flacMono.BackColor = RGB(192, 192, 192)
                    .flacStereo.BackColor = RGB(192, 192, 192)
                    .mp3Mono.Enabled = False
                    .mp3Stereo.Enabled = False
                     .flacMono.Enabled = False
                    .flacStereo.Enabled = False
                    .mp3ASide.Enabled = False
                    .mp3BSide.Enabled = False
                    .flacASide.Enabled = False
                    .flacBSide.Enabled = False
                    .mp3Album.Enabled = True
                    .flacAlbum.Enabled = True
                .mp3Album.BackColor = RGB(0, 255, 0)
                .flacAlbum.BackColor = RGB(0, 255, 0)
    
    
            Case Else
                'default to A-Side
                     .flacBSide.Caption = "B-Side"
                    .mp3BSide.Caption = "B-Side"
                    .mp3ASide.Enabled = True
                    .mp3BSide.Enabled = True
                    .flacASide.Enabled = True
                    .flacBSide.Enabled = True
                    .mp3Mono.Enabled = True
                    .mp3Stereo.Enabled = True
                     .flacMono.Enabled = True
                    .flacStereo.Enabled = True
                    .mp3Album.Enabled = False
                    .flacAlbum.Enabled = False
                    .mp3Album.BackColor = RGB(192, 192, 192)
                    .flacAlbum.BackColor = RGB(192, 192, 192)
    I'm just thinking all this repetition is crazy, there must be some way to condense this.
    But perhaps not? Or no real advantage ?
    Does that help? I didn't mean to be vague.

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Well, maybe you select groups (like all the ones related to 192) in design view, then in the property sheet enter anything that tickles your fancy for the Tag property (maybe 192) and save the form. Then in code, do a loop:
    Code:
    Dim ctl As Control
    
    For Each ctl In Me.Controls
       If ctl.Tag = "192" Then ctl.Backcolor = RGB(192, 192, 192)
    Next
    Or even put RGB(192,192,192) as the Tag value **, then it could be
    ...ctl.Backcolor = ctl.Tag

    It's probably always true that there's a way to cut repetition. Of course, you'd have to apply that approach to groups of controls on your form, wherever they have some sort of commonality.

    Note: this ** is a guess since Tag property is a string and that may not play well with RGB function.
    Last edited by Micron; 02-25-2025 at 08:47 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    you can simplify the code to this
    Code:
    'set the basics - colour won't matter if the control is disabled 
    
    
    .mp3ASide.BackColor = RGB(192, 192, 192)
    .mp3BSide.BackColor = .mp3ASide.BackColor
    .flacASide.BackColor = .mp3ASide.BackColor
    .flacBSide.BackColor =.mp3ASide.BackColor
    .mp3Mono.BackColor = .mp3ASide.BackColor
    .mp3Stereo.BackColor =.mp3ASide.BackColor
    .flacMono.BackColor =.mp3ASide.BackColor
    .flacStereo.BackColor = .mp3ASide.BackColor
    
    
    .mp3Album.BackColor =.RGB(0, 255, 0)
    .flacAlbum.BackColor = .mp3Album.BackColor
    
    .mp3Mono.Enabled =instr("123",.grid)>0
    .mp3Stereo.Enabled = .mp3Mono.Enabled
    .flacMono.Enabled = .mp3Mono.Enabled
    .flacStereo.Enabled = .mp3Mono.Enabled
    .mp3ASide.Enabled =.mp3Mono.Enabled
    .mp3BSide.Enabled =.mp3Mono.Enabled
    .flacASide.Enabled =.mp3Mono.Enabled
    .flacBSide.Enabled =.mp3Mono.Enabled
    .mp3Album.Enabled =not .mp3Mono.Enabled
    .flacAlbum.Enabled = .mp3Album.Enabled
    
    
    'now change if required
    if instr("123",.grid)=0 then
         .flacBSide.Caption = "B-Side"
         .mp3BSide.Caption =.flacBSide.Caption
         .mp3Album.BackColor=.mp3ASide.BackColor
         .flacAlbum.BackColor =.mp3ASide.BackColor
    end if

  10. #10
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    Thanks CJ. I'll try that out.
    I thought there may have been some way to set a group of controls to accept the one command.
    Microns suggestion would work, but it you have to loop through every control doesn't that sort of defeat the purpose.

  11. #11
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I thought there may have been some way to set a group of controls to accept the one command.
    Well, I think we were all focused on properties. Are you now saying you want to assign the same method/event to a bunch of controls? Big difference. For that you need to use With Events a class module. Not something I've done.
    But the code seems to be about changing properties, not the associated event.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    I thought there may have been some way to set a group of controls to accept the one command.
    not clear what you mean - you can have a function in a standard module which can be called from whatever control event you want e.g. in a standard module

    Code:
    function myFunction()
    
        msgbox "!"
    
    end function
    and to call it where you would normally see [Event Procedure]" put

    =myFunction()


    so in design view you can select the controls you want to use the function, and type it in once.

    If you need to assign the function dynamically to only certain controls you can perhaps limit the number of controls to loop through by only looping through a forms section

    Code:
    for each ctl in me.section(1).controls
        ctl.onclick="=myFunction()"
    next ctl
    section 1 is the header, 2 is footer and 3 the detail section

    I use the technique to add a sort functionality to each header label for example

    Or as others have suggested, use the tag property to indicate which controls need the functionality - but you still need to loop through the controls

    In another case I have a form which can show everything in one view and in another view, only display certain controls - and I use the tag property to indicate which controls should be displayed, all the others are hidden.

  13. #13
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Or loop through and set the click event (or whatever) to =myFunc(). Maybe even loop and set to
    =myFunc([ActiveControl].[Tag])
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    I see now how that works. Thanks for replies.

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

Similar Threads

  1. Replies: 4
    Last Post: 01-20-2017, 08:16 AM
  2. Replies: 8
    Last Post: 09-18-2015, 01:52 PM
  3. Replies: 1
    Last Post: 08-15-2013, 08:12 PM
  4. Replies: 4
    Last Post: 06-13-2013, 01:02 PM
  5. Replies: 10
    Last Post: 09-18-2012, 02:00 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