Restrict User from Entering Zero in *Both* of Two Fields?

C

croy

In one table (or form), there are a pair of fields that
accept Integer type data. Zero is a common entry for either
of these fields, but I would like to prevent users from
putting zero in *both* of these fields.

What would be the best approach?
 
K

Ken Snell \(MVP\)

Use the form's BeforeUpdate event to test whether there are zero values in
both textboxes, and to cancel the event if yes:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Textbox1.Value = 0 And Me.Textbox2.Value = 0 Then
Cancel = True
Msgbox "You cannot put a zero in both textboxes!"
End If
End Sub
 
D

Dirk Goldgar

In
Ken Snell (MVP) said:
Use the form's BeforeUpdate event to test whether there are zero
values in both textboxes, and to cancel the event if yes:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Textbox1.Value = 0 And Me.Textbox2.Value = 0 Then
Cancel = True
Msgbox "You cannot put a zero in both textboxes!"
End If
End Sub

As an alternative or supplement to the above, you could define a
table-level validation rule, as for example:

Not ([Field1]=0 And [Field2]=0)
 
C

croy

Use the form's BeforeUpdate event to test whether there are zero values in
both textboxes, and to cancel the event if yes:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Textbox1.Value = 0 And Me.Textbox2.Value = 0 Then
Cancel = True
Msgbox "You cannot put a zero in both textboxes!"
End If
End Sub


Thanks Ken. That works very well.
 
C

croy

In
Ken Snell (MVP) said:
Use the form's BeforeUpdate event to test whether there are zero
values in both textboxes, and to cancel the event if yes:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Textbox1.Value = 0 And Me.Textbox2.Value = 0 Then
Cancel = True
Msgbox "You cannot put a zero in both textboxes!"
End If
End Sub

As an alternative or supplement to the above, you could define a
table-level validation rule, as for example:

Not ([Field1]=0 And [Field2]=0)


Hmmm. Thanks Dirk. Table-level *does* seem more
fail-safe...
 
Top