Spell check individual control

I would like to run spell check on a few controls (using A2013 runtime) and I don't want the user to have to push a button to make this happen because they just forget.  So, I added the code to call the spell check to the on Exit event (that is the only event that works.  Both Before and After update raise errors and Change isn't even a consideration.) of the controls I want to spellcheck.  Below is the suggested code.  I commented out a few lines that were causing a problem but it still creates a never ending loop.  It seems to make Access forget where it was going when the LostFocus event was interrupted.  The other piece of code from the same website that checks all the controls on a form works fine.  But for this form, I have tab controls and those are interfering with the spell checker when it tries to set focus to a control.

I tried a couple of things such as comparing the .value property to the .oldvalue property.  That works to avoid the spell check when the user is just tabbing through the controls without changing anything but once something is changed, the values are different so the spell checker keeps running.

It would be nice if there were some property that told us where Access thought it was going next so the spell check could just move the focus there but I don't know of any.
Private Sub cmdSpell_Click()
   Dim ctlSpell As Control

   Set ctlSpell = Screen.PreviousControl
   If TypeOf ctlSpell Is TextBox Then
     If IsNull(Len(ctlSpell)) Or Len(ctlSpell) = 0 Then
       ''''MsgBox "There is nothing to spell check."
       ''''ctlSpell.SetFocus
       Exit Sub
     End If
     With ctlSpell
       .SetFocus
       .SelStart = 0
       .SelLength = Len(ctlSpell)
     End With
     DoCmd.RunCommand acCmdSpelling
   Else
     MsgBox "Spell check is not available for this item."
   End If
   '''ctlSpell.SetFocus
 End Sub

Open in new window

LVL 42
PatHartmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tony HungateDirector of TrainingCommented:
I came across a similar issue a while back, I found this post and tweaked to meet my needs.  Perhaps you can do the same for your specific need.

Here is the post solution I utilized is about 2/3 of the way down:
http://bytes.com/topic/access/answers/952205-vba-run-custom-spell-checker-log-errors-table

I used this in an older DB to meet a similar function, may also help in your way ahead.

Public Function SpellChecker(Calling As Form)

 Dim ctlSpell As Control
 Dim Incoming As String, Outgoing As String

 DoCmd.SetWarnings False
 Set ctlSpell = Calling.ActiveControl
 If (ctlSpell.Locked) Then
 Line1 = "Cannot spell check. Field is read-only"
 mbResult = MessageBox("OK", "", "", Line1)
 Else
 If Len(ctlSpell) > 0 Then
 Incoming = ctlSpell
 With ctlSpell
 .SetFocus
 .SelStart = 0
 .SelLength = Len(ctlSpell)
 End With
 DoCmd.RunCommand acCmdSpelling
 Outgoing = ctlSpell
 End If
 ' See if any changes were made 09/29/04
 If (Incoming <> Outgoing) Then
 ' Notify user that changes
 ' were made, if you want to,
 ' or give Bronx cheer
 End If
 End If

 End Function

Open in new window


I will check back as well, please update as you find a solution.

~:TLH:~
0
PatHartmanAuthor Commented:
Here's what I finally got to work.  Calling the spell checker seems to make Access loose track of what you are doing and it ends up in a loop.  On this particular subform, I had tried the code I originally posted and I called the spell checker from the BeforeUpdate() event of the form as the last line of code.  I wanted to make sure that all the form and control level edits were completed before I got into fixing spelling errors.  But even though the record got saved, the BeforeUpdate event never completed and the AfterUpdate event didn't run.  So, the spell checker is interfering with the event model in Access.

Prompted by TLH's suggestion I went back to the field by field approach which I had abandoned earlier for other reasons and finally figured out a solution that seems to work.  I call the spell checker from the control's LostFocus event but ONLY if the field was updated.  Then the next statement moves focus to the next logical control.  The trick comes at the end.  The last control in the set has a GoToRecord to move the record pointer to a new record.  Without that, you end up not being able to get out of the subform.

Private Sub txtNeeds_LostFocus()
    If Me.txtNeeds & "" <> Me.txtNeeds.OldValue & "" Then     'only spell check if dirty
        Call SpellChecker(Me)
        Me.txtCHCPService.SetFocus
    End If
End Sub
Private Sub txtCHCPService_LostFocus()
    If Me.txtCHCPService & "" <> Me.txtCHCPService.OldValue & "" Then     'only spell check if dirty
        Call SpellChecker(Me)
        Me.txtMeasuredOutcome.SetFocus
    End If
End Sub
Private Sub txtMeasuredOutcome_LostFocus()
    If Me.txtMeasuredOutcome & "" <> Me.txtMeasuredOutcome.OldValue & "" Then     'only spell check if dirty
        Call SpellChecker(Me)
        DoCmd.GoToRecord , "", acNext
    End If
End Sub

Public Function SpellChecker(Calling As Form)

 Dim ctlSpell As Control

  DoCmd.RunMacro "mWarningsOff"
 Set ctlSpell = Calling.ActiveControl
 If (ctlSpell.Locked) Then
    ''Ignore
 Else
    If Len(ctlSpell) > 0 Then
        With ctlSpell
        .SetFocus
        .SelStart = 0
        .SelLength = Len(ctlSpell)
        End With
        DoCmd.RunCommand acCmdSpelling
    End If
End If
 DoCmd.RunMacro "mWarningsOn"
End Function

Open in new window


Note that in TLH's original code he set warnings Off but never set them back on.  This is extremely dangerous as anyone who has been burned by having warnings off will attest.  Warnings off not only stops the annoying messages you don't want your user to see, it also stops the "are you sure you want to close without saving" message that you most certainly want to see if you modify a form and than close it without explicitly saving.

To avoid this issue in my applications, I never just turn warnings off.  I created two macros (the ONLY macros in my applications) .  In addition to turning the warnings off, the macro turns the hourglass on and vice versa.  That way if I inadvertently forget to turn the warnings back on, I have a visible clue that something is amiss.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PatHartmanAuthor Commented:
Although I used some of the code from the assisted solution, it didn't actually solve the problem which was getting out of the loop that the spell checker was causing.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.