Solved

Spell check individual control

Posted on 2014-11-06
3
365 Views
Last Modified: 2014-12-09
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

0
Comment
Question by:PatHartman
  • 2
3 Comments
 
LVL 10

Assisted Solution

by:t_hungate
t_hungate earned 250 total points
ID: 40455024
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
 
LVL 34

Accepted Solution

by:
PatHartman earned 0 total points
ID: 40481577
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
 
LVL 34

Author Closing Comment

by:PatHartman
ID: 40488416
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

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now