Question : Spell Check Single Field

I found the following function:

Function SpellCheck_Field()

' Put the call to the function in the "OnExit"
' OR "LostFocus" event for the required field
' as:    =SpellCheck_Field()
'
' Do NOT put it in the "AfterUpdate" event

' ----------------------------------------------------------------------
' Requires the following files:

    ' Spelling checker      (Msp*.dll, Mssp*.dll, and Mssp*.lex files)
    ' Hyphenator            (Mshy*.dll and Mshy*.lex files)
    ' Thesaurus             (Msth*.dll and Msth*.lex files)
    ' Grammar dictionary    (Msgr*.lex files)

' to exist in: Program Files\Common Files\Microsoft Shared\Proof folder (which they should)

' ----------------------------------------------------------------------

    Dim L As Integer
    L = Nz(Len(Screen.ActiveControl), 0)
    If L > 0 Then
        With Screen.ActiveControl
            .SetFocus
            .SelStart = 0
            .SelLength = L
        End With
        DoCmd.SetWarnings False
        DoCmd.RunCommand acCmdSpelling
        DoCmd.SetWarnings True
    End If
   
End Function


It works in one spot and doesn't in another.

I have a form (frmInspection) that has several tab pages on it.  One tab page has a subform with several fields on it.  On one of the fields (memo) I put the following in the On Exit Event of of the field:  =SpellCheck_Field()

Words like a charm.

On another tab page I have a single memo field and I put the same =SpellCheck_Field().  It spell checks alright, but doesn't allow you to ever loose focus of the field.  When you try to view the form in design view (the only way to get out of the form) it gives me the following error:

Run-time error '2474'
The expression you entered requires the control be the active window.

When I press Debug the following line of code is highlighted yellow in the function I pasted above:

    L = Nz(Len(Screen.ActiveControl), 0)

What do I need to do to make this work?  There are only a few fields that I want Spell Checked and I only want the spell checked on exit of the field.

Hope I haven't confused you too much,
Lena

By the way this will be used in Access 97 (although I am working on Access 2000 at home)

Answer : Spell Check Single Field

Here is a modified version,

I called the following module as fSpellChek:

Public Function mySpellingChecker(CheckWhat) As String
   
    Dim X As Object
    Temptxt = CheckWhat 'we need this if Close was selected
   
    Set X = CreateObject("Word.Application")
    X.Visible = False
    X.Documents.Add
    X.Selection.Text = CheckWhat.Text
    X.ActiveDocument.CheckSpelling
    CheckWhat.Text = X.Selection.Text
    GotIt = CheckWhat.Text
    X.ActiveDocument.Close savechanges:=wdDoNotSaveChanges
    X.Quit
    Set X = Nothing
   
    If IsNull(CheckWhat) Then
        mySpellingChecker = Temptxt
    Else
        mySpellingChecker = GotIt
    End If

End Function



in the Form, I used On Lost Focus, and this what should go there:

Private Sub Memo1_LostFocus()
    DoCmd.Save
    [Memo1] = mySpellingChecker([Memo1])
End Sub


References required, in order:
Visual Basic for applications,
Microsoft Access X.X object Library
OLE automation
Microsoft ActiveX Data objects 2.1 Library.


I will try the On Exit to check all the controls in the Form and SubForm, all in 1 go, but I don't know how successfull I will be,
If successfull, then I will post it here too.

jaffer
Random Solutions  
 
programming4us programming4us