Enable fields VBA

jsic1210

Registered User.
Local time
Today, 16:24
Joined
Feb 29, 2012
Messages
188
When I update a combo box to "Active," I want multiple fields to be enabled. I know the way to do it field by field. Example:

If Status = "Active" Then
Trial.Enabled = True
Else
Trial.Enabled=False
End If

Since there are several fields, is there a way to group those fields and name the GROUP of fields, so I don't have to do each one individually? I'm thinking something like this:

If Status = "Active" Then
Group1.Enabled = True
Else
Group1.Enabled=False
End If
 
One way is a loop of controls, using the Tag property to identify the ones you want set:

Code:
  Dim ctl As Control

  For Each ctl In Me.Controls    
    If ctl.Tag = "SetMe" Then
      ctl.Enabled = (Me.Status = "Active")
    End If
  Next ctl
 
you can manage controls with more concise code


sub enablecontrols(setting as boolean)
somecontrol1.enabled = setting
somecontrol2.enabled = setting
somecontrol3.enabled = setting
end sub



then just simply use one of these, as appropriate

enablecontrols(true) or
enablecontrols(false)

I think this is probably easier than messing around with tag properties, personally. It's clear what is happening, and you do not have to remember to set/clear the tags.
 
This is awesome. I've been looking for a way to do this, but with the visible property instead.
 
@pbaldy: The code works for After Update, but not for On Load. I tried it for when the main form loads, and when the subform loads. (The subform is where Status is located). Here is my code:

Code:
Private Sub Form_Load()
'Enable Contract Changes if Active
Dim ctl As Control

For Each ctl In Parent.Controls
    If ctl.Tag = "SetMe" Then
        ctl.Enabled = (Me.Status = "Active")
    End If
Next ctl

If Status = "Active" And Parent.BD = True Then
    Parent.YBMail.Enabled = True
Else
    Parent.YBMail.Enabled = False
End If
End Sub
 
I would try the current event (probably the subform's).
 
Try both main form and subform?
 
Yes, I tried both the main form and subform. Here is what I have for the main form:
Code:
Private Sub Form_Current()
'Enable Contract Changes if Active
Dim ctl As Control

For Each ctl In Me.Controls
    If ctl.Tag = "SetMe" Then
        ctl.Enabled = (Me!frmAmendmentSub.Form.Status = "Active")
    End If
Next ctl
End Sub
And here is what I have for the subform:
Code:
Private Sub Form_Current()
'Enable Contract Changes if Active
Dim ctl As Control

For Each ctl In Parent.Controls
    If ctl.Tag = "SetMe" Then
        ctl.Enabled = (Me.Status = "Active")
    End If
Next ctl
End Sub

End Sub
 
What happens? Error, doesn't do the task, etc. Can you post the db here?
 
Now it seems to be working. I was getting an error before, but this time it's fine. Thanks!
 

Users who are viewing this thread

Back
Top Bottom