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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating new record in subform

Status
Not open for further replies.
Aug 16, 2002
3
US
I would like to have a button that creates a new record for a subform, and the button is not located on either the main form or the subform, but a different form.

I would think it would look something like this:

DoCmd.GoToRecord acDataForm, "Forms![Form]![SubForm].Form",acNewRec

But access will generate an error saying that form is not open. I also tried storing the "Forms![Form]![SubForm].Form" into a string variable, and putting the variable into the DoCmd function but that received the same error. Thanks for your help!
 
Rostabosta

I can give you somewhat dirty solution. Maybe someone will come up with a better idea.

1. Make a public sub procedure in a subform:

Public Sub MoveNewRec()
DoCmd.GoToRecord , , acNewRec
End Sub

2. In the main form (where the subform is) make a public sub procedure:

Public Sub SubSetFocus()
Docmd.GotoControl "SubFormControlName"
End Sub

3. In the OnClick event of the button write:

Sub Button_OnClick()
Form_MainFormName.SetFocus
Form_MainFormName.SubSetFocus
Form_SubFormName.MoveNewRec 'Use the Name of the form, not the SubFormControlName
End Sub

Hope it helps
Mangro
 
Well, that looks like it would work but Access doesn't like it.

I can't get it to set the focus on a subform control in the, "Public Sub SubSetFocus()" function. It will only set it for a mainform control. Maybe I just don't know the syntax, I tried:

DoCmd.GotoControl "SubFormControlName" , which didn't work
DoCmd.GotoControl "Forms![MainForm].[SubForm].Form.SubControlName" which didn't work either.

Thank you so much for your help. : )
 
Rostabosta

don't hold it against me but I must ask you if you used the control name you gave to the subformcontrol (Name property of subform control)? Do you have allow edits and all that stuff set properly?

It's strange it doesn't work. I tested it before I posted my reply and it worked.

Usually it is a bizarre problem that couses all the headache:)

Good luck
Mangro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top