防止 Access 2007 中的意外数据覆盖

发布于 2024-12-11 19:58:53 字数 1081 浏览 0 评论 0原文

我有一个简单的 Access 2007 数据库,并且在表单中添加了一个按钮,用于切换编辑当前显示记录的能力。

默认情况下,我将AllowEdits、AllowAdditions 和AllowDeletions 设置为 false。单击“编辑模式”按钮可以更改此设置。

我还添加了几个表单事件,以便当显示的记录更改时,编辑模式会被取消。

我现在发现无法创建新记录,因为当我单击 BtnNew(附加标准添加新记录宏)时,我收到错误“您无法转到指定的记录”。

我在数据库中的VB代码如下,任何人都可以看到我做错了什么,或者从哪里开始寻找? (我缺乏 VB/Access 知识,但我理解这些概念,因为我是一名 C# 开发人员)。

Private Sub BtnEdit_Click()
    If lblEditMode.Caption = "Edit Mode" Then
        Disable
    Else
       Enable
    End If
End Sub

Private Sub Form_AfterDelConfirm(Status As Integer)
    Disable
End Sub

Private Sub Form_Current()
    Disable
End Sub

Private Sub Disable()
    AllowEdits = False
    AllowAdditions = False
    AllowDeletions = False
    BtnNew.Enabled = False
    BtnDelete.Enabled = False
    lblEditMode.Caption = ""
End Sub

Private Sub Enable()
    AllowEdits = True
    AllowAdditions = True
    AllowDeletions = True
    BtnNew.Enabled = True
    BtnDelete.Enabled = True
    lblEditMode.Caption = "Edit Mode"
End Sub

令我震惊的是,我没有将AllowAdditions 设置为True,但我的标签清楚地正确显示“编辑模式”。

I have a simple Access 2007 database, and I've added a button to the form which toggles the ability to edit the currently displayed record.

By default, I have AllowEdits, AllowAdditions and AllowDeletions set to false. Clicking the 'Edit Mode' button changes this.

I've also added several form events, so that when the displayed record is changed, edit mode is cancelled.

I'm now finding that I'm not able to create new records, as when I click BtnNew (standard Add new record macro attached), I receive the error "You can't go to the specified record".

The VB code I have in the database is as follows, can anyone see what I'm doing wrong, or where to start looking? (My VB/Access knowledge is lacking, but I understand the concepts as I'm a C# developer).

Private Sub BtnEdit_Click()
    If lblEditMode.Caption = "Edit Mode" Then
        Disable
    Else
       Enable
    End If
End Sub

Private Sub Form_AfterDelConfirm(Status As Integer)
    Disable
End Sub

Private Sub Form_Current()
    Disable
End Sub

Private Sub Disable()
    AllowEdits = False
    AllowAdditions = False
    AllowDeletions = False
    BtnNew.Enabled = False
    BtnDelete.Enabled = False
    lblEditMode.Caption = ""
End Sub

Private Sub Enable()
    AllowEdits = True
    AllowAdditions = True
    AllowDeletions = True
    BtnNew.Enabled = True
    BtnDelete.Enabled = True
    lblEditMode.Caption = "Edit Mode"
End Sub

It strikes me that I'm somehow not setting AllowAdditions to True, yet my label clearly displays 'Edit Mode' correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-12-18 19:58:53

您的问题是,当您转到新记录时,Access 通过 Form_Current 将 AllowAdditions 设置为 false,这使得保留在新记录上无效(本质上)。我不确定这是最好的解决方案,但我会设置一个与表单级别相同的状态变量,然后当选择新按钮时,设置该变量并设置 Form_Current 以跳过禁用功能。

Enum ValidStates
    Locked = 0
    Edit = 1
    NewRecord = 2
End Enum

Dim formState As ValidStates

然后对于 btnNew,使用 [事件过程]

Private Sub btnNew_Click()
    On Error GoTo Err_btnNew_Click

    formState = NewRecord
    DoCmd.GotoRecord , , acNewRec

    Exit Sub

Err_btnNew_Click:
    MsgBox Err.Description
End Sub

并在 Form_Current 事件中添加以下内容:

Private Sub Form_Current()
    If formState <> NewRecord Then Disable
    formState = Edit
End Sub

这将允许您编辑新记录。您的启用和禁用功能还将分别添加一行来设置正确的表单状态:

formState = Locked ' for Disable
formState = Edit ' for Enable

Your problem is that when you go to the new record Access sets the AllowAdditions to false via Form_Current which makes staying on the new record invalid (essentially). I'm not sure this is the best solution, but I would set a state variable that is as the form level, then when the new button is selected, set that variable and set the Form_Current to skip the disable function.

Enum ValidStates
    Locked = 0
    Edit = 1
    NewRecord = 2
End Enum

Dim formState As ValidStates

Then for the btnNew, use an [Event Procedure]

Private Sub btnNew_Click()
    On Error GoTo Err_btnNew_Click

    formState = NewRecord
    DoCmd.GotoRecord , , acNewRec

    Exit Sub

Err_btnNew_Click:
    MsgBox Err.Description
End Sub

And in the Form_Current event, add this:

Private Sub Form_Current()
    If formState <> NewRecord Then Disable
    formState = Edit
End Sub

This will allow you to edit the new record. Your Enable and Disable function would also each add a line to set the correct form states:

formState = Locked ' for Disable
formState = Edit ' for Enable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文