Can't Set Focus after remoing text from field

A

Ann

Help, I haven't a clue why this won't work.

If Me.dtmClosedDate < Me.dtmOpenDate Then
MsgBox "The Closed Date can not be prior to the Open Date."
Me.dtmClosedDate.Value = ""
Me.dtmClosedDate.SetFocus
End If

The message box appears, the old date is removed from the field but the set
focus won't work. I've also tried gotocontrol but my cursor just ends up on
the next field. I tried cancelling the event but the same thing happens.

If I used Text instead of Value I get a run time error 2115 which says "The
macro or function set to the BeforeUpdate or Validation Rule property for
this field is preventing Microsoft Access from saving the data in the field."
but I don't have anything in BeforeUpdate or the Validation Rule property.
Can anyone help me out? Thanks in advance.
 
A

Allen Browne

What event did you use? If it was the control's AfterUpdate event, Access
moves the focus after this event is complete. Therefore any change you make
in this event is immediately overridden, so it does not do what you expect.

Perhaps you could use the BeforeUdpate event of the control. If you cancel
the event, Access won't let the focus leave, so it has the effect you want.
Therefore:
If Me.dtmClosedDate < Me.dtmOpenDate Then
Cancel = True
MsgBox "The Closed Date can not be prior to the Open Date."
End If

That may be annoying: if the problem was with the dtmOpenDate being
incorrect, the user now can't get out of this box to fix the other one. For
comparing fields, it is therefore a much better idea to use the BeforeUpdate
event of the *form* rather than of the control.

Also, if dtmClosedDate is meant to be a date/time value, you must not set it
to a zero-length string. Either set it to Null, or Undo it.
 
A

Ann

Sorry, I forgot to mention I was on the AfterUpdate event.

I changed it to the BeforeUpdate Event as you suggested and it stayed in the
field. But how do I select the text so it can be typed over. The insertion
point was blinking at the end of the field and the user would have to select
the date themselves? Thanks for the help!
 

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