Avatar of SteveL13
SteveL13
Flag 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

Microsoft Access

Avatar of undefined
Last Comment
SteveL13

8/22/2022 - Mon
mbizup

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.
mbizup

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.
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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SteveL13

ASKER
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."
??
mbizup

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).
SteveL13

ASKER
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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SteveL13

ASKER
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
mbizup

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SteveL13

ASKER
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

SteveL13

ASKER
Sorry.  THREE lines were commented out.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
mbizup

Are you good to go then?
SteveL13

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