"You Can't Use the GoToRecord Action or Method ..."

PaulW
2StarLounger
Posts: 125
Joined: 17 Feb 2010, 16:25

"You Can't Use the GoToRecord Action or Method ..."

Post by PaulW »

I have a report that is opened in preview mode in the OnClick event of a command button in a subform called frm5-2-3 InvDistribution. The main form containing the subform is called frm5-2-0 Inventory Maintenance.

Here is the code in the OnClick event:

Private Sub Save_Record_Click()
On Error GoTo Err_Save_Record_Click

frm256ErrorMsg = ""
If IsNull(frm485DistributionDate) Or IsEmpty(frm485DistributionDate) Then
frm485DistributionDate = CLng(Date)
End If


DoCmd.RunCommand acCmdSaveRecord


strwhere = "Id = " & Me.Id
DoCmd.OpenReport "rptT4SDistributionReceipt", acViewPreview, , strwhere



DoCmd.GoToRecord , , acNewRec

Forms![frm5-2-0 Inventory Maintenance].Requery
Call calctopfields

Forms![frm5-2-0 Inventory Maintenance].Repaint


Exit_Save_Record_Click:
Exit Sub

Err_Save_Record_Click:
MsgBox Err.Description
Resume Exit_Save_Record_Click

End Sub

The issue: When the DoCmd.GoToRecord , , acNewRec statement following the DoCmd.OpenReport statement is executed, I get an error message stating "You Can't use the GoToRecord Action or Method on an object in Design View". Nothing is open in design view -- not the report nor any of the forms.

What am I missing here? Thanks in advance.
PaulW
Lost Wages, NV USA
(former Cobol Programmer)

User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: "You Can't Use the GoToRecord Action or Method ..."

Post by HansV »

You can't go to a new record on the form when the report has the focus. You can get around it by switching the commands to open the report and to go to a new record: instead of

strWhere = "Id = " & Me.Id
DoCmd.OpenReport "rptT4SDistributionReceipt", acViewPreview, , strWhere
DoCmd.GoToRecord , , acNewRec

use

strWhere = "Id = " & Me.Id
DoCmd.GoToRecord , , acNewRec
DoCmd.OpenReport "rptT4SDistributionReceipt", acViewPreview, , strWhere
Best wishes,
Hans

PaulW
2StarLounger
Posts: 125
Joined: 17 Feb 2010, 16:25

Re: "You Can't Use the GoToRecord Action or Method ..."

Post by PaulW »

Hans,

Again Thank You. I don't understand why it works that way, but I appreciate you sharing your knowledge with me.
PaulW
Lost Wages, NV USA
(former Cobol Programmer)

User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: "You Can't Use the GoToRecord Action or Method ..."

Post by HansV »

The line

DoCmd.GoToRecord , , acNewRec

acts on the currently active database object. In the original version of the code, the report had become the active database object by the time the line was executed. But in a report, you can't go to a new record, hence you get an error message. The mention of "an object in Design View" in the error message is misleading, but that is not uncommon in Access.
Best wishes,
Hans

PaulW
2StarLounger
Posts: 125
Joined: 17 Feb 2010, 16:25

Re: "You Can't Use the GoToRecord Action or Method ..."

Post by PaulW »

Hans,

Thanks again. I was just venting, but you answered in a coherent way that makes my understanding if this process much better. I am happy to know that I am not the only one who cannot understand/interpret the error messages. :smile:
PaulW
Lost Wages, NV USA
(former Cobol Programmer)