vba MS Access 表单字段中的拼写检查代码 - 接受更改时抛出错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11784221/
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
Spell Check code in MS Access form field - throws error when change is accepted
提问by Michael T
I added the following code in the AfterUpdate event of a textbox in an MS Access form:
我在 MS Access 表单中的文本框的 AfterUpdate 事件中添加了以下代码:
Private Sub txtComments_AfterUpdate()
With Me!txtComments
.SetFocus
If Len(.Value) > 0 Then
DoCmd.SetWarnings False
.SelStart = 1
.SelLength = Len(.Value)
DoCmd.RunCommand acCmdSpelling
.SelLength = 0
DoCmd.SetWarnings True
End If
End With
End Sub
This runs a spell check when the user exits the field. It partially works. It opens the spell check dialogue, and locates the first error. The problem is, when you click Ignore, Change, etc to handle/repair the spelling error, the code fails and the following error box appears:
这会在用户退出该字段时运行拼写检查。它部分有效。它打开拼写检查对话框,并定位第一个错误。问题是,当您单击忽略、更改等来处理/修复拼写错误时,代码失败并出现以下错误框:
"The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Office Access from saving the data in the field."
“为此字段设置为 BeforeUpdate 或 ValidationRule 属性的宏或函数阻止 Microsoft Office Access 保存该字段中的数据。”
I tried adding record-saving code before the spell check code:
我尝试在拼写检查代码之前添加记录保存代码:
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70 DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70 DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
but this didn't solve it.
但这并没有解决它。
回答by HansUp
This code works as the On Exit event (instead of After Update).
此代码用作 On Exit 事件(而不是 After Update)。
Private Sub txtComments_Exit(Cancel As Integer)
With Me!txtComments
.SetFocus
If Len(.value) > 0 Then
.SelStart = 1
.SelLength = Len(.value)
DoCmd.RunCommand acCmdSpelling
.SelLength = 0
End If
End With
End Sub
回答by Fionnuala
Using an update event associated with the control is not going to work, because each change triggers the event again. You need a button, or such like:
使用与控件关联的更新事件是行不通的,因为每次更改都会再次触发该事件。你需要一个按钮,或者像这样:
Private Sub Spell_Click()
With Me!txtComments
.SetFocus
.SelStart = 0
.SelLength = Len(Me!txtComments)
End With
DoCmd.RunCommand acCmdSpelling
End Sub
It would be possible to avoid some of the problems with the On Exit event by the addition of a line:
通过添加一行,可以避免 On Exit 事件的一些问题:
If Me.txtComments.Value <> Me.txtComments.OldValue Then
With Me!txtComments
.SetFocus
.SelStart = 0
.SelLength = Len(Me!txtComments)
End With
<...>
At least this will only run when you pass through the control until the record is saved, not every time, whether txtComments is changed or not.
至少这只会在您通过控件时运行,直到保存记录为止,而不是每次都运行,无论 txtComments 是否更改。