SetFocus and SpellCheck

J

J.P.

I'm having a problem with spell checking a textbox field using
acCmdSpellCheck. Here is the code:

Me.myTextBox.SetFocus
Me.myTextBox.SelStart = 0 ' bombs here, says the control needs focus
Me.myTextBox.SelLength = Len(Me.myTextBox)
DoCmd.RunCommand acCmdSpellCheck

If I insert:

MsgBox Screen.ActiveControl.Name

directly after:

Me.myTextBox.SetFocus

The control does indeed have focus, but when I try to set the SelStart
property the code bombs complaining that the control does not have
focus. This code is being executed in the click event for a button on
the form.

Any ideas on what may be going wrong would be greatly appreciated.

Thanks,
J.P.
 
J

J.P.

Here is the actual code being run up to the spellcheck:

Dim rst As Recordset
Dim rst1 As Recordset
Dim dteEventDate As Date

If (IsNull(Me.cboClientID)) Then
strMsgAnswer = MsgBox("You must choose a client.", vbOKOnly,
conAppName)
Me.cboClientID.SetFocus
ElseIf (IsNull(Me.cboState)) Then
strMsgAnswer = MsgBox("You must choose a state.", vbOKOnly,
conAppName)
Me.cboState.SetFocus
ElseIf IsNull(Me.txtEventDate) Then
strMsgAnswer = MsgBox("You Must enter the Event's Date.",
vbOKOnly, conAppName)
Me.txtEventDate.SetFocus
ElseIf ((IsNull(Me.cboCaseName)) And (Me.cboTrialGroup.Visible =
False)) Then 'The CaseName and Number are showing
strMsgAnswer = MsgBox("You must choose a Case", vbOKOnly,
conAppName)
Me.cboCaseName.SetFocus
ElseIf ((IsNull(Me.cboTrialGroup)) And (Me.cboTrialGroup.Visible =
True)) Then 'This is a Trial Group entry
strMsgAnswer = MsgBox("You must choose a Trial Group.", vbOKOnly,
conAppName)
Me.cboTrialGroup.SetFocus
ElseIf Me.txtEvent = Null Then
strMsgAnswer = MsgBox("You must enter the Event Description.",
vbOKOnly, conAppName)
Me.txtEvent.SetFocus
ElseIf IsNull(Me.txtEventID) Then 'this is a new entry

'Check the spelling in the Event
Me.txtEvent.SetFocus
Me.txtEvent.SelStart = 0
Me.txtEvent.SelLength = Len(Me.txtEvent)
DoCmd.RunCommand (acCmdSpelling)
 
D

Douglas J. Steele

While I know it doesn't address your question,

ElseIf Me.txtEvent = Null Then

should be

ElseIf IsNull(Me.txtEvent) Then

It's possible that might solve your problem, though, since your present code
would be trying to set SelStart even if txtEvent contains Null.
 
J

J.P.

Thanks for pointing that out, I've made the change in the code
although the problem still persists.
 
J

J.P.

I've also tried commenting all of the code up to the spell check
routine and change the ElseIf line above where it starts to just If
True Then, again no change in the behavior. I've also checked the text
control's contents before SelStart is assigned to ensure it's picking
up any data at all and it does infact pick up the text in the control.
This same code works great elsewhere so I think it has something to do
with the form itself. Needless to say, this is a bad bug to get stuck
with since I know the code _should_ work based on past projects.
 
J

J.P.

Yeah, tried rebuilding it from scratch. Figure it must be a form
property maybe causing the problem? It's also a form embedded in a tab
control, but there are other forms that do work with the spellcheck
embedded in the same tab control.

I tried another approach, in the GotFocus event handler for txtEvent I
tried the following code:

Private Sub txtEvent_GotFocus()
If bSpellCheck = True Then
With txtEvent
.SelStart = 0
.SelLength = Len(txtEvent)
End With
DoCmd.RunCommand acCmdSpelling
End If
End Sub

but it bombs out on the same piece of code as before, the SelStart
property, even if the control has text in it and indeed does have
focus, I mean the code is executing in the event fired when that
control gains focus so one would think it must have focus.
 
J

J.P.

As a side note, it does this with any text control on the form, not
just this one. I've setup test text control's on the form to confirm
the behavior exists for any text control.
 
M

missinglinq via AccessMonster.com

Really don't see why your code shouldn't work, but try plugging this in
instead and see if it runs:

With Me!txtEvent
Me.txtEvent.SetFocus
If Len(.Value) > 0 Then
DoCmd.SetWarnings False
.SelStart = 1
.SelLength = Len(.Value)
DoCmd.RunCommand acCmdSpelling
.SelLength = 0
DoCmd.SetWarnings True
End If
End With
 
J

J.P.

No go, thanks for the help and time guys. I'm just gonna scratch this
and start over again testing the spell check each time I make a change
to the form and see if I can't figure out where and what exactly I'm
doing to this form that's causing it to break.
 
J

J.P.

Well, I've figured out what is going wrong after 2 days of messing
with this. The problem goes back to the forms RecordSource.

The old query was this:

SELECT
Calendars.CalendarID,
Calendars.Reminder,
Calendars.DueDate,
Calendars.Rem1Week,
Calendars.Rem2Week,
Calendars.Rem30Day,
Calendars.Rem60Day,
Clients.ClientCode,
Calendars.State,
Cases.CaseName,
Calendars.EventDate,
Calendars.Time,
Calendars.Event,
Calendars.Remarks,
Calendars.CaseID,
Calendars.ClientID
FROM (Calendars INNER JOIN Cases ON Calendars.CaseID=Cases.CaseID)
INNER JOIN Clients

If I remove that from the RecordSource for the form, spellcheck works
like a charm but of course it breaks the functionality of the form. So
I tried this query which is close to the same thing:

SELECT
Calendars.CalendarID,
Calendars.Reminder,
Calendars.DueDate,
Calendars.Rem1Week,
Calendars.Rem2Week,
Calendars.Rem30Day,
Calendars.Rem60Day,
Clients.ClientCode,
Calendars.State,
Cases.CaseName,
Calendars.EventDate,
Calendars.Time,
Calendars.Event,
Calendars.Remarks,
Calendars.CaseID,
Calendars.ClientID
FROM
Calendars,
Cases,
Clients,
WHERE Cases.CaseID = Calendars.CaseID AND
Clients.ClientID = Calendars.ClientID


With this query I get an entirely different error message, errno 2046
The command or function 'Spelling' isn't available now. When I got
this message I was able to dig through the help pages for problems
that may cause this error. From what I've gathered, the problem is
because the query is hitting 3 tables (straight from the docs: Query
based on three or more tables in which there is a many-to-one-to-many
relationship). This problem shows up in the "Data can't be updated"
section of the documentation. Okay, I think understand the problem but
the kicker is, the field I'm trying to spell check is not bound to any
field in this query. The only controls on the form that are bound to
fields from this query are controls on the continuous section of the
form "Detail" and they can not be edited directly, deleted directly or
have new records inserted directly from that section of the form. I
don't really know what to do from here except come up with a hack that
does the spell check on an entirely separate form because that query
or one that produces that same results is not an optional piece of
functionality on the form, it has to be there. I'm reaching here, but
maybe, just maybe, there is a property that can be set to stop this
behavior on this particular form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top