Spell check entire tabbed form

SteveL13
SteveL13 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
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.
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
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.

Author

Commented:
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?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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."
??
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
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).

Author

Commented:
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

Author

Commented:
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

Nerd
Most Valuable Expert 2012
Top Expert 2013
Commented:
You're very close...

The only problem is that your "If" block is only running the spell check when the tag property is "Skip", so the logic and functionality is opposite of what it should be.


You just need to change the If statement so that the check runs if the tag property is NOT "Skip", like this:

            If ctrl.Tag <> "Skip" Then
                  'etc
 

Open in new window



(I made that single change to your code, and it seemed to work as expected.)

Author

Commented:
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

Author

Commented:
Sorry.  THREE lines were commented out.
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
Are you good to go then?

Author

Commented:
Yes.  It seems to be working correctly.  I will award points to you in a few minutes.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial