Button to Add Record to subform from main form

G

Guest

How do I code a button on my main form to add a new record to a subform?

I can reference controls in the subform from the main form easily enough,
but to add a new record uses DoCmd and that doesn't seem to work with
subforms...

Any ideas?
 
A

Allen Browne

Since the code is in the subform, you will need to:
a) SetFocus to the subform control (so it becomes the active control of the
main form), and
b) SetFocus to a control in the subform.

You can then move the subform to a new record:
RunCommand acCmdRecordsGotoNew
Then you can start assigning values to the controls.

If you don't need the user to supply values before the record is saved, an
alternative approach would be to AddNew to the RecordsetClone of the
subform.
 
G

Guest

Thanks for that Allen!

I used what you said to simply send focus to the subform, then the normal
code for adding a new record worked. 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