Wordy code for enable-disable controls

  • Thread starter Thread starter Joel Wiseheart
  • Start date Start date
J

Joel Wiseheart

I use the following code, that initializes all of the
data entry controls on a form as disabled. Then, a person
searches for a "MRR Number". Not all MRR's require
rework, so sometimes the resulting search returns no
records. I only want to enable the controls if the form's
recordset contains records after the requery.
The code below is what I have. I was wondering if I'm
missing a way to do this in less than twenty-something
lines. I thought about using a For Each - Next loop, but
not all of the controls need to be disabled, only the 8
for data entry (There's some other controls that provide
reference data). Is there a shorter way to do this?

With Me
!ReworkedBy.Enabled = False
!ReworkDate.Enabled = False
!btnReworkDate.Enabled = False
!ReworkLabor.Enabled = False
!InspectedBy.Enabled = False
!InspectionDate.Enabled = False
!btnInspectionDate = False
!InspectionLabor.Enabled = False
End With

If rst.RecordCount > 0 Then
With Me
!ReworkedBy.Enabled = True
!ReworkDate.Enabled = True
!btnReworkDate.Enabled = True
!ReworkLabor.Enabled = True
!InspectedBy.Enabled = True
!InspectionDate.Enabled = True
!btnInspectionDate = True
!InspectionLabor.Enabled = True
!ReworkedBy.SetFocus
End With
Else
If Me.Parent!txtMRR = "" Then
Exit Sub
Else
MsgBox "This MRR has no dispositions.",
vbExclamation, "No MRR Dispositions"
Me.Parent!txtMRR.SetFocus
End If
End If


Thanks!
Joel
 
I use the following code, that initializes all of the
data entry controls on a form as disabled. Then, a person
searches for a "MRR Number". Not all MRR's require
rework, so sometimes the resulting search returns no
records. I only want to enable the controls if the form's
recordset contains records after the requery.
The code below is what I have. I was wondering if I'm
missing a way to do this in less than twenty-something
lines. I thought about using a For Each - Next loop, but
not all of the controls need to be disabled, only the 8
for data entry (There's some other controls that provide
reference data). Is there a shorter way to do this?

There are a couple of ways to do this, including a "For ... Next" loop method,
but here is a variation on your existing code:

'***
Dim boolRecords As Boolean
boolRecords = rst.RecordCount > 0

With Me
!ReworkedBy.Enabled = boolRecords
!ReworkDate.Enabled = boolRecords
!btnReworkDate.Enabled = boolRecords
!ReworkLabor.Enabled = boolRecords
!InspectedBy.Enabled = boolRecords
!InspectionDate.Enabled = boolRecords
!btnInspectionDate = boolRecords
!InspectionLabor.Enabled = boolRecords
End With

If Not boolRecords Then
If Me.Parent!txtMRR = "" Then
Exit Sub
Else
MsgBox "This MRR has no dispositions.", _
vbExclamation, "No MRR Dispositions"
Me.Parent!txtMRR.SetFocus
End If
End If
'***
 
Bruce M. Thompson said:
There are a couple of ways to do this, including a "For ... Next"
loop method, but here is a variation on your existing code:

'***
Dim boolRecords As Boolean
boolRecords = rst.RecordCount > 0

With Me
!ReworkedBy.Enabled = boolRecords
!ReworkDate.Enabled = boolRecords
!btnReworkDate.Enabled = boolRecords
!ReworkLabor.Enabled = boolRecords
!InspectedBy.Enabled = boolRecords
!InspectionDate.Enabled = boolRecords
!btnInspectionDate = boolRecords
!InspectionLabor.Enabled = boolRecords
End With

If Not boolRecords Then
If Me.Parent!txtMRR = "" Then
Exit Sub
Else
MsgBox "This MRR has no dispositions.", _
vbExclamation, "No MRR Dispositions"
Me.Parent!txtMRR.SetFocus
End If
End If
'***

And if you set the Tag property of each of these controls (in design
view) to some special value, then you can loop through the controls:

Dim ctl As Control
Dim boolRecords As Boolean

boolRecords = rst.RecordCount > 0

For Each ctl In Me.Controls
If ctl.Tag = "Joel" Then
ctl.Enabled = boolRecords
End If
Next ctl
 
And if you set the Tag property of each of these controls (in design
view) to some special value, then you can loop through the controls:

Dim ctl As Control
Dim boolRecords As Boolean

boolRecords = rst.RecordCount > 0

For Each ctl In Me.Controls
If ctl.Tag = "Joel" Then
ctl.Enabled = boolRecords
End If
Next ctl

That's *exactly* the "For ... Next" method I was referring to (although I
probably wouldn't have thought of that tag value). <chuckle>

Thanks, Dirk.

:-)
 
On Tue, 25 Nov 2003 08:50:31 -0800, "Joel Wiseheart"

[snip]
The code below is what I have. I was wondering if I'm
missing a way to do this in less than twenty-something
lines.

You can add the controls you're interested in to a Collection, then
loop through the contents of the Collection.
 
Back
Top