Update problem

G

Guest

I have a form with a few bound fields and a subform. I also have an addnew
command button. When I press the addnew button the form goes to a new record
where the fields take their defauld values. If I don't make any changes on
the default values or new entries in the main form and go to the sub form
directly the main form is nor updated and the record is lost.
Can anyone suggest an effective way of updating programmatically or
otherwise the main form.

Michalis
 
A

Allen Browne

You need to make at least one keystroke in the main form, to get the new
record started.

There are a couple of ways to avoid the orphan records in the subform:

1. Open the subform's *table* in design view.
Select the foreign key field (i.e. the one that links to the main form's
table.)
In the lower pane of table design, set its Required property to Yes.

2. Use the BeforeInsert event procedure of the form (the subform) to warn if
the main form is blank. Example:

Private Sub Form_BeforeInsert(Cancel As Integer)
If Me.Parent.NewRecord Then
Cancel = True
MsgBox "Enter the main form record first."
End If
End Sub
 
J

John Spencer

You could add a bit of code to your Add New button and set one of the
controls equal to itself. That will dirty the record which will mean a
record is automatically created.. The problem with this solution is that if
you decide you don't want the record, you will have to take some action to
cancel the creation of the new record.
 
G

Guest

Thanks a lot Allen. If there is no way to start the new record without
entering a value in one field I will go for your second suggestion.
 
G

Guest

Thanks a lot John. That trick worked fine. I have combined it with the
BeforeInsert event of the subform as Allen suggested so that the new record
is created when I insert some data in the subform. Otherwise the record is
descarted.
 

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