P
Peter Hallett
In the heady far-off days of Kernighan & Ritchie’s ‘C’, the Switch statement
(= VBA Select Case) defaulted to ‘fall-through’, unless a Break command was
used to terminate each ‘case’ clause. This allowed considerable flexibility
– a feature of the language. VBA is not so accommodating.
One particularly useful property of ‘fall-though’ was that it allowed
conditional processing of a list. Imagine a situation where a set of form
controls has to be cleared and disabled in a manner determined by the outcome
of an event. Let us assume that the latter returns values 1, 2, 3, 4 …
Ignoring the data clearance commands, for the sake of simplicity, the
‘flushing’ module might be contained in a sub-routine of the form:-
Public Sub Flush (InForm As Form, InStart As Byte)
‘Some initial code, followed by:–
With InForm
.Control_A.Enabled = False
.Control_B.Enabled = False
.Control_C.Enabled = False
…
Label_2:
.Control_D.Enabled = False
.Control_E.Enabled = False
.Control_F.Enabled = False
.Control_G.Enabled = False
.Control_H.Enabled = False
…
Label_3:
.Control_I.Enabled = False
.Control_J.Enabled = False
.Control_K.Enabled = False
.Control_L.Enabled = False
…
Etc.
Etc.
End With
End Sub
This permits disabling to be started from a selected point determined by the
value of InStart. For 1, the default, all the specified controls will be
disabled. If InStart = 2 then clearance will start at Label_2, and so on.
(Such a construction can be used to force a user to complete data entry to a
form in a particular order. This is particularly useful where the permitted
input values for later controls depend upon earlier entries. Should the user
change his mind, part way though data entry, and amend an earlier control,
potentially invalidating later entries, a clearance routine, of the above
type, can be used to delete all subsequent inputs, forcing selective re-input
and hence revalidation against the revised earlier values. (The alternative,
of locking each control following data entry, is not attractive. It
virtually prohibits amendment.)
If VBA permitted fall-though, Select Case would provide a simple solution.
The alternative, however, appears to be the use of the dreaded multiple GoTo
statements in the initial code of the above sub-routine, directing execution
to the appropriate label. The fact that GoTo will not accept parameterized
label addresses makes the thing even messier. It works, but that is the best
that can be said for it. Can anyone suggest a more elegant solution?
(= VBA Select Case) defaulted to ‘fall-through’, unless a Break command was
used to terminate each ‘case’ clause. This allowed considerable flexibility
– a feature of the language. VBA is not so accommodating.
One particularly useful property of ‘fall-though’ was that it allowed
conditional processing of a list. Imagine a situation where a set of form
controls has to be cleared and disabled in a manner determined by the outcome
of an event. Let us assume that the latter returns values 1, 2, 3, 4 …
Ignoring the data clearance commands, for the sake of simplicity, the
‘flushing’ module might be contained in a sub-routine of the form:-
Public Sub Flush (InForm As Form, InStart As Byte)
‘Some initial code, followed by:–
With InForm
.Control_A.Enabled = False
.Control_B.Enabled = False
.Control_C.Enabled = False
…
Label_2:
.Control_D.Enabled = False
.Control_E.Enabled = False
.Control_F.Enabled = False
.Control_G.Enabled = False
.Control_H.Enabled = False
…
Label_3:
.Control_I.Enabled = False
.Control_J.Enabled = False
.Control_K.Enabled = False
.Control_L.Enabled = False
…
Etc.
Etc.
End With
End Sub
This permits disabling to be started from a selected point determined by the
value of InStart. For 1, the default, all the specified controls will be
disabled. If InStart = 2 then clearance will start at Label_2, and so on.
(Such a construction can be used to force a user to complete data entry to a
form in a particular order. This is particularly useful where the permitted
input values for later controls depend upon earlier entries. Should the user
change his mind, part way though data entry, and amend an earlier control,
potentially invalidating later entries, a clearance routine, of the above
type, can be used to delete all subsequent inputs, forcing selective re-input
and hence revalidation against the revised earlier values. (The alternative,
of locking each control following data entry, is not attractive. It
virtually prohibits amendment.)
If VBA permitted fall-though, Select Case would provide a simple solution.
The alternative, however, appears to be the use of the dreaded multiple GoTo
statements in the initial code of the above sub-routine, directing execution
to the appropriate label. The fact that GoTo will not accept parameterized
label addresses makes the thing even messier. It works, but that is the best
that can be said for it. Can anyone suggest a more elegant solution?