Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Spell check entire tabbed form

I'm trying to figure out how to use a command button onclick event to spell check an entire Access tabbed form.  Does anyone have a solution?

I have tried this code but it gives me an error that reads:

"Databasename can't move the focus to the control txtExpirationDate"  The field is set to TabStop = No but I tried TabStop = Yes also and got the same message.

Private Sub cmdSpellCheck_Click()

    Dim i As Integer
    'Turning off warnings to prevent completion message after each control
    ' is checked
    DoCmd.SetWarnings False
    'Loop through each control on the form
    For i = 0 To Me.Count - 1
    'Check to see if the control is a textbox
    If TypeOf Me(i) Is TextBox Then
    'Verify that focus is set to the control
    Me(i).SetFocus
    'Select starting point and select the contents of the control
    Me(i).SelStart = 0
    'Verify that the control contains data and select the contents
    If Len(Me(i)) > 0 Then
    Me(i).SelLength = Len(Me(i))
    'Run the Spell Checker on the contents of the control
    RunCommand acCmdSpelling
    End If
    End If
    Next i
    'Tell user that the check is complete for this record.
    MsgBox "Spell Check is Complete"
    'Turning warnings back on
    DoCmd.SetWarnings True

End Sub

Open in new window

Avatar of mbizup
mbizup
Flag of Kazakhstan image

Are all your controls unlocked, enabled and visible?  If not, you will have to add code to unhide and enable and/or unlock them temporarily before setting focus to them.
Alternatively, if you have hidden or disabled textboxes whose values are set through code and DON'T need to be spell checked you can conditionally exclude them.
Avatar of SteveL13

ASKER

Regarding, "Alternatively, if you have hidden or disabled textboxes whose values are set through code and DON'T need to be spell checked you can conditionally exclude them. " how do I do that?
Now trying this:

Private Sub cmdSpellCheck_Click()

    Dim i As Integer
    Dim ctrl As Control
    DoCmd.SetWarnings False
    For i = 0 To Me.Count - 1
        If TypeOf Me(i) Is TextBox Then
            If ctrl.Tag = "Skip" Then
                ctrl.Locked = False
                Me(i).SetFocus
                Me(i).SelStart = 0
                If Len(Me(i)) > 0 Then
                    Me(i).SelLength = Len(Me(i))
                    RunCommand acCmdSpelling
                    ctrl.Locked = True
                End If
            End If
        End If
    Next i
    MsgBox "Spell Check is Complete"
    DoCmd.SetWarnings True

End Sub

Open in new window


But getting "Object variable or With block variable not set."
??
That's because you haven't set your Object variable.   ;-)

In this case the Object Variable is Ctrl.  

To set it (this needs to happen before you use ctrl):

Set ctrl = Me(i)

Open in new window



For consistency, I'd use ctrl instead of Me(I) throughout this block of code, or use Me(I) instead of ctrl.  My own preference would be to use ctrl (it just looks cleaner).
I must have this thing totally botched up.  Here is what I have now.  Still getting "Object variable or With block variable not set."

Private Sub cmdSpellCheck_Click()

    Dim i As Integer
    Dim ctrl As Control

    DoCmd.SetWarnings False
    For i = 0 To Me.Count - 1

        Set ctrl = ctrl

        If TypeOf ctrl Is TextBox Then
            If ctrl.Tag = "Skip" Then
                ctrl.Locked = False
                ctrl.SetFocus
                ctrl.SelStart = 0
                If Len(ctrl) > 0 Then
                    ctrl.SelLength = Len(ctrl)
                    RunCommand acCmdSpelling
                    ctrl.Locked = True
                End If
            End If
        End If
    Next i
    MsgBox "Spell Check is Complete"
    DoCmd.SetWarnings True

End Sub

Open in new window

Ok.  Can't say I haven't tried.  Here's where I'm at.  But I intentionally misspelled a word and it didn't catch it.  I have set the tag to Skip in several fields but not the one I misspelled.


    Dim ctrl As Control
    Dim frm As Form

    Set frm = Screen.ActiveForm

    DoCmd.SetWarnings False

    For Each ctrl In frm.Controls

        If TypeOf ctrl Is TextBox Then

            If ctrl.Tag = "Skip" Then

                If Len(ctrl) > 0 Then
                    With ctrl
                        .SetFocus
                        .SelStart = 0
                        .SelLength = Len(ctrl)
                    End With
                    DoCmd.RunCommand acCmdSpelling
                End If

            End If
            
        End If

    Next

    DoCmd.SetWarnings True
    
    MsgBox "This estimate has been spell checked."

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is what  I ended up with so far.  Please note that I had to comment two of the lines out to make it work.

    Dim ctrl As Control
    Dim frm As Form
    Dim frmtab As TabControl
  
    Set frm = Screen.ActiveForm

    DoCmd.SetWarnings False

    For Each ctrl In frm.Controls
       If TypeOf ctrl Is TextBox Then
            If ctrl.Tag <> "Skip" Then
                If Len(ctrl) > 0 Then
                    With ctrl
                        Debug.Print .Name
                        intCount = intCount + 1
'                        .SetFocus
'                        .SelStart = 0
'                        .SelLength = Len(ctrl)
                    End With
                    DoCmd.RunCommand acCmdSpelling
                End If
            End If
        End If
    Next

    DoCmd.SetWarnings True
    MsgBox "This estimate has been spell checked."

Open in new window

Sorry.  THREE lines were commented out.
Are you good to go then?
Yes.  It seems to be working correctly.  I will award points to you in a few minutes.