Subform Validation

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

Hi All,

I'm having a problem with validating a continuous subform's data. I'm
calling a validation function on Before Update to check values for 4 fields
so that the data is validated before the user starts the next line. The
problem I run into is allowing the user to close without saving data when
there is an incomplete record in the subform. When the close button (on the
main form) is clicked, the subform's data validation fires because of the
Before Update call to the validation function.

Anyone know of a way around this?

Thanks & Ciao,

Tony
 
If you want to allow the user to quit in mid-record, you need to decide
what to do with the record. You either discard the incomplete record
(Me.Form.Subform.Undo) or validate and store it (this is what you are
doing, right?).
If you Undo, then you want to skip validation. For that, you should have
a

If Me.NewRecord = True Then ... Validate Record

check in the validation code

Good luck,
Pavel
 
Hi Pavel,

Thanks for the info. However, I don't think I was clear enough...

The button that closes the main & sub forms is on the main form. When I try
to click that button after entering a partial record in the subform, the
subform's Before Update fires. Since this has the validation call in it,
the validation gets run before the close button receives the click. I have
the validation in the Before Update so that the records on the subform are
validated each time the user tabs to a new line. And, I've incorporated a
button on the subform so that the user can delete individual records. What
I want to do may be impossible, but here's what I'd like to happen:

1) User in the subform decides to close the form without completing the
record in the subform (they really don't need to know about main & subforms)
2) User clicks cmdClose in the main form. Before the subform's Before
Update can fire, I cancel it and prompt the user with 'Record not saved.
Continue?'
3) User clicks yes and the form closes, deleting the record in the main form
and any records, complete or not, in the subform

Make a little more sense?

Again, thanks for the assistance.

Tony
 
Hi Tony,
I think that you should try something like this, in the OnClick event of
the cmdClose button, *before* you code closes the form (using DoCmd or something):

if Me.SubformControl.Form.Dirty Then
if MsgBox("Record was not saved. Are you sure?", vbOkCancel) = vbOk Then
Me.SubformControl.Form.Undo
Else
Exit Sub
End If
End If

I think that it is the closing of the form that causes the update and
subsequent validation in the subform. If you discard the changes before
that happens, your validation code will not run (assuming you are
checking if it is a NewRecord) or it will not find issues (because there
is no new record data).
Good luck,
Pavel
 
Hi Pavel,

I'm sure your suggestion would work fine if the close button was on the
subform & perhaps I should put it there. Right now it's on the main form
and when I click it, the onClick does not fire first (as far as I can tell).
Before Update (for the subform) fires first & calls the validation function.
And, I need the Before Update to call the validation because I allow the
user to tab to the next record in the subform.

If you can think of anything else, post when you have a chance. I'm going
to look at putting the close button on the subform and see how that works in
the meantime.

Again, thank a million for the help. I appreciate it.

Tony
 
How difficult would it be to move the prompt to "Record is incomplete.
Discard?" into the validation code? Maybe that would be a solution?
I think I see what you are saying - the Update in subform occurs as soon
as you leave the SF for the Close button, before you get to the button.

Pavel
 
Hi Pavel,

You hit the nail on the head. Unfortunately, I need the validation code to
run in two places; when a new record is created on the subform and when the
user attempts to close without saving. The thing that trips me up is when
the validation runs on attempting to close the record.

I've given it some thought & the odds that the user will create a new record
without completing one are slim, so I'm only going to run the validation on
close. I'm probably going to have to go back to the drawing board on this
form to make it bullet-proof, but will move forward as is.

Thanks again for your suggestions. I appreciate the help.

Ciao,

Tony
 
Back
Top