Full Version: Open Subform To A New Record
UtterAccess Forums > MicrosoftŪ Access > Access Forms
SpiritedAway
Hi all,
I have a continous subform (frmB) on a main form (frmA) (Main form is unbound - has a navigation bar)
I'm trying to open the subform (frmB) to a new record each time the Mainform (frmA) is opened.
On the Load event of the Main form (frmA), I have:
Me.[frmB].SetFocus
Docmd.GoToRecord , , acNewRec
But when the main form opens the continuious subform (frmB) is still displaying all the records.
Can anyone advise on why this code isn't working.
I'm using A2007
Thanks for your help.
BruceM
You could open the subform to a new record. In the subform's Load event:
Me.Recordset.AddNew
However, the nature of a continuous subform is that you see all of the records. It's not clear exactly what you want to accomplish.
The code isn't working because although you have set the focus to the subform control, the next line of code applies to the main form since that's where the code is located. To perform an action of the subform from the main form you would need to reference the Form property of the subform control, then take action from there. Something like:
Me.SubformControlName.Form.Recordset.AddNew
I'm not sure how to do that with DoCmd.
missinglinq
I've answered this elsewhere for the OP, but I'll put it here in case someone else has similar needs and finds this thread.
On the Main Form OnLoad event set focus to the Subform Control. Make sure you're using the name of the Control, not the name of the form the Subform control is based on, unless they are the same. Sometimes they are and sometimes they aren't.
Now, in the OnLoad event of the form being used as the Subform, place the code
Docmd.GoToRecord , , acNewRec
Linq ;0)>
SpiritedAway
Hi,
did as you suggested:
Main Form OnLoad event I did
ProjectSub.Setfocus
and on the Onload event of the subform I did
Docmd.GoToRecord , , acNewRec
But its still showing all the records when the main form is open. I've uploaded a stripped back version -
ProjectSub is the sub form on the left of the main form -
Thanks once again for your help
SpiritedAway
I think I found a solution that I thought I would share just in case anyone has similar needs -
On the ProjectSub - I set the 'Data Entry' property to true
and it works! - when the mainform opens - the subform is on AddNew mode.
Thanks once again for you help - i appreciate it.
missinglinq
Per your original post
The code I gave you does just that! You failed to mention that you only wanted a new record to be added, without seeing the other records in the Subform! To be honest, having a Subform simply to add a new record, without displaying the existing, related records, is bizarre, to say the least. It really defeats the whole purpose of having a Subform and adds unnecessary overhead to your app. Most developers would simply pop up a second form for adding the record, passing it the field that relates the two tables.
I'm glad you got it working! Good luck on your project!
Linq ;0)>
missinglinq
In reading this over when coming back in tonight, I realized that the code, in the Main Form, for setting Focus to the Subform Control, should be in the Form_Current event, rather than the Form_Load event.
orry! dunce.gif
Linq ;0)>
BruceM
I think the Load event accomplishes the intent of moving to the subform when the main form is opened. Moving the focus to the subform control every time the user navigates to another record may not have the desired effect, but in any case should not happen if it is a new record (assuming the subform records are related to the main form records in a typical way).
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.