From: Marshall Barton on
Kurt Heisler wrote:

>What is the best to way to exclude a specific control (or controls)
>from the following condition? (e.g., I would like to exclude SubjectID
>and MRN from being Enabled or Disabled):
>
>Dim Ctl As Control
> On Error Resume Next
>
>If IsNull(Me.SubjectID) OR IsNull(Me.MRN) Then
> For Each Ctl In Me.Controls
> Ctl.Enabled = False
> Next Ctl
>Else
> For Each Ctl In Me.Controls
> Ctl.Enabled = True
> Next Ctl
>End If
>
>I'm using this to ensure that the user enters the SubjectID and MRN
>*before* entering more data for the subject. By disabling all the
>controls when SubjectID and MRN are blank, he has no choice but to
>start there.
>
>(I realize I can evaluate later whether SubjectID & MRN are null, like
>when the user tries to leave the record, but I'd prefer to do it ahead
>of time.) I need the code to work in the form's OnCurrent Event
>(basically, for new records), and also in the OnClick event of a
>button that says "Go!" (which will enable the controls).
>
>If there is a more reliable/efficient way to go about enforcing this
>kind of quality control, I'm all ears.


First, you must not disable the two controls that the user
is supposed to use.

Second, you can not disable a control if it has the focus.

I think it would be more general if you set the Tag property
of the controls you want to enable/disable to something like
DISABLE. Then the code could look like:

Dim Ctl As Control
Dim OffOn As Boolean
On Error Resume Next

If IsNull(Me.SubjectID) Then
Me.SubjectID.SetFocus
ElseIf IsNull(Me.MRN) Then
Me.MRN.SetFocus
Else
OffOn = True
End If
For Each Ctl In Me.Controls
If ctl.Tag = "DISABLE" Then
Ctl.Enabled = OffOn
End If
Next Ctl
End If

--
Marsh
MVP [MS Access]