vba DmCmd.GoToRecord , , acNewRec 运行,但是不能添加其他代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23504679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:00:47  来源:igfitidea点击:

DmCmd.GoToRecord , , acNewRec runs, but can't add other code

vbams-access-2007

提问by Mitch

I am using an Access form for data entry. When the user gets to the end they have two command button options. 1) Add From Same Capture, and 2) Add from Different Capture. If the user wants to enter data from the same capture they click the command button. I envision some VBA code to do the following:
1) Add the record to the underlying table
2) Clear the form so it is ready for the new data.
3) Auto fill several fields from the previous record.
4) Go to the top of the form.

我正在使用访问表单进行数据输入。当用户到达最后时,他们有两个命令按钮选项。1) 从同一捕获添加,以及 2) 从不同捕获添加。如果用户想要输入来自同一个捕获的数据,他们可以单击命令按钮。我设想一些 VBA 代码来执行以下操作:
1) 将记录添加到基础表
2) 清除表单,以便为新数据做好准备。
3) 自动填充上一条记录中的几个字段。
4) 转到表格顶部。

So far I've only been able to get the DoCmd.GoToRecord , , acNewRecto work.

到目前为止,我只能让DoCmd.GoToRecord 、 、 acNewRec工作。

When I use this code I receive a "Run-time error '2105': You can't go to the specified record"

当我使用此代码时,我收到“运行时错误‘2105’:您无法转到指定的记录”

Sub cmdSameCap_Click()
    DoCmd.GoToRecord , , acNewRec
    DoCmd.GoToControl CapDate
End Sub

I figure I can't have commands below the .GoToRecord, but when I convert a macro (which does this same thing) it shows that the .GoToControl is underneath the .GoToRecord. When I copy paste or use the VBA conversion and attempt to add the autofill code I get the same error.

我想我不能在 .GoToRecord 下有命令,但是当我转换一个宏(它做同样的事情)时,它显示 .GoToControl 在 .GoToRecord 下。当我复制粘贴或使用 VBA 转换并尝试添加自动填充代码时,我得到了同样的错误。

Any suggestions, Thank you!

有什么建议,谢谢!

Mitch

米奇

回答by Mitch

I've solved this issue as Wayne suggested.

我已经按照韦恩的建议解决了这个问题。

I saved the values on the current form as a variable. Then used:

我将当前表单上的值保存为变量。然后使用:

Me.CapDate.SetFocus
DoCmd.GoToRecord , , acNewRec

To move to the next record. I then set the values for the new record equal to the variables I declared. Works like a champ!

移动到下一个记录。然后我将新记录的值设置为等于我声明的变量。像冠军一样工作!