写冲突 - 即使与我.dirty
在我正在编写的应用程序中,当我使用 VBA 代码更改表单上复选框的值时,出现写入冲突。 我尝试将以下代码插入到使 VBA 更改表单上任何对象的值的事件中:
If Me.Dirty Then
Me.Dirty = False
End If
但问题仍然存在 - 就好像在我希望通过手动操作更改任何内容之前未保存记录一样。
以下是我遇到此问题的表单中的完整代码的一些示例:
更改“空白定价”复选框时的代码:
Private Sub chkSupAllowBlankPrice_AfterUpdate()
If Me.Dirty Then
Me.Dirty = False
End If
If (chkSupAllowBlankPrice.Value = True) Then
chkSupRequirePrice.Value = False
End If
End Sub
更改“需要定价”复选框时的代码:
Private Sub chkSupRequirePrice_AfterUpdate()
If Me.Dirty Then
Me.Dirty = False
End If
If (chkSupRequirePrice.Value = True) Then
chkSupAllowBlankPrice.Value = False
chkSupAllowBlankPrice.Visible = False
chkSupAllowBlankPrice.Enabled = False
chkSupAllowBlankPrice.Locked = True
lblSupAllowBlankPrice.Visible = False
Else
chkSupAllowBlankPrice.Visible = True
chkSupAllowBlankPrice.Enabled = True
chkSupAllowBlankPrice.Locked = False
lblSupAllowBlankPrice.Visible = True
End If
End Sub
不确定它是否有帮助,但这些表存储在 SQL Server Express 数据库中 - 因此有了标签。
-- 编辑于 05/29/2009 @ 1201 小时 --
我尝试注释掉所有对象值更改,只保留可见、锁定和启用的更改 - 但我不断遇到写入冲突。 我尝试将 Me.Dirty = False 放在事件过程的末尾,甚至尝试将其删除。 到目前为止,每当我更改“要求定价”或“允许空白定价”,而 VBA 代码未更改其他值时,都会遇到写入冲突。
-- 编辑于 05/29/2009 @ 1318 小时 --
一旦创建记录,这些复选框操作的字段将不会接受任何更改,每当我尝试使用它们时都会生成写入冲突。 现在我很困惑,想把一切都扔掉然后重新开始。
-- 编辑于 06/01/2009 @ 1209 小时 --
经过调查,似乎在服务器上为所涉及的表定义了许多检查约束,但我无法删除它们。 有些东西导致链接表总是报告脏数据,即使项目没有更改 - 我认为 Access 和 SQL 正在使用默认值来解决这个问题。 我将擦除所有表,删除所有信息,然后重新开始我的设计,因为无论如何,如果不删除表,检查约束似乎都无法删除。 谢谢大家的帮助,这个问题可以关闭吗 - 将问题提交给这个新问题(不允许更新记录 - 写入冲突)?
-- 编辑于 06/03/2009 @ 1307 小时 --
从其他问题交叉发布,下面描述了所有好奇的解决方案。 感谢所有和我一起努力走到这一步的人,我真的很感谢你们的帮助。 我发现使用“是/否”复选框和带有 Access 的 SQL Server 时会出现一个奇怪的问题。 显然,Access 会将 NULL 解释为 No - 更改值,但 SQL Server 不会将 NULL 解释为位字段中的“否”(“是/否”在转换中变成“否”),因此当值不正确时,它会引发写入冲突错误必填,且为 NULL。 解决方案是重新设计表格,以便需要一个值,并且为每个以前的“是/否”复选框分配一个默认值。 这解决了神秘的写入冲突消息,并允许在创建记录后对其进行更改。
In an application that I am writing, I am getting Write Conflicts when I use VBA code to change the value of a checkbox on a form. I have tried inserting the following code into the events that have VBA changing the values of any object on the form:
If Me.Dirty Then
Me.Dirty = False
End If
But the problem persists - as if the record is not being saved before I look to change anything through manual manipulation.
Here is some examples of the full code from the form where I am encountering this problem:
Code for when Blank Pricing checkbox is changed:
Private Sub chkSupAllowBlankPrice_AfterUpdate()
If Me.Dirty Then
Me.Dirty = False
End If
If (chkSupAllowBlankPrice.Value = True) Then
chkSupRequirePrice.Value = False
End If
End Sub
Code for when Require Pricing checkbox is changed:
Private Sub chkSupRequirePrice_AfterUpdate()
If Me.Dirty Then
Me.Dirty = False
End If
If (chkSupRequirePrice.Value = True) Then
chkSupAllowBlankPrice.Value = False
chkSupAllowBlankPrice.Visible = False
chkSupAllowBlankPrice.Enabled = False
chkSupAllowBlankPrice.Locked = True
lblSupAllowBlankPrice.Visible = False
Else
chkSupAllowBlankPrice.Visible = True
chkSupAllowBlankPrice.Enabled = True
chkSupAllowBlankPrice.Locked = False
lblSupAllowBlankPrice.Visible = True
End If
End Sub
Not sure if it helps, but the tables are stored in a SQL Server express database - hence the tag.
-- Edited 05/29/2009 @ 1201 hours --
I have tried commenting out all object value changes, leaving in only visible, locked, and enabled changes - but I keep getting write conflicts. I tried putting the Me.Dirty = False at the end of the event procedure, and I even tried to remove it. So far, I am getting write conflicts whenever I change Require Pricing or Allow Blank Pricing, without the other value being changed by VBA code.
-- Edited 05/29/2009 @ 1318 hours --
The fields that these checkboxes manipulate -will not- accept any changes once the record is created, generating a Write Conflict any time I attempt to work with them. Now I am totally confused, thinking of gutting everything and starting over.
-- Edited 06/01/2009 @ 1209 hours --
After investigation, it seems there is a number of check constraints defined on the server for the tables involved which I cannot remove. Something is causing the linked tables to always report dirty even when items have not been changed - and I think Access and SQL are fighting it out with default values. I am going to wipe all tables, delete all information, and start over with my design, as the check constraints don't appear to be removable without dropping the table anyway. Thank you everyone for the help, could this question be closed - referring the problem to this new question (Updates to Records not allowed - Write Conflict)?
-- Edited 06/03/2009 @ 1307 hours --
Cross-posted from other question, solution described below for all that were curious. Thank you to all the people who banged their heads with me to get this far, I really aprpeciate the help. I found out that a strange problem that crops up when using Yes/No checkboxes and SQL Server with Access. Apparently, Access will interpret NULL as No - changing the value, but SQL Server will not interpret NULL as a No in a Bit field (what Yes/No gets turned into in conversion) so it throws a Write Conflict error when a value is not required, and is NULL. The solution was to redesign the table so that a value was required, and that there was a default value assigned for EVERY former Yes/No checkbox. This solved the mysterious Write Conflict messages, and allowed changes to records once they were created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些评论:
在更改其他字段的值(这将使表单再次变脏)之前保存表单数据(设置
Dirty=false
)是否有任何特殊原因?我会首先更改其他字段的数据然后保存表单的数据,否则表单总是脏。
从代码中更改控件的值不会调用其事件,因此从
chkSupAllowBlankPrice_AfterUpdate()
更改chkSupRequirePrice
的值不会调用chkSupRequirePrice_AfterUpdate()
.与其将特定代码放入每个事件中,不如将所有内容重新组合到同一个中,例如从每个事件处理程序调用的
Update()
子函数。这将使您更容易管理代码及其副作用,因为它是始终被调用的同一段代码。
话虽如此,您的代码应该可以工作,所以我猜测问题来自代码中的其他地方。
如果已在其他地方打开相同的记录进行编辑,则会发生写入冲突。
您可以尝试一下锁定类型,看看它是否会改变任何内容。
要查看问题是否确实与您的 SQL Server 设置有关,请尝试取消链接表并将其复制到本地 Access 数据库中,看看在处理本地 Access 表时是否仍然遇到此问题。< /p>
A few comments:
is there any particular reason you save the form's data (set
Dirty=false
) before changing the other field's value (which will make the form dirty again)?I would first change the other field's data then save the form's data, otherwise the form is always dirty.
changing control's values from code does not call their events, so changing
chkSupRequirePrice
's value fromchkSupAllowBlankPrice_AfterUpdate()
will not callchkSupRequirePrice_AfterUpdate()
.Instead of putting specific code into each event, it's better to regroup everything into the same, say,
Update()
sub called from each event handler.It will make it easier to manage you code and its side effects because it's the same piece of code that's called all the time.
having said that, your code should work, so I'm guessing the issue comes from somewhere else in your code.
Write conflicts happen if the same record is already opened for editing elsewhere.
You can play around with the type of locking and see if it changes anything.
to see if the issue is really related to your SQL Server setup, try to unlink the table and instead copy it into your local Access database to see if you're still getting this issue when dealing with a local Access table.
过去,如果 SQL Server 链接表没有主键,我在 Access 中会遇到一些奇怪的问题。 如果您的表没有主键,则它不知道要更新哪条记录。 我知道我曾经收到过类似的消息,但那是几年前的事了,所以我不知道这是否是您的问题。
IN the past, I've had some wierd problems in Access occur if the SQL Server linked table did not have a primary key. If your table doesn't have a primary key, it doesn't know which record to update. I know I got a similar message for this once but it was years ago, so I don't know if this is your problem.
Access 处理是/否复选框值的方式与 SQL Server 之间存在差异。 将“是/否”布尔值从 Access 转换为 SQL Server 时,必须记住定义默认状态并将其标记为需要答案。 否则,每次都会遇到写入冲突,并且一旦设置了初始值,就会阻止记录与更改一起保存。
There is a difference between how Access handles Yes/No checkbox values and SQL Server. When translating Yes/No booleans from Access to SQL Server, you must remember to define a default state as well as mark it as requiring an answer. Otherwise, you will get write conflicts every time, and it will prevent the record from being saved with your changes once the initial values have been set.