代码之家  ›  专栏  ›  技术社区  ›  Michael T

MS Access表单字段中的拼写检查代码-接受更改时引发错误

  •  4
  • Michael T  · 技术社区  · 12 年前

    我在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

    但这并没有解决问题。

    2 回复  |  直到 12 年前
        1
  •  5
  •   HansUp    12 年前

    此代码用作“退出时”事件(而不是“更新后”事件)。

    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
    
        2
  •  3
  •   Fionnuala    11 年前

    使用与控件关联的更新事件是不起作用的,因为每次更改都会再次触发该事件。你需要一个按钮,或者类似的东西:

    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是否更改。