We help IT Professionals succeed at work.

Spell check entire tabbed form

SteveL13
SteveL13 asked
on
82 Views
Last Modified: 2018-09-19
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

CERTIFIED EXPERT
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.
CERTIFIED EXPERT
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?

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."
??
CERTIFIED EXPERT
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

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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.
CERTIFIED EXPERT
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions