使用 vba DatasheetDoCmd.RunCommand acCmdDeleteRecord 从数据表中删除记录(错误 2046)

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

Delete record from datasheet with vba DatasheetDoCmd.RunCommand acCmdDeleteRecord (error 2046)

vbams-access-2007access-vbaruntime-error

提问by pec

Please help me!

请帮我!

What I have:

我拥有的:

First, I'm using Access 2007. I have a form that include's a subform. In that subform a have a datasheet from a SQL query which get 3 fields. The first field (Supprimer) his a dummy field to display a hyperlink that will be use has a button:

首先,我使用的是 Access 2007。我有一个包含子表单的表单。在该子表单中,有一个来自 SQL 查询的数据表,其中包含 3 个字段。第一个字段 (Supprimer) 是一个虚拟字段,用于显示将要使用的超链接有一个按钮:

SELECT "Supprimer", permis.abbreviation, permis.description
FROM permis 
INNER JOIN (clients INNER JOIN clients_permis 
  ON clients.ID=clients_permis.id_clients) 
  ON permis.ID=clients_permis.id_permis;

What I wantI want my "delete" hyperlinks to delete the datasheet selected record has would pressing the "del" key would

我想要什么我希望我的“删除”超链接删除所选记录的数据表,然后按“del”键

My code for onClick event

我的 onClick 事件代码

Private Sub del_Click()

    DoCmd.RunCommand acCmdSelectRecord
    'SendKeys ("{DELETE}")       
    DoCmd.RunCommand acCmdDeleteRecord

End Sub

If I uncommented, the Sendkeys ("{DELETE}")the record is deleted, but, on my systeme this also triggers the numlock key. Who knows what it can do on other system.

如果我取消注释,则记录的Sendkeys ("{DELETE}")将被删除,但是,在我的系统上,这也会触发 numlock 键。谁知道它可以在其他系统上做什么。

the DoCmd.RunCommand acCmdDeleteRecordcommand provokes a Run-Time Error: 2046.

DoCmd.RunCommand acCmdDeleteRecord命令引发一个运行时错误:2046

At a previous point the above code worked perfectly.

在前一点,上面的代码工作得很好。

采纳答案by pec

As mentioned in the comments by HK1, here's what can be done (although it's workaround):

正如 HK1 在评论中提到的,这里是可以做的(虽然这是解决方法):

Use a continuous form instead of the datasheet.

使用连续形式而不是数据表。