/ Published in: Visual Basic
                    
                                        
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
Private Sub cboColor_BeforeUpdate(Cancel As Integer)
' Validation for combo box with a corresponding text field.
' When users select "Other" in cboColor, they are encouraged to specify in txtOther what they mean.
' If they enter something in txtOther and then decide to change or delete their answer in cboColor, this routine
' tells them the data in txtOther will be deleted. This prevents orphaned data being stored in txtOther
' when cboColor is blank or <> "Other".
If Not IsNull(Me.txtOther) Then ' If txtOther has data
If Not Me.cboColor.Value = "Other" Or IsNull(Me.cboColor) Then ' and the user selects something different than "Other", ask the user to confirm the change
If MsgBox("Changing this answer from 'Other' will delete the information in the adjacent text field. " & _
Chr(13) & Chr(13) & "Continue?", vbQuestion + vbYesNo + vbDefaultButton2, "Change Answer?") = vbYes Then
' If the user clicks 'Yes'
Me.txtOther = Null ' delete the data in txtOther
Else ' Otherwise, if the user doesn't click 'Yes'
Cancel = True ' don't go through with the change (i.e., cancel the update),
Me.cboColor.Undo ' restore the value of cboOther to the original answer (i.e., 'Other')
Exit Sub ' and leave this routine
End If
End If
End If
'Check the final value in cboColor to determine whether to enable or disable txtOther.
'If cboColor = 'Other', set txtOther's enabled property to True (i.e., -1).
'If cboColor <> 'Other', set txtOther's enabled property to False (i.e., 0).
Me.txtOther.Enabled = IIf(Me.cboColor = "Other", -1, 0)
End Sub
Comments
 Subscribe to comments
                    Subscribe to comments
                
                