Adding a record to a subform

R

Rich Ellis

Hi all,.

I have a small issue I would like to resolve with VBA. I have a form which
it's fields are bound to a table. Within that form is a subform which is a
subtable.

Tables relationship is a one to many from the main table to the sub table.

On the main form I have a "Add Record" button which creates a new record in
the main table.

What I want to do is put a statement in that Add Record button, on click
event that will automatically add 20 records to the subtable where the first
field in the sub form is numberd 1-20.


Obviously I can go to the subform and put in a button that will do that
using this code(Lane is the first field in the subtable/form):

Me.Lane = 1
Me.PartNumber = X
DoCmd.GoToRecord , , acNewRec
Me.Lane = 2
DoCmd.GoToRecord , , acNewRec
Me.Lane = 3
DoCmd.GoToRecord , , acNewRec
Me.Lane = 4
DoCmd.GoToRecord , , acNewRec
Me.Lane = 5
DoCmd.GoToRecord , , acNewRec
Me.Lane = 6
DoCmd.GoToRecord , , acNewRec
Me.Lane = 7
DoCmd.GoToRecord , , acNewRec
Me.Lane = 8
DoCmd.GoToRecord , , acNewRec
Me.Lane = 9
DoCmd.GoToRecord , , acNewRec
Me.Lane = 10
DoCmd.GoToRecord , , acNewRec
Me.Lane = 11
DoCmd.GoToRecord , , acNewRec
Me.Lane = 12
DoCmd.GoToRecord , , acNewRec
Me.Lane = 13
DoCmd.GoToRecord , , acNewRec
Me.Lane = 14
DoCmd.GoToRecord , , acNewRec
Me.Lane = 15
DoCmd.GoToRecord , , acNewRec
Me.Lane = 16
DoCmd.GoToRecord , , acNewRec
Me.Lane = 17
DoCmd.GoToRecord , , acNewRec
Me.Lane = 18
DoCmd.GoToRecord , , acNewRec
Me.Lane = 19
DoCmd.GoToRecord , , acNewRec
Me.Lane = 20


But I would prefer my users not have to push 2 buttons for a new record.
What code would I use to add to the main form's "Add Record" button to achive
the same results as adding a button and the code directly to the subform?

Thanks
 
M

Marshall Barton

Rich said:
I have a small issue I would like to resolve with VBA. I have a form which
it's fields are bound to a table. Within that form is a subform which is a
subtable.

Tables relationship is a one to many from the main table to the sub table.

On the main form I have a "Add Record" button which creates a new record in
the main table.

What I want to do is put a statement in that Add Record button, on click
event that will automatically add 20 records to the subtable where the first
field in the sub form is numberd 1-20.


Obviously I can go to the subform and put in a button that will do that
using this code(Lane is the first field in the subtable/form):

Me.Lane = 1
Me.PartNumber = X [ . . . ]
DoCmd.GoToRecord , , acNewRec
Me.Lane = 20


Try using:

With Me.subformcontrol.Form.RecordsetClone
.AddNew
!Lane = 1
!PartNumber = X
.Update
For k = 2 To 20
.AddNew
!Lane = k
.Update
Next K
End With
 

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