Lostfocus question

  • Thread starter Thread starter mcnewsxp
  • Start date Start date
M

mcnewsxp

how can i override or disable lostfocus on a control when a user clicks
cancel or closes a form?

tia,
mcnewsxp
 
(re-posting, as my original reply hasn't appeared)

mcnewsxp said:
how can i override or disable lostfocus on a control when a user clicks
cancel or closes a form?

Do you mean, how to prevent the LostFocus event from firing? You can't.
Depending on what you're using the LostFocus event for, you may be able to
use some other event for the purpose, or else have some sort of flag that
you can check inside the LostFocus event to see whether you should skip the
rest of the event procedure. However, you won't be able to click elsewhere
to set that flag without triggering the event.
 
I agree with Dirk.LostFocusand OnExit are often used when the control's
BeforeUpdate or AfterUpdate event is more appropriate, or at times even
theForm_BeforeUpdate event.

What exactly are you using theLostFocusfor?

to make sure a text box is filled as a validation rule.
the form also has a cancel button on it, the textbox with the
lostfocus validation fires when the cancel button is clicked.
not good....
 
mcnews said:
to make sure a text box is filled as a validation rule.
the form also has a cancel button on it, the textbox with the
lostfocus validation fires when the cancel button is clicked.
not good....


You can't do it that way. Instead, I suggest you use the form's
BeforeUpdate event to check that this and any other required fields are
filled in. If they aren't, set the event procedure's Cancel argument to
True to abort the update, and display an appropriate message to the user.
In your Cancel button's Click event, undo the form before closing it; e.g.,

Private Sub cmdCancel_Click()

If Me.Dirty Then Me.Undo

DoCmd.Close acForm, Me.Name, acSaveNo

End Sub
 
Back
Top