防止 Access 2007 中的意外数据覆盖
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是,当您转到新记录时,Access 通过 Form_Current 将 AllowAdditions 设置为 false,这使得保留在新记录上无效(本质上)。我不确定这是最好的解决方案,但我会设置一个与表单级别相同的状态变量,然后当选择新按钮时,设置该变量并设置 Form_Current 以跳过禁用功能。
然后对于 btnNew,使用 [事件过程]
并在 Form_Current 事件中添加以下内容:
这将允许您编辑新记录。您的启用和禁用功能还将分别添加一行来设置正确的表单状态:
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.
Then for the btnNew, use an [Event Procedure]
And in the Form_Current event, add this:
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: