Any way to enforce subfrom edits from main form

Posted on 2017-10-12
Medium Priority
Last Modified: 2017-10-13
In my Access 2013 application I have a mainform 'frmDocumentDefinition' with two subforms 'frmDocumentInterestedParties_Sub' and 'frmDocumentTriggeredCosts_Sub' on it.

The two subforms are continuous forms.  Within the subforms I have an edit routine in the 'before update' event for each record entered or updated on the subforms to make sure that all required fields have been entered on each line.  The edits work and put out messages for any errors encountered.  While working only in the subform the user cannot go to another line until all the edits for the current line are passed.

The problem occurs when users clicks anywhere on the main form while working on an incomplete line in one of the subforms.  When that occurs:

The subform edits are invoked and any subform error messages are displayed " Cost Date is a required field."

However, after the error message from the subform is displayed the focus shifts to the main form.  The bound subform record is saved but it has the errors in it.  For example: required fields are blank.

How do I stop the subform from losing focus when there are edit errors on the subform?  

How can I make sure the edits on the subforms are enfoorced from the the main form?
Question by:mlcktmguy
LVL 10

Accepted Solution

Anders Ebro (Microsoft MVP) earned 2000 total points
ID: 42329507
In the subforms before update event, are you setting Cancel=true, when the required fields are missing? The will prevent the record from being saved, and that should also prevent the focus from moving to the main form.

Also if the field must ALWAYS be filled out, then you should, at the table level set the field to be required. Don't get me wrong, the msgbox in the beforeUpdate event is still a good idea, as it presents a much more user friendly message.
LVL 52

Expert Comment

by:Gustav Brock
ID: 42329544
Also, the subform controls on the main form have an OnExit  event you might be able to use.

LVL 49

Expert Comment

by:Dale Fye
ID: 42329857
Agree with Gustav, the Exit event of the subform has a Cancel argument, which allows you to cancel exiting the form.  you could simply test to determine whether the subform is dirty and cancel if it is still dirty, something like:
Private Sub SubformControlName_Exit(Cancel as Integer)

    Cancel = me.subformControlName.Form.Dirty

end sub

Open in new window


Author Closing Comment

ID: 42330220
Anders asked: "In the subforms before update event, are you setting Cancel=true"

I was not setting the Cancel=True

That works and keeps the focus on the subform.  Thanks.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question