before update to trap for data entry error

G

Guest

Let’s say there are a pair of date fields called ‘Printed’ and ‘Received’.
The idea is that when the user clicks the ‘Print’ form command, the current
date is entered into the former. When the form is circulated and returned to
the user, the user is expected to enter the date ‘Received’. So I decided to
create some error traps. Here’s my vba code

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not IsNull(Me.Printed) And Not IsNull(Me.Received) Then
If (Printed >= Received) Then
Cancel = True
strMsg = "The dates were not entered correctly. "
End If
End If
If Cancel Then
strMsg = strMsg & "Complete the data or press <Esc> to undo your
entry." & vbCrLf
MsgBox strMsg, vbExclamation, "Invalid Data"
End If
End Sub

What I thought would happen when the user entered dates that were
implausible would be that on hitting “<Esc>â€, the data just entered would be
striken/removed from the control. That’s not what happens. Can someone show
me the error of my ways?

I don’t know if this bears mention, but when the user clicks the ‘Print’
button, the following pair of commands appear just after the one to print it
out:

Me.Printed = Date
Me.Received = ""

Thanks for any help in advance.

-Ted
 
M

Marshall Barton

Ted said:
Let’s say there are a pair of date fields called ‘Printed’ and ‘Received’.
The idea is that when the user clicks the ‘Print’ form command, the current
date is entered into the former. When the form is circulated and returned to
the user, the user is expected to enter the date ‘Received’. So I decided to
create some error traps. Here’s my vba code

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not IsNull(Me.Printed) And Not IsNull(Me.Received) Then
If (Printed >= Received) Then
Cancel = True
strMsg = "The dates were not entered correctly. "
End If
End If
If Cancel Then
strMsg = strMsg & "Complete the data or press <Esc> to undo your
entry." & vbCrLf
MsgBox strMsg, vbExclamation, "Invalid Data"
End If
End Sub

What I thought would happen when the user entered dates that were
implausible would be that on hitting “<Esc>”, the data just entered would be
striken/removed from the control. That’s not what happens. Can someone show
me the error of my ways?

I don’t know if this bears mention, but when the user clicks the ‘Print’
button, the following pair of commands appear just after the one to print it
out:

Me.Printed = Date
Me.Received = ""


Esc only undoes the changes to the control that has the
focus. Since you have your code in a button's click event,
the change to the date control has already been updated.

I think(?) you want to use Esc Esc (twice) to undo all edits
since the record was last saved.

OTOH, If you really only want to undo a single **bound**
control, then you can use:
Me.contolname = Me.contolname.OldValue

Just remember that unbound controls don't have any of these
features. For unbound controls, just set their value to
Null or whatever value you set it to originally.
 
S

Steve Schapel

Ted,

This is not a complete answer to your question. But if your code puts a
"" into Received, then Received will never be Null, so the
IsNull(Me.Received) in your initial If clause will never be true. Don't
know if that helps. Maybe the code on the print button should be like
this instead...
Me.Received = Null

Apart from that, I'm not 100% sure I understand what you want to happen.
Is this to simply test that the Received date is entered at a date
that is later than the Printed date?
 
G

Guest

hi marsh,

in the intervening time, i cut this out of the form's bu event and dropped
it into the control's and added another line to handle the case when the
user's Printed is still empty but there's an attempt at entering a Received
date:

Private Sub Received_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not IsNull(Me.Printed) And Not IsNull(Me.Received) Then
If (Printed >= Received) Then
Cancel = True
strMsg = "The dates were not entered correctly. "
End If
End If
If IsNull(Printed) = True And IsNull(Received) = False Then
Cancel = True
strMsg = "The Printed date needs to be entered first. "
End If
If Cancel Then
strMsg = strMsg & "Complete the data or press <Esc> to undo your
entry." & vbCrLf
MsgBox strMsg, vbExclamation, "Invalid Data"
End If
End Sub

i 'think' it's working now. unless further testing reveals the error of my
ways, i might add yet another 'test' to assure that the 'Printed' date is at
never after the 'Received' date on the 'Printed' control.

whatcha think?

best,

-ted
 
G

Guest

i thought i had tried programmatically setting the date to Null but that A2k
choked on the idea and then had to back off to using the quotations. you're
right, steve, the idea is basically to insure that the two dates hold H20
when entered pairwise.

thank you,

-ted
 
M

Marshall Barton

That looks ok. Since the Cancel = True prevents the control
from losing the focus, a single Esc will undo the received
control. If Received is a bound control, you could do this
for the users (instead of telling them to hit Esc) by
adding
Me.Received.Undo
to the procedure.
 

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

Top