Disable/Enable all controls on a form based on value of anothercontrol

K

Kurt Heisler

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.
 
J

Jeff Boyce

Kurt

One approach might be to set those controls' properties to disabled
initially, then only enable them when the controls you wish have had values
added/selected.

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.
 
B

Beetle

The Tag property might come in handy here. Put something like
X (or whatever you want) in the Tag property of the controls for
SubjectID and MRN. Then modify your code as follows;

Dim Ctl As Control
On Error Resume Next

If IsNull(Me.SubjectID) OR IsNull(Me.MRN) Then
For Each Ctl In Me.Controls
If Ctl.Tag <> "X" Then Ctl.Enabled = False
Next Ctl
Else
For Each Ctl In Me.Controls
Ctl.Enabled = True
Next Ctl
End If
 
M

Marshall Barton

Kurt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top