Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add a new record in a subform from a parent form. 2

Status
Not open for further replies.

vanleurth

Programmer
Sep 1, 2001
156
US
Hi everyone,

I would like to know;
How can I add a record to a subform from a parent form?
This is I have a button in the main form that adds a new record in the main form. Easy enough.

But whenever I try to do it so it will add the new record to the subform it gives me this;
The object 'frm phones sub' isn't open

Here is what I'm using


Private Sub btn_addnew_Click()
On Error GoTo Err_btn_addnew_Click


If Me.tab_form.Value = 0 Then Me.tab_form.Value = 1
DoCmd.GoToRecord acDataForm, "frm phones sub", acNewRec


Exit_btn_addnew_Click:
Exit Sub

Err_btn_addnew_Click:
MsgBox Err.Description
Resume Exit_btn_addnew_Click

End Sub

Thanks in advance,

V.
 
Technically, a subform isn't considered an open form, but is a control on the main form, and can't be referenced as this.

One workaround is to setfocus to the subform, then perform the acnewrec, and set focus back to the main form again. Perhaps something like this:

[tt]Me![frm phones sub].SetFocus
DoCmd.GoToRecord ,,acnewrec
Me!txtSomeControl.SetFocus ' setting focus back?[/tt]

Roy-Vidar
 
Vidar,

Thanks so much for the feedback. I've tried this;

Private Sub btn_addnew_Click()
On Error GoTo Err_btn_addnew_Click

' Check which tab as the focus and then insert a new record in that tab.
If Me.tab_form.Value = 0 Then
Me.frm_phones_list_sub!NumID.SetFocus
DoCmd.GoToRecord , , acNewRec

'DoCmd.GoToRecord acDataForm, "frm phones sub", acNewRec
ElseIf Me.tab_form.Value = 1 Then
DoCmd.GoToRecord , , acNewRec
End If

Exit_btn_addnew_Click:
Exit Sub

Err_btn_addnew_Click:
MsgBox Err.Description
Resume Exit_btn_addnew_Click

End Sub

Still gives me a message saying ...
You can't use the GoToRecord action or method on an object in Design view.
If you have any other ideas, it will be appretiated. In the mean time I'll look in google and see if I can get more insights.


V.
 
Hi again!

Just tested this on both a97 and XP, and made it work.

One thing, I set focus to the subform control not a control on the subform, so try:

[tt]Me.frm_phones_list_sub.SetFocus
DoCmd.GoToRecord , , acNewRec[/tt]

And, you are doing this from the parent form? Else, syntax like:

[tt]Me.Parent!frm_phones_list_sub.SetFocus[/tt]

or something similar might perhaps work.

Roy-Vidar
 
Very Strange,

Thanks for your time and answers. I have tried that but still no luck. I'm thinking perhaps, there is a library I'm missing or a property.

I'll keep trying with different properties.
But the help file says that this should work as well.
I don't understand why ACCESS is so a pain to program.

Any other ideas will be welcome, thanks.

V.
 
The subform name "frm_phones_list_sub", is that also the subform control name?

Check it in the "junction" properties, (where you also find the link child/master field properties). You should use the name from the "other" tab there.

The design view message, I get that too, when trying to step thru the code, but works fine just when hitting the button.

If nothing helps, try importing the form to a new database, in case it might be some corruption.

Roy-Vidar
 
Santas Cachuchas Batman !!

You are right. It didn't work because I was stepping through all the time.

Thank you so much. Star for you.

Access has so many levels of driving one crazy. Now, I can resume the development.

Thanks again

V.
 
Hi there,

A related question on this but in the opposite way.

I have a main form that has a combo box, a button, and a subform so that I can add records for the many size of a relationship. The subform is locked so user can't tamper with what they have put in. Now I want to add another button on the main form so they can delete the selected record on the subform. My idea was, once the user has selected a record on the subform then I would turn on this button (was not enable initially) so that they can click on it to perform the deletion. I don't know how to reference this button. It is sort of going from the subform back to the main form as supposed to from the main form to the subform.

Does any 1 here know how to reference a control on the main form from a subform?? If you do please share it with me.

TIA
 
how to reference a control on the main form from a subform
Forms![name of mainform]![name of control]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top