Wordy code for enable-disable controls

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
 
B

Bruce M. Thompson

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
'***
 
D

Dirk Goldgar

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
 
B

Bruce M. Thompson

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.

:)
 
M

Mike Sherrill

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.
 

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