Solved

Spell check individual control

Posted on 2014-11-06
3
407 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 35

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 35

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

803 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