Yes, you can quite easily do this.
Two things:
I assume that you turned "off" the allow additions in the sub-form (this
does give you "extra" space, as you will not see the "extra" blank line
where a user could just cursor into and start adding that new record (is
this assuming correct???).
So, our code will
add the new record
requery the sub-form to display this new record (because we used
code..it don't display automatic)
move the focus/cursor to that new record
Also note that the code that the custom menu calls HAS TO BE placed in the
PARENT form. So, that simply means that the right click code has to be in
the main form, as the menu bar can't resolve to the sub-form.
So, lets first write out the code needed to add the new record in the
sub-form, and then move the cursor/focus to the new record.
Public Function AddToSub()
Dim rst As dao.Recordset
Dim lngNewID As Long
Dim f As Form
Set f = Me.child1_test.Form
Set rst = f.RecordsetClone
rst.AddNew
rst!contact_id = Me.ContactID
lngNewID = rst!ID
rst.Update
Set rst = Nothing
' now re-load sub-form to display new record
f.Requery
' now move to this new reocrd
f.Recordset.FindFirst "id = " & lngNewID
' now set the focus to the sub-form
Me.child1_test.SetFocus
' now set the focus to the field on teh sub-form
Me.child1_test.Form.desc.SetFocus
End Function
So, the above is only about 10 lines of code, but I spread it out with
comments.
Note that since we are using *code* to add the record, you have to set the
child/master link field in our code. Also, much of the above code would NOT
be needed if your sub-form is set to allow additions (but, then why do you
need the right click feature then? -- you have to correct me if my assuming
is wrong..as then I am sending you on a wild goose chase and wasting your
time).
Ok, I would then test the above code by placing a button on the main form
(that we will remove later).
The code behind this "test" button would be:
AddToSub
Does the above work...get his code working....
Ok....got the button working?
Now, lets add this to a right click for the sub-form.
So, we need to create a custom right click for the sub-form.
first step, create a new menu bar.
(right click on your menu bar-> customize)
You should be on the "toolbars" tab, and simply click on eh "new button"
Lets call the new menu bar MySubAdd
Now, lets add a command to this menu bar
click on the command tab, and then on the right side, drag the "custom"
setting to the new menu bar
now, right click on the new menu bar (custom), select "properties". You can
now change the 'custom' text to
Add a new child record
In the "on-action" setting, we put the name of the function. In our example,
it i s AddToSub, so,
AddToSub()
Close this
Now, while in customise mode, go back to the toolbars tab, and select the
new menu we made. Then, hit the properties button.
Change the type of menu bar to a popup.
Please make note of and read the message you get at this point.
Now, simply open up your sub-form in design mode, and in the other tab of
the properties sheet, specify our menu as the
ShortCut menu Bar
(in fact, the dropdown for that setting will only show your new shortcut
menu you built).
That is it.
Now, when you right click on the sub-form, you get that new shortcut
menu....
You can also add additional features (such as cut/paste) etc. to this
shortcut menu if you wish (just use the standard approach to create menu
bars...and use drag and drop (hold down the control key when you "steal"
other built in menu bars, as you want to *copy* those menu items...not MOVE
them!!!). Not that you must display the "shortcut" menu bar, as that is
where ALL YOUR shortcut (popup) menus will be placed (and hat nasty long
message I told you to read tells you the same thing!!!).