K
kheisler6
I'm looking for a more efficient way to write some code designed to
enforce some data integrity rules.
For example, the user selects his favorite color from a combo box. The
options are: Red, Green, Blue, and Other. If he selects Other, a
nearby text box is enabled so he can write in his favorite color. If
he writes in his favorite color (e.g., "Orange") but then changes his
mind and tries to select one of the other colors from the combo list,
a message tells him, "Changing your answer from 'Other' will delete
the information in the related text field. Continue?" If he says Yes,
the information is deleted and text field is no longer enabled.
The code I use for this scenario works, but it seems lengthy for such
a simple routine. Is there a more efficient way to code this routine?
I have lots of other controls that need this same kind of enforcement.
Thanks.
Code I'm using:
###
Private Sub cboFavoriteColor_AfterUpdate()
If Me.cboFavoriteColor.Value = "Other" Then
Me.txtOther.Enabled = True
Else
If IsNull(Me.txtOther.Value) Then
Me.txtOther.Enabled = False
Else
iresponse = MsgBox("Changing your answer from 'Other' will
delete the information in the related text field." & _
Chr(13) & Chr(13) & "Continue?", 4 + 48 + 256, "Delete
confirmation")
If iresponse = 7 Then
Me.cboFavoriteColor.Value = "Other"
Exit Sub
Else
Me. txtOther.Value = Null
Me. txtOther.Enabled = False
End If
End If
End If
End Sub
###
enforce some data integrity rules.
For example, the user selects his favorite color from a combo box. The
options are: Red, Green, Blue, and Other. If he selects Other, a
nearby text box is enabled so he can write in his favorite color. If
he writes in his favorite color (e.g., "Orange") but then changes his
mind and tries to select one of the other colors from the combo list,
a message tells him, "Changing your answer from 'Other' will delete
the information in the related text field. Continue?" If he says Yes,
the information is deleted and text field is no longer enabled.
The code I use for this scenario works, but it seems lengthy for such
a simple routine. Is there a more efficient way to code this routine?
I have lots of other controls that need this same kind of enforcement.
Thanks.
Code I'm using:
###
Private Sub cboFavoriteColor_AfterUpdate()
If Me.cboFavoriteColor.Value = "Other" Then
Me.txtOther.Enabled = True
Else
If IsNull(Me.txtOther.Value) Then
Me.txtOther.Enabled = False
Else
iresponse = MsgBox("Changing your answer from 'Other' will
delete the information in the related text field." & _
Chr(13) & Chr(13) & "Continue?", 4 + 48 + 256, "Delete
confirmation")
If iresponse = 7 Then
Me.cboFavoriteColor.Value = "Other"
Exit Sub
Else
Me. txtOther.Value = Null
Me. txtOther.Enabled = False
End If
End If
End If
End Sub
###