Keep Cursor in Subform

J

John Lane

How do I keep the cursor (or focus) in a subform until a certain action has
taken place (ie, filled in a certain field)? I have a Close buttom on the
main form and when they click it, the subform events fire (Ie., Exit and Lost
Focus), but so does the Close button event.
 
T

tina

try adding code to the subform's BeforeUpdate event procedure, as

If IsNull(Me!SomeField) Then Cancel = True: Msgbox "Enter the data."

note that this will only work if the subform record has been dirtied
(added/edited).

hth
 
J

John Lane

That only works as long as the cursor stays in the subform. If the mouse
clicks outside it, like say a button with a Close command in it, then it
fires and the setfocus doesn't work.
 
T

tina

well, your original post asked how you can *keep* the cursor in the
subform...and the solution i posted will effectively do that, subject to the
caveat i included in the post. i tested the code in an A2000 db running in
A2003. i set the code in the BeforeUpdate event of a subform, and added a
command button in the mainform to run a Close action. i dirtied the subform
record, but left the required field blank, then clicked on the Close button
in the mainform. the Close code never ran, because the subform's
BeforeUpdate event ran first, and was cancelled, leaving the focus in the
subform record. clicking the "X" button in the top right corner of the
mainform (instead of the Close button on the form) prompted a warning
message that the subform record could not be saved if the form closed, with
Yes/No buttons to either cancel the Close, or close the form and lose the
edits to the subform record.

did you test the setup as posted? (i didn't understand the SetFocus
reference in your reply to my first post, since i didn't suggest using
SetFocus.) and what results did you observe?

hth
 
G

Gordon Lincoln

Hi John,

Well - if they click on a close button on the main form, then the focus
isn't on the sub-form any more, is it?

I'd probably intercept the close button click event, check to see if the
sub-form was dirty, if dirty then do a validation inspection of the required
sub-form fields, send a msgbox if something needed to be completed before
closing - then set focus to the offending control. If the above checks all
passed, then I'd close the form using a docmd.close with the appropriate
arguments.

Hope this helps,
Gordon
 

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