Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spell Check code in MS Access form field - throws error when change is accepted

I added the following code in the AfterUpdate event of a textbox in an MS Access form:

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."

I tried adding record-saving code before the spell check code:

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70 DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70

but this didn't solve it.

like image 388
Michael T Avatar asked Feb 20 '23 16:02

Michael T


1 Answers

This code works as the On Exit event (instead of 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
like image 118
HansUp Avatar answered Apr 05 '23 23:04

HansUp