Closing a Form after Validation (1 Viewer)

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
Wow! All I was missing was the:

Code:
If frm.Dirty = True Then
at the beginning of the function.

Yes, that is exactly how I wanted it to work!!
Thanks DBguy for all your help.
Very much appreciated.
 

theDBguy

I’m here to help
Local time
Today, 05:27
Joined
Oct 29, 2018
Messages
8,767
Wow! All I was missing was the:

Code:
If frm.Dirty = True Then
at the beginning of the function.

Yes, that is exactly how I wanted it to work!!
Thanks DBguy for all your help.
Very much appreciated.
Hi. You're very welcome. We're happy to help. Good luck!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:27
Joined
Feb 19, 2002
Messages
28,625
Validation should be done IN the form's BeforeUpdate event or called from it. Think of the form's BeforeUpdate event as the flapper at the bottom of a funnel. Everything goes through the funnel and the form's BeforeUpdate is the last event to run and is the one that decides if a record may or may not be saved. Validation done in other events, will fail under certain conditions.

Rather than running the validation code twice, I would just run it once in the form's BeforeUpdate event. The button click event would then be:

Code:
Private Sub Command5CloseButton_Click()
On Error GoTo ErrProc
    If Me.Dirty Then
        DoCmd.RunCommand acCmdSaveRecord        
    End If
    DoCmd.Close
ExitProc:
    Exit Sub
ErrProc:
    Select Case Err.Number
        Case 2501
        Case Else
            Msgbox Err.Number & "--" & Err.Description
     End Select
     Resume ExitProc
End Sub
NOTE: under some conditions the error might not be 2501 so check carefully to see what error you get when the update is cancelled and add any other error number you want to ignore to the case statement. The close will raise an error if the update is cancelled.

FIXED typo in code.
 
Last edited:

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
Pat,
When I input the code for the command button, it won't compile.
"Compile Error: Expected Next"
Am I missing something?
Brad
 

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
DBguy - I want to also force an entry by the user, for a Location in the subform.
I've tried a few things but no dice....
I was thinking to set the focus to the control cboLocation1 and then using,
DoCmd.GoToRecord , , acNewRec.
I just can't seem to make it work.
Any hints....??
 

theDBguy

I’m here to help
Local time
Today, 05:27
Joined
Oct 29, 2018
Messages
8,767
DBguy - I want to also force an entry by the user, for a Location in the subform.
I've tried a few things but no dice....
I was thinking to set the focus to the control cboLocation1 and then using,
DoCmd.GoToRecord , , acNewRec.
I just can't seem to make it work.
Any hints....??
Hi. Unfortunately, this would be a chicken and egg situation. If the subform is bound to a child table of the main form, then you cannot create a record in the subform until you have a parent record in the main form. But if you're saying you don't want to save the new record on the main form unless there's a record in the subform, then you can't create a record in the subform because there is no parent record yet.
A possible workaround is to put something in the Current and Close event of the form to check for any empty child records and either open/navigate the main form to the parent record without a child, or delete the parent record and close the form.
 

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
Could I force the subform to be dirty if the parent form is dirty?
I put the * Tag on the subform and the subform control I want filled so if the subform is dirty it seems to work.
Thoughts on that approach?
 

theDBguy

I’m here to help
Local time
Today, 05:27
Joined
Oct 29, 2018
Messages
8,767
Could I force the subform to be dirty if the parent form is dirty?
I put the * Tag on the subform and the subform control I want filled so if the subform is dirty it seems to work.
Thoughts on that approach?
Hi. I still think the same as before, but give it a try, so we can know for sure. Let us know how it goes...
 

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
Pat,
I changed the "On Error Resume ErrProc" to "On Error GoTo ErrProc" and it works!
Thanks for that.
I can see that BeforeUpdate Event is final stop before the record gets saved.
 

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
I'm still at a loss in figuring out how to force the user to enter data in the subform, F_Location. The subform is a continuous form with only on control, cboLocation1.
I'm sure this too should be in the Before Update Event of the parent form, F_Project, but I've tried a couple of ideas after searching the internet for hours now.

Can someone give me hand?

I'm sure this has been done many times before but I can't seem to find it explained anywhere.

Thanks for any help!
 

theDBguy

I’m here to help
Local time
Today, 05:27
Joined
Oct 29, 2018
Messages
8,767
I'm still at a loss in figuring out how to force the user to enter data in the subform, F_Location. The subform is a continuous form with only on control, cboLocation1.
I'm sure this too should be in the Before Update Event of the parent form, F_Project, but I've tried a couple of ideas after searching the internet for hours now.

Can someone give me hand?

I'm sure this has been done many times before but I can't seem to find it explained anywhere.

Thanks for any help!
Hi. As I have been trying to tell you, this is a Catch-22 situation. The only way I know it can be done is if you use unbound forms (or forms bound to temporary tables). But if you find a different solution than that, then it would be interesting to know it. So please, make sure to share it with us. Good luck!
 

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
DBguy,
The subform is bound to its own table. (It’s actually a junction table for later use) But it’s not bound to the table of the parent form.
I thought that is what you meant in your previous post.
Does that make a difference or am I still missing the point?
Thanks
 

theDBguy

I’m here to help
Local time
Today, 05:27
Joined
Oct 29, 2018
Messages
8,767
DBguy,
The subform is bound to its own table. (It’s actually a junction table for later use) But it’s not bound to the table of the parent form.
I thought that is what you meant in your previous post.
Does that make a difference or am I still missing the point?
Thanks
Unfortunately, no, it does not make a difference. Like I was saying, this is a chicken or egg or catch-22 situation. To save a record in the child table, you need a parent. But you're asking not to save the parent without a child, which is almost impossible.
 

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
Ah I see.
So I added this step since I notice a problem when you add a project and the user only inputs the Location and then closes the form.
I put code in the OnCurrent Event of the subform, F_Location to enable the form only of then main form has a ProjectID.
This seems to work fine
Now, since that is in place, the user will be required to input into the main form first, creating the record before the choosing a Location.
But I still need to either make the subform dirty, thereby fire the before update for the subform or put something in the close or before update of the main form.
Is that more on track?
If so, that’s where I’m stuck.
Thanks
 

theDBguy

I’m here to help
Local time
Today, 05:27
Joined
Oct 29, 2018
Messages
8,767
Hi. I have already left the office to start the long weekend, so I'm not in front of a computer now. But to make sure we're on the same page, I will ask you a series of questions, and all I want you to do is quote my question and answer it with just a yes or a no. Please don't elaborate on your answers, because I don't want us to get sidetracked. I can't ask you all the questions at once because the next question may depend on your answer to the previous one. Okay, so here we go.

Let's say I, as a user, opens your main and subform to a new record, which means the main and subform are both blank. The first question is, do you want me to able to select a location first?
 

theDBguy

I’m here to help
Local time
Today, 05:27
Joined
Oct 29, 2018
Messages
8,767
No
(Message needs 10 characters or won’t send)
Please quote the question you're answering, and you won't get that notice. Also, it will confirm which question you were answering.

Okay, moving on. Two questions this time.

So, you want the user to fill in the main form first. After doing so,

1. Do you want me to be able to Close the form without selecting a location? And,

2. Do you want me to be able to go to a new record to enter new information on the main form without selecting a location for the first record on the main form I just entered?
 

Weekleyba

Silver Supporter
Local time
Today, 07:27
Joined
Oct 10, 2013
Messages
288
1. Do you want me to be able to Close the form without selecting a location?
No

2. Do you want me to be able to go to a new record to enter new information on the main form without selecting a location for the first record on the main form I just entered?
No
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom