Save default values on a subform

G

Guest

Hi. I am opening a form (WorkOrder) with a subform and am populating the
fields on the subform, using the Default property, with values from the
previous form (Project). The user does not have to enter any other values,
though there is an additional optional field, on the subform. There is a
report button on the main form. If the user does not add data to the optional
field the default data in the subform is not saved to the table and the
report is empty. How can I save the default data in the subform when it is
never updated by the user?

I have tried me.requery in the Open and Activate events of the subform, and
me.SubformHolder.requery in the Open event of the main form. I have tried
me.dirty = false in the Open and Activate events of the subform, and other
stuff. Nothing I've tried saves the record with the default values. I am down
to the wrap-up of this project and need this to work to be finished! Any help
is appreciated.
 
B

Baz

You are thinking along the right lines with the Dirty property. However,
Me.Dirty = False will not work because the record isn't dirty anyway. What
you need to do is to dirty it i.e. Me.Dirty = True.
 
J

John W. Vinson

You are thinking along the right lines with the Dirty property. However,
Me.Dirty = False will not work because the record isn't dirty anyway. What
you need to do is to dirty it i.e. Me.Dirty = True.

I think you're mistaken, Baz. If the record isn't dirty then there is nothing
to save (it's already saved, or it is unchanged from when it was opened). The
squib of code

If Me.Dirty Then
Me.Dirty = False
End If

will check to see if there are changes on the form, and if so, save them.

I don't believe that you can set the Dirty property to True other than by
explicitly dirtying the form - i.e. changing the value in some bound control.

John W. Vinson [MVP]
 
B

Baz

John W. Vinson said:
I think you're mistaken, Baz. If the record isn't dirty then there is nothing
to save (it's already saved, or it is unchanged from when it was opened). The
squib of code

If Me.Dirty Then
Me.Dirty = False
End If

will check to see if there are changes on the form, and if so, save them.

I don't believe that you can set the Dirty property to True other than by
explicitly dirtying the form - i.e. changing the value in some bound control.

John W. Vinson [MVP]

You believe wrong. I've been doing this for years in exactly the same
circumstances as the OP is dealing with i.e. where a new record is
adequately populated by default values and the user doesn't need to change
anything.

The following saves a record that isn't dirty, try it:

SomeControl.SetFocus 'The focus needs to be in a bound control
Me.Dirty = True
Me.Dirty = False
 
J

John W. Vinson

The following saves a record that isn't dirty, try it:

SomeControl.SetFocus 'The focus needs to be in a bound control
Me.Dirty = True
Me.Dirty = False

But isn't it the last line - setting Me.Dirty to False - what actually saves
the record?

John W. Vinson [MVP]
 
B

Baz

John W. Vinson said:
But isn't it the last line - setting Me.Dirty to False - what actually saves
the record?

John W. Vinson [MVP]

Me.Dirty = False does nothing if it's already false i.e. the record isn't
dirty: it's a property setting not a method call. This is the whole point
of this thread: it's a new record and the required fields are all populated
by default values which the user is happy with. Hence the record is not
dirty and needs to be dirtied before it can be saved. This can be done by
Me.Dirty = True.

Please try it for yourself.
 
J

John W. Vinson

Me.Dirty = False does nothing if it's already false i.e. the record isn't
dirty: it's a property setting not a method call. This is the whole point
of this thread: it's a new record and the required fields are all populated
by default values which the user is happy with. Hence the record is not
dirty and needs to be dirtied before it can be saved. This can be done by
Me.Dirty = True.

Please try it for yourself.

<smacks forehead>

You're quite right, Baz. My error.

John W. Vinson [MVP]
 
G

Guest

Thanks so much; seems so simple when you see it laid out like that ;-)
I'm putting the code in now!

KJW
 
G

Guest

I think you pointed out where I was off; I was thinking of it more as a
method, not a property.
However, next part of the question, Baz... I'm trying to figure out where to
put the code. If I put it in the Open event of the subform I get an error
because the main form is not loaded and edited yet, so there is no
corresponding record in the main table yet. Is there a way to structure the
code to make the subform dirty from the main form? The report button is also
on the main form.

Thx!
KJW
 
G

Guest

Oh my...there I go not thinking clearly again.
I answered my own question and it is working like a champ!
I push a workorder type to the main form so I just saved it, then I can save
the subform data values in its Open event. Means the user can't escape out of
creating the workorder, though. So, if you have any better ideas, let me know.
Thanks again.
 
B

Baz

Can you do the Dirty/Save of the subform in the AfterInsert event of the
main form i.e. the subform record only gets saved after the main form record
is saved?
 
G

Guest

I can't get a syntax that works to set the subform's dirty property from
anywhere in the main form. If you know the way to do that, what you are
saying might work. If I could figure out the syntax to do that I could do it
in AferInsert or the OnClick event of the Report button. Saving the subform
from within the main form definitely seems like the answer, but I can't get
there with what I know or can find in my trusty Developer's Handbook. If
anybody knows how, please hellllp!
 
J

John W. Vinson

I can't get a syntax that works to set the subform's dirty property from
anywhere in the main form. If you know the way to do that, what you are
saying might work.

Try

Me!subformcontrolname.Form.Dirty = True

using the name of the subform control (the container), which might or might
not be the same as the name of the form within it.

John W. Vinson [MVP]
 
G

Guest

Thank you, thank you, thank you!!!
I was close once or twice; didn't get the Form parm in there; that stuff
always throws me.
Putting that in the click event of the report button right after saving the
main form makes it all work smoothly for any of the 13 types of workorders
the user can create.

In case you're interested...this little bit of code works to save a work
order header record and create a work order details record that the user
never touched and runs a report to prove it :)

Me.Dirty = False 'save data in main form
'need to save the data in the subform as well
Me.subfrmWODetails.Form.Dirty = True
Me.subfrmWODetails.Form.Dirty = False

DoCmd.OpenReport strReportName, acPreview, , "WONum = " & WONum

Thanks again to both of you,
KJW
 
J

JEN

Hi,

I am a very beginner MS Access person. I have a DB that has a main form and
then a sub form. The main form is Footwear inspection information and the sub
is the inspection default codes. We defaulted the sub form to N/A and a zero
for number of defects but when the user saves the N/A does not save. They use
this a lot since not all shipments have defects. I think I need the me.dirty
but not sure how to use it or where to put it. Thanks!
 

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