Return focus to form

B

Brian

I made all my forms modal to prevent a problem when shutting down the app,
but I know there is a way to get past it. Scenario (much simplified):

Form1: main menu, with button that runs DoCmd.Quit & a button that opens
Form2 (among many other buttons that open other forms)
Form2: bound input form

I disabled the control box on Form2 to prevent closure of the form when a
record is incomplete, and then run code to ensure various controls are
populated before the user can navigate to another record or close the form.
When a null control is found, the navigation/closure action is cancelled, the
user sees a MsgBox indicating which control is incomplete, and the focus is
passed to the control.

To prevent users from closing the app via Form1 while having an incomplete
record on Form2, I made Form2 modal. However, I would really like to make it
non-modal to accomodate some other user needs.

How do I properly do the following when the user attempts to close the app
from Form1 while there is an incomplete record represented on Form2:

1. Cancel the close operation (from the button on Form1)
2. Pass control back to Form2 so that the user does not get an error when
the completeness-checking code attempts to place the focus on the control on
Form2 that must be completed?
 
C

Cheese_whiz

Hi Brain,

This is one way to handle it, I guess, that I don't know how well it will
work with your setup and you may not like the effect it has on your
application generally speaking.

I have the following in my code on the form's before update event:

Private Sub Form_BeforeUpdate(Cancel As Integer)

On Error GoTo Err_BeforeUpdate

If Me.Dirty Then

If MsgBox("Data Has been modified! Do you want to save?", vbYesNo +
vbQuestion, _
"Save Record") = vbNo Then
Me.Undo
End If
End If

Exit_BeforeUpdate:
Exit Sub

Err_BeforeUpdate:
MsgBox Err.Number & " " & Err.Description
Resume Exit_BeforeUpdate
End Sub

What that does is make someone confirm any edits/saves before they leave a
record by, I think, any means. That means if they try to navigate to a
different record, close the form, or close the application a message box will
pop asking if they want to save changes.

You may not want that to happen all the time, but for me, making people
aware that they are changing a record or NOT changing it is worth an extra
click.

Hope that helps,
CW
 
M

mcescher

I handled a situation like that by hiding Form1 while Form2 was open,
and then the "Close" button unhid Form1.

HTH,
Chris
 
B

Brian

Except that I have many more forms than just Form1 & Form2. Form1 is my main
menu, and I want to allow users to get back to the menu so they can get to
Form3, Form4, etc. from there while Form2 is still open, even if Form2 is
still in the middle of an incomplete record. Otherwise, I might as well just
keep my forms modal the way I have it now, which offers perfect protection
but less flexibility for the user.
 
B

Brian

I already do this and much more. In fact, I analyze each required
control/field and generate a custom message indicating which control is
incomplete, cancel the current action and return the focus to the control
that requires input.

This work well as long as the form that has the incomplete data has the
focus when the user attempts to close the form. This pretty much requires
modal forms. However, I now want to make the app a little more flexible and
allow the user to go set up a vendor, for instance, while in the middle of
creating the contract and he discovers he needs a new vendor - without
cancelling the partial record already input (and I certainly do not want to
allow the save of an incomplete record, as it will likely never get completed
and wreak havoc with reporting output ever after!)

If the user minimizes Form2 having an unsaved half a contract on it, goes
over to Form3 to creates his vendor, and then forgets to go back to complete
the contract on Form2 and instead attempts to close the program from the main
menu, he correctly gets the message from the code on Form1 indicating that
the information is incomplete.

However, in this scenario, the code on Form1 then attempts to move the focus
to the control bound to the data that needs to be completed, but since Form2
does not have the focus, it generates an error.

What I need is a way for the code on Form2 to cancel the action called for
(DoCmd.Quit) on Form1 (the main menu from whence the user is attempting to
close the application) and get the focus back to Form2 before setting the
focus to a particular control on Form2.
 

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