Prevent Parent form closing if Tab Control with Sub Form required fields are complete

Rx_

Nothing In Moderation
Local time
Today, 04:45
Joined
Oct 22, 2009
Messages
2,803
Prevent Parent form from closing Tab Control Sub Form from closing until required fields are completed - unbound form.
(see attachment)

Until now, a function determines if all required fields are completed and returns a True/False. This sets an Application Global Variable. The Close button checks the Global variable and with an If statement allows / prevents the Close code from executing.

It would appear that if a Parent is issued a Close, none of the child objects can stop the close process.

While setting a Global variable does work, just wondering if there is any better solution? My number of Global flags are getting up there as this one application is growing.




This post is great to prevent users from changing to another tab until the required fields in the first tab is satisfied.
http://www.access-programmers.co.uk/forums/showthread.php?t=222546
 

Attachments

  • Parent Tab Control Subform Close.png
    Parent Tab Control Subform Close.png
    21.5 KB · Views: 205
Greetings Rx_

My Add/Edit record forms have a Validation class they create an instance of. So one class shared between the Add form and the Edit form.

When it comes to Validating the form fields, the Form makes a call to either the Insert / Update method of the Validation class, and passes it Me which the Validation class receives ByRef into MePointer, so that the Validation class may reach back and suck the data out of the form fields.

Insert / Update of the Validation class call Validate of the same Valiation class which does the actual field sucking and also runs through business validation rules.

Should it fail Validation, it turns the error fields red and pops a message box. The Insert / Update method of the database class are never called as the form did not pass Validation.

Had it passed, Insert / Update methods would then move onto transferring the good data to the DB object, and call Insert / Update respectively. Should that fail, an error message would be shown stating the DB error.

BTW: Those DB objects are global to the application, created in separate modules. And like I said, the Validation classes exist only in the context of the Add/Edit record forms.

Should that complete, THEN the form would be allowed to close. ffffeeeewww!!! :cool:
 
I like that concept! If I was to rework this module again, might just go that route.

Was rushing to lunch and said Global... Really the scope of the flag is the form itself.
Tried to find out if Access had ever thought about an Access Parent form closing before the sub form was completed.
Basically, it looks like we have to deal with this ourselves. If they could have only elevated some event to the parent form...
Thanks for the input - it tells me I wan't missing something simple.
 
Why do you need that Form Variable?

It would appear that the Close button could call the Function directly.

Chris.
 
That is a good point, the function in the sub form could be called directly.
Or, the function could be in the Close button itself.
That way, it could set the focus to the object on the correct subform that needs completed.

This is a simplistic representation of what could be in the Parent's close button:
Code:
Private Sub btn_Close_Click()
    ' use this to put in a variable or [B]function[/B] to check several controls on the sub form - the Function called could be in the Sub form to keep the code organized together.
    Debug.Print Forms!sundries_2!Wells_Sundry_Combined_1.Form!txtSpudDate
    ' The logic actually checks from this subforms control(s) is used
    ' logic above may or may not skip this docmd.close
    DoCmd.Close     
      ' if skipped
          ' assuming the above needed completed - skip the close and reset focus to whatever control the function found deficient
    ' setting the focus to a sub form takes two (2) commands - the container then the control.
    Forms!sundries_2!Wells_Sundry_Combined_1.SetFocus
    Forms!sundries_2!Wells_Sundry_Combined_1.Form!txtSpudDate.SetFocus
End Sub

So basically the parent form Close button polls the subform's controls - uses the result in a Function (located in the subform) that evaluates completeness. If complete, it closes, if not complete, the function returns the identity of the subForm's control that is deficient and issues a command to set the focus to that deficient control.

That is a mouthful for "basically".
Searched the web for half an hour and didn't come up with this at all.
Thanks to both of you, this may be a good, yet simple solution.

From the Parent command button, an example of a private function in the child form is shown. IsUserApproved(User_ID) is used as an example
Code:
' Run from Parent form - both of these work    
Dim Answer As String
     Answer = Me!Wells_Sundry_Combined_1.Form.IsUserApproved(2)
    Answer = Forms!sundries_2!Wells_Sundry_Combined_1.Form.IsUserApproved(2)
 
Last edited:
Did you actually manage to call a private function in the subform?

Chris.
 
Had to go look again - This is in the sub form:
Exact copy / paste of the Function 1st line
Function IsUserApproved(User_ID As String) As Boolean
doesn't have the key word public or private.
It is open inside a Parent form - inside a Tab control
 
If Public or Private is not specified then it is Public by default.

Hence you are calling a Public procedure not a Private procedure.

If you test with each word, or none, then you will see the result.

Chris.
 

Users who are viewing this thread

Back
Top Bottom