The reason its not saving the record was actually explained in my original
reply. Its because setting the DefaultValue of a control doesn't initiate a
new record, but merely puts values into the controls on the form. A new
record is only initiated if you either change one of those values or add new
data in another field on the form. If you want to save the subform record
you'll need to add another line of code to the procedure to do this. The way
to initiate a new record without changing a default value or adding new data
to another control is simply to set the value of one of the control's to its
existing value, so you could put this line after the others in the procedure:
Me.sfrBillings.Form.Ad1 = Me.Ad1
It only has to be done with one of the controls not all of them. If you do
this the new record added to the subform will be saved. You'll notice
however that the subform will now have the same values showing in its next
new record.
If you are only adding one record to the subform for the current parent
form's record, and you want it automatically saved unless you 'undo' it (but
still allowing you to make changes before its saved), then a better strategy
would be to set the Value property of each control in the subform. This is
the controls' default property (not to be confused with its DefaultValue
property) so you don't need to specifically refer to it, merely to the
control, you don't need to wrap the value in quotes, and you don't need the
extra line to 'dirty' the form. You should first move the subform to a new
record, however, or you'll overwrite any existing subform record which is its
current record. You can avoid repetition of the reference to the subform's
Form property by setting an object variable (frm below) to point to the
subform's underlying form. The code would thus go like this:
Dim frm As Form
Set frm = Me.sfrBillings.Form
Me. sfrBillings.SetFocus
DoCmd.GoToRecord Record:=acNewRec
frm.Ad1 = Me.Ad1
frm.Ad2 = Me.Ad2
frm.Ad3 = Me.Ad3
frm.Ad4 = Me.Ad4
As far as your other question is concerned, if you use this alternative
method and set the Value of each control rather than its DefaultValue
property you can automate this by putting the code in the AfterInsert event
procedure of the parent form so the values are passed to the subform
automatically when you enter a new parent record. To do this select the form
in design view by clicking on the little black square in its top left corner.
In the properties sheet select the After Insert event property on the Event
tab. Click on the 'build' button; that's the one on the right with 3 dots.
Select Code Builder in the next dialogue and OK out of it. The VBA window
will open at the form's AfterInsert event procedure. Enter the code as new
lines between the two lines which will be already in place.
Using the alternative code above don't put the code in the form's Current
event procedure, but if you use the original code plus the extra line, then
you should also put the same code in the form's Current event procedure by
following the same procedure after selecting that event property in the
form's properties sheet.
Ken Sheridan
Stafford, England
"Ultraviolet47" wrote:
>
> OK-now I have a strange problem! If I enter the details in the shipping
> form and billing subform by hand, it saves them to the relevant tables.
>
> However, if I enter the details in the shipping form and then click
> transfer with the code, it transfers the data over to the subform, but
> when I click save, it doesn't save the subform data to the billing
> table, but does save the shipping form data to shippng table?
>
> Is it something to do with my link child fields, etc?
>
> Thanks
>
>
|