Cancel update from invalid data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Re Access XP (no SP's)

I have a continuous form which needs to validate two text boxes (i.e. one
number entered is greater than the other)

If the data is invalid when the user clicks away from the current record, I
want to cancel the update and return to the offending text box.

I am using the LostFocus event of one of the text boxes to make the check,
and display an appropriate message:

Private Sub txtThisWk_LostFocus()
If txtLastWk > txtThisWk Then
MsgBox "This week's milage is less than last weeks!" & vbCrLf & _
"Please re-enter.", vbCritical, "Stop!"

Me!txtThisWk.Undo
'txtThisWk.SetFocus
End If
End Sub

The problem is that the form does not return to the offending record and
does not cancel the update i.e. it updates the record even with the wrong
value.

How can I cancel and return focus to the offending record (the very last
record updated?

Is there a single command to achieve this or will I need to ues the
RecorsetClone method?

Many thanks.
 
Pete said:
Re Access XP (no SP's)

I have a continuous form which needs to validate two text boxes (i.e.
one number entered is greater than the other)

If the data is invalid when the user clicks away from the current
record, I want to cancel the update and return to the offending text
box.

I am using the LostFocus event of one of the text boxes to make the
check, and display an appropriate message:

Private Sub txtThisWk_LostFocus()
If txtLastWk > txtThisWk Then
MsgBox "This week's milage is less than last weeks!" & vbCrLf & _
"Please re-enter.", vbCritical, "Stop!"

Me!txtThisWk.Undo
'txtThisWk.SetFocus
End If
End Sub

The problem is that the form does not return to the offending record
and does not cancel the update i.e. it updates the record even with
the wrong value.

How can I cancel and return focus to the offending record (the very
last record updated?

Is there a single command to achieve this or will I need to ues the
RecorsetClone method?

Many thanks.

Validation should be done in the BeforeUpdate event of the control. Then you
can set the Cancel argument for that event to True when the test fails. Done
this way the control never loses focus so there is no requirement to set focus
back.
 
Of course!

A classic case of trying to "close the gate after the hores has bolted"

Works perfectly!

Many thanks Rick.
 
Back
Top