Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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:

    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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top