[英]Spell Check code in MS Access form field - throws error when change is accepted
我在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
当用户退出该字段时,会运行拼写检查。 它部分有效。 它会打开拼写检查对话框,并找到第一个错误。 问题是,当您单击忽略,更改等以处理/修复拼写错误时,代码将失败并显示以下错误框:
“为此字段设置为BeforeUpdate或ValidationRule属性的宏或函数阻止Microsoft Office Access在字段中保存数据。”
我尝试在拼写检查代码之前添加记录保存代码:
DoCmd.DoMenuItem acFormBar,acRecordsMenu,acSaveRecord,acMenuVer70 DoCmd.DoMenuItem acFormBar,acRecordsMenu,5,acMenuVer70
但这并没有解决它。
此代码用作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
使用与控件关联的更新事件不起作用,因为每次更改都会再次触发事件。 你需要一个按钮,或者像这样:
Private Sub Spell_Click()
With Me!txtComments
.SetFocus
.SelStart = 0
.SelLength = Len(Me!txtComments)
End With
DoCmd.RunCommand acCmdSpelling
End Sub
通过添加一行可以避免On Exit事件的一些问题:
If Me.txtComments.Value <> Me.txtComments.OldValue Then
With Me!txtComments
.SetFocus
.SelStart = 0
.SelLength = Len(Me!txtComments)
End With
<...>
至少这只会在您通过控件直到保存记录时运行,而不是每次都会运行txtComments是否更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.