vba 如何将新记录添加到子表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6796476/
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-08 11:40:31  来源:igfitidea点击:

How to add new record to subform

ms-accessvbaaccess-vba

提问by MAW74656

I recently got help on When select value from combo, highlight that record in Access subform datasheetand now I'm trying to do a related task.

我最近在从组合中选择值时获得帮助,在 Access 子表单数据表中突出显示该记录,现在我正在尝试执行相关任务。

How can I make the subform give me a new row for data entry from a button on the main form?

如何让子表单从主表单上的按钮为我提供一个新的数据输入行?

Tried the method found here with no luck: http://www.access-programmers.co.uk/forums/showthread.php?t=26537

尝试在这里找到的方法没有运气:http://www.access-programmers.co.uk/forums/showthread.php?t=26537

EDIT: Not sure if it matters, but the table my subform is supposed to update is a linked table to SQL Server.

编辑:不确定是否重要,但我的子表单应该更新的表是到 SQL Server 的链接表。

采纳答案by Jim Parker

Please first make sure your recordsource is updateable by opening the table or query in which your subform recordsource is based, and ensure that you can edit and add new records. If not, then it's possible that your SQL Server linked table is missing a unique indentifier. To create one, simply delete the linked table, and relink it. Upon relinking, MS Access should prompt you to specify a unique index (may take more than one field to make it truly unique). Then try again.

请首先通过打开您的子表单记录源所基于的表或查询来确保您的记录源可更新,并确保您可以编辑和添加新记录。如果不是,则您的 SQL Server 链接表可能缺少唯一标识符。要创建一个,只需删除链接表,然后重新链接它。重新链接时,MS Access 应提示您指定唯一索引(可能需要多个字段才能使其真正唯一)。然后再试一次。

回答by David-W-Fenton

If the subform datasheet is editable and with AllowAdditions turned on, it should already give you the * button to add a new row.

如果子表单数据表是可编辑的并且打开了 AllowAdditions,它应该已经为您提供了 * 按钮来添加新行。

If it doesn't, then you need to check the datasheet's properties and change them to match what you're actually seeking.

如果不是,那么您需要检查数据表的属性并更改它们以匹配您实际寻找的内容。

回答by Krapivnik

For those who need just to add a new record to sub form: just set focus from main form to subform. Then use DoCmd to add new record:

对于那些只需要向子表单添加新记录的人:只需将焦点从主表单设置到子表单即可。然后使用 DoCmd 添加新记录:

Forms![MainFormName]![SubFormName].SetFocus
DoCmd.GoToRecord , , acNewRec

回答by Dean Davids

it has been a while since I used Access. Doesn't the RecordSource property of the subform give you the underlying table or query. If the source is editable, you should be able to add a record then Requery or Refresh the subform.

自从我使用 Access 以来已经有一段时间了。子窗体的 RecordSource 属性不会为您提供基础表或查询。如果源是可编辑的,您应该能够添加记录,然后重新查询或刷新子表单。