goto new record in subform (Datasheet View)

smercer

Registered User.
Local time
Tomorrow, 00:26
Joined
Jun 14, 2004
Messages
442
Hi all

I am trying to make some code to go to a new record in a sub form (called "frm_Each_Book_subform") from a command button.

Here is the code:
Code:
[COLOR=Blue]Private Sub[/COLOR] cmd_New_Copy_Click()
frm_Each_Book_subform!Cost_Price.SetFocus [COLOR=Green]'sets the focus to cost_price in frm_Each_Book_subform[/COLOR]
[COLOR=Blue]With[/COLOR] frm_Each_Book_subform
[INDENT]DoCmd.GoToRecord , Cost_Price, acNewRec  [COLOR=Green]'Gos to the new record[/COLOR][/INDENT]
[COLOR=Blue]End With
End Sub[/COLOR]

the problem I am having is it works but it makes the master form go to a new record too, as well as making a blank record in the subform (called "frm_Each_Book_subform").

all I want is for the subform only to goto new record

Can anyone help me? Thanks you smarties :)
 
Last edited:
YourSubControlName.SetFocus
SomeControlOnSubForm.SetFocus
DoCmd.GotTo etc.

you need two set focus statements, one to set focus to the sub a second to set focus to a control on the sub, don't see any need for the With statement here either.
 
Thanks Rich

You have half solved my problem. well done!!

What happens now is it selects the form, then selects Cost_Price but then I get an error on the acutual DoCmd.GoToRecord line. :(

Here is my code:

Code:
[COLOR=Blue]Private Sub[/COLOR] cmd_New_Copy_Click()
frm_Each_Book_subform.SetFocus [COLOR=Green]'sets the focus to  frm_Each_Book_subform[/COLOR]
frm_Each_Book_subform!Cost_Price.SetFocus [COLOR=Green]'sets the focus to cost_price in frm_Each_Book_subform[/COLOR]
DoCmd.GoToRecord , frm_Record_selected_FormView!frm_Each_Book_subform, acNewRec [COLOR=Green]'Gos to the new record[/COLOR]

[COLOR=Blue]End Sub[/COLOR]

Thanks alot for helping :)
 
DoCmd.GoToRecord , frm_Record_selected_FormView!frm_Each_Book_subform , acNewRec wrong!


DoCmd.GoToRecord , , acNewRec or DoCmd.GoToRecord , "", acNewRec
 
No sorry Rich,

but I have just tried that and they do not work.
(I end up with the original problem) :(

Good Try thanks
and good night :)
 
Last edited:
then you're not referencing the subform correctly, it's the name that appears on the MainForm property sheet for the subform control you need and not the name of the subform as it appears in the db window
 
hi all

it's the name that appears on the MainForm property sheet for the subform control you need and not the name of the subform as it appears in the db window
I always do get the name out of the form properties box

Well I found out why it was inserting a blank record. I had a "OnExit" on the Cost_Price field. Changed that to "OnDirty".

Current situation
I have also tried to nest the datasheet form (called "frm_Each_Book_subform") inside another form (called "frm_Nest_Each_Book_Subform")so that the master form will not change when you click. and basing the form on a query. so far so good. now all i get when I press the button, is it will go in to the subform like I want, without the master form changing to new record.

Current problem
but i still have the problem with the gotorecord line (now selection just sits there with no errors)

By the way I have deleted the last post so that this thread does not get too long

Current code
Code:
[COLOR=Blue]Private Sub [/COLOR]
cmd_New_Copy_Click()
frm_Each_Book_subform.SetFocus [COLOR=Green]'sets the focus to frm_Each_Book_subform[/COLOR]
frm_Each_Book_subform!Cost_Price.SetFocus[COLOR=Green] 'set the focus to Cost_Price in the frm_Each_Book_subform[/COLOR]
DoCmd.GoToRecord , , acNewRec [COLOR=Green]'Goes to the new record[/COLOR]
[COLOR=Blue]End Sub[/COLOR]

Thanks a lot for your help. You have been a big help (It is because your very clever)
 
Last edited:
I have just use the same code on different form and that works but I do not understand why this is not working.

Now what happens is it goes to the first record or it may go to the last activated cell when you click on the button. :confused:

I beleive it has something on how the form properties are set up does anyone know how to fix this?

Thanks all you smart people
 
Hi all

Now I know why it is not going to the new record, but I don't know how to fix it.

What I did was I went into the subform without it being in the master form itself and the button works well there, but when I put it in the master form, it ignores the gotorecord line :confused:

Is there a way around this? can I use something other then GoToRecord

Thanks everyone
 
Hi all

I got my problem fixed. thanks to all

here is my gotorecord substitute

Private Sub cmd_New_Copy_Click()
On Error GoTo new_Err

sfrm_Each_Book_subform.SetFocus 'sets the focus to sfrm_Each_Book_subform
DoCmd.RunCommand acCmdRecordsGoToNew
new_Err:
End Sub

Works well now.

thanks to all who gave this brain time!!!
 

Users who are viewing this thread

Back
Top Bottom