Spell Check different fields

  • Thread starter Thread starter Creeater
  • Start date Start date
C

Creeater

Guest
I have the following code that when I click the Spell Check button it checks the Summary_Incident field. What I would like is where ever the cursor is at the time, if I click the Spell Check button, it will check that field. Please note this is the only code that I could get to work that would check the selected record only. Previous codes wanted to spell check all records. Thanks.

Private Sub SpellCheck_Click()
With Me.Summary_Incident
.SetFocus
.SelStart = 0
.SelLength = Len(Me.Summary_Incident)
End With
DoCmd.RunCommand acCmdSpelling
End Sub
 
Hmmm, Thats a good question...
I'll just have a wack at it. If it were me I would probably keep track of what control has the focus in variable and when the button is hit set focus to the last one that had the focus and run your code on it...

At the top put
Code:
Public SelectedControl As String

And on each control that is possibly one that can be checked for spelling put

Code:
Private Sub TextBox1_GotFocus()
SelectedControl = "TextBox1"
End Sub

And then on the click event of your spell check button you can just change the setfocus line to
Code:
Me.Controls(SelectedControl).SetFocus

But that seems like alot of work if you have a bunch of possible fields to be checked. Maybe someone else has a better way? This does work though...
 
Code:
Private Sub SpellCheck_Click()

   With Screen.PreviousControl
      .SetFocus
      .SelStart = 0
      .SelLength = Len(Screen.PreviousControl.Name)
   
      DoCmd.RunCommand acCmdSpelling
      .SelLength = 0
   End With

End Sub
 
Jon, your code works except for one problem. Say I type a sentence in a field, spell check it, then go back to type a second sentence in that field it doesn't spell check the second sentence, just the first. Solution??? Thanks for your help.
 
I think declaring a control variable should work.
Code:
Private Sub SpellCheck_Click()
   
   Dim ctl As Control
   
   Set ctl = Screen.PreviousControl
   With ctl
      .SetFocus
      .SelStart = 0
      .SelLength = Len(ctl)
   
      DoCmd.RunCommand acCmdSpelling
      .SelLength = 0
   End With
   
   Set ctl = Nothing
End Sub
 

Users who are viewing this thread

Back
Top Bottom