简体   繁体   English

Microsoft Access forms,删除记录按钮错误

[英]Microsoft Access forms, delete record button error

I have to create forms in ms access with a database linked from postgresql.我必须使用从 postgresql 链接的数据库在 ms 访问中创建 forms。

The forms need to show functionalities such as delete record, and this is where my problem is. forms需要显示删除记录等功能,这就是我的问题所在。 I have created a button with a delete command:我创建了一个带有删除命令的按钮:

Private Sub Delete_Click()

    If MsgBox("Are you sure?", vbYesNoCancel) <> vbYes Then
        Exit Sub
    End If
    DoCmd.RunCommand acCmdDeleteRecord

End Sub 

However when I press it, the command gets cancelled and comes up with a 2501 runtime error.但是,当我按下它时,该命令被取消并出现 2501 运行时错误。

I have only just started using ms access and any kind of commands so I'm unsure how to fix this issue.我才刚刚开始使用 ms access 和任何类型的命令,所以我不确定如何解决这个问题。

I haven't tried anything as no one else seemed to have this issue.我没有尝试任何东西,因为似乎没有其他人遇到过这个问题。

Generally this means that the underlying linked table does not contain enough information about the index scheme of the table to know how to formulate a DELETE statement.通常这意味着底层链接表不包含有关表的索引方案的足够信息以了解如何制定 DELETE 语句。 Specifically, it does not understand what the primary key is so that it can generate a DELETE statement that only deletes a single row.具体来说,它不知道主键是什么,因此它可以生成只删除一行的 DELETE 语句。

You can verify if this is your issue by opening the Linked Table itself, highlighting the row, and pressing delete.您可以通过打开链接表本身、突出显示该行并按删除来验证这是否是您的问题。 If the row cannot be deleted from the table, you won't be able to delete it from a form based on it.如果无法从表中删除该行,您将无法从基于它的表单中删除它。

If this is the case, to solve it, right click the table and run the Linked Table Manager.如果是这种情况,要解决它,请右键单击该表并运行链接表管理器。 When relinking the table, if Access needs more information about the index schema it will prompt for it.重新链接表时,如果 Access 需要有关索引架构的更多信息,它将提示您提供。 Help it by selecting the column(s) that comprise the primary key.通过选择组成主键的列来帮助它。 It will cache this information along with the linked table and you should be able to delete the current row from your form.它会将此信息与链接表一起缓存,您应该能够从表单中删除当前行。

Another approach would be to create a DELETE Query that references the ID in the form in the WHERE condition (ie, Forms.MyForm,ThePKFieldName), Then, in your code behind the button, replace the另一种方法是创建一个 DELETE 查询,在 WHERE 条件下引用表单中的 ID(即 Forms.MyForm,ThePKFieldName),然后,在按钮后面的代码中,替换

DoCmd.RunCommand acCmdDeleteRecord

with

 DoCmd.OpenQuery "qryYourQuery" 

See this documentation: https://learn.microsoft.com/en-us/office/vba/api/access.docmd.openquery请参阅此文档: https://learn.microsoft.com/en-us/office/vba/api/access.docmd.openquery

If this still does not work, your ODBC driver may need to be updated, or you can use an alternative driver.如果这仍然不起作用,您的 ODBC 驱动程序可能需要更新,或者您可以使用替代驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM