Dim oCtrl As Control Dim oButton As CommandButton For Each oCtrl In MyForm.Controls If oCtrl.Tag = "SomeValue" Then Set oButton = oCtrl oButton.Enabled = False End If Next Set oCtrl = Nothing Set oButton = Nothing
If you have other buttons that you do not want to disable, the solution should use the Tag property. Set the Tag property on all buttons that you want to enable or disable along with the same value. Then you can check this value in view mode and enable / disable them. Another way is to call them the same prefix or suffix and check that in your code.
Adding
Btw, the Control object does not have the Enabled property. Therefore, you must throw it in the CommandButton to disable it . Apparently, the Control object has the Enabled property, but it does not appear in intellisense. However, you should still try to enable Control in the CommandButton to make sure you have one. Here is the extended version:
Dim oCtrl As Control Dim oButton As CommandButton For Each oCtrl In MyForm.Controls If oCtrl.Tag = "SomeValue" Then On Error Resume Next Set oButton = oCtrl On Error GoTo 0 If Not oButton Is Nothing Then oButton.Enabled = False End If End If Next Set oCtrl = Nothing Set oButton = Nothing
source share