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

  • Thread starter Thread starter croy
  • Start date Start date
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?
 
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
 
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)
 
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.
 
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...
 

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

Back
Top