Setfocus prevents SelStart = 1, field still highlighted

bignose2

Registered User.
Local time
Today, 06:10
Joined
May 2, 2010
Messages
248
Must be missing something here & driving me crazy.

Moving from Field 1 to field 2, I don't want the whole textbox highlighted

Private Sub Field1_Exit(Cancel As Integer)
Field2.SetFocus
End Sub

Private Sub Field2_GotFocus()
Field2.SelStart = 1
End Sub

If I simply tab from Field1 to Field2 it is fine, cursor moves to 2nd character and no highlighting.

If I jump to Field2 using setfocus from any other field it highlights the whole field.

Tried everything I can think of
Tried in on exit & every combination of different event triggering
Varying SelLength etc.

This is a test database & only these 2 fields.
I don't want to modify the core behaviour of the database.

This is the example I see everywhere & how to prevent highlighting & just does not work for me.

Private Sub Field1_Exit(Cancel As Integer)
With Me.Field2
.SetFocus
.SelStart = 1
.SelLength = 0 ' Nothing selected
End With

End Sub

Private Sub Field2_GotFocus()

' nothing

End Sub

If I mouse click on field2, it jumps to the 2nd character (SelStart 1) but of course nothing selected, I assume because of the mouse click

What am I missing?
Thanks I/A
 
try the access settings:
access options,
advanced,
behavior entering field,
set to : Go To Start Of Field
 
As I say, I don't want to modify the core behaviour of the database.

Big database and not sure how it would effect behaviour throughout, would mean hours of testing, most of the time I do want it selected.

I figured this should be able to work around

Thanks anyway
 
When you use VBA to do anything on a form the other events don't fire. So by setting focus using code, the got focus event you have programmed will be ignored.

You would need to copy your code to the calling previous event to get it to do what you want.
 
Hi, As with the example I put at the bottom (copy below), I have tried from the exit & lost focus when leaving Field1 but nothing works.
I want to be able to TAB from a number of different previous control/fields and it jump to Field2 and not select/highlight the whole field
Tabbing in the natural order works fine.

(Also, the got focus does trigger when using VBA)


Private Sub Field1_Exit(Cancel As Integer)
With Me.Field2
.SetFocus
.SelStart = 1
.SelLength = 0 ' Nothing selected
End With

End Sub

Private Sub Field2_GotFocus()

' nothing

End Sub
 
A little follow up info.

Firstly changing the Main Options behaviour to First, makes no difference.
If following normal Tab order it does do whats expected but as soon as you use setfocus it highlights/selects the field no matter what.

What I have discovered & allowed a work around (not pretty) is on the
KeyUp event. If I put the SelStart code there it will move to the correct cursor position.
The odd thing is, this is fired without typing anything, it seems to carry over the TAB or Enter being pressed from the previous control which is useful but also might explain'ish what is happening. The TAB or Enter press seems to still be happening long after Field2 has the focus or OnEnter triggered.
I wonder if somehow trying to cancel the key press on the previous control might work, to experiment. Keycode=0 not work so far.
Or I will stick with the KeyUp workaround.
I simply SelStart = 1 if this is the first time KeyUp is triggered. simple flag.

I would prefer to know why this does what it does as other seem to be able to get it to work.
Access 2007 & tried on 3 different PC's.

I have seen a few others confused by similar behaviour but never any resolution.

thanks again.
 
Private Sub Field2_GotFocus()
Field2.SelStart = 1
End Sub

If I simply tab from Field1 to Field2 it is fine, cursor moves to 2nd character and no highlighting.

If I jump to Field2 using setfocus from any other field it highlights the whole field.
So the GotFocus event handler, above, works when the focus is set using normal UI events, like tabs and mouseclicks, right?

But if you set the focus programmatically then write a custom .SetFocus method, like...
Code:
private sub MyField2SetFocus()
   with me.field2
      .setfocus
      .selstart = 1
      .sellength = 0
   end with
end sub
...and call that sub instead of Field2's .setfocus method when you want to set the focus programmatically.
 
Is this what you mean?

Calling this separate Sub still selects/highlights the whole field or am missing something else. Tried it in the Field1 OnExit event also.

Private Sub Field1_LostFocus()
Call MyField2SetFocus
End Sub

Private Sub MyField2SetFocus()
With Me.Field2
.SetFocus
.SelStart = 1
.SelLength = 0
End With
End Sub

Pressing TAB or Enter whilst on Field1 highlights as in attached picture (I know picture not necessary but thought I would include)
 

Attachments

This really is begining to wind me up,

I see what you were getting at but did not help.
If I have a button that runs the selstart sub & mouse click on it, it all works but if I call the Sub it does not, it needs the physical click onto the form, setfocus to the button does not help.
Just don't get that.
 
No, that's not what I mean. Maybe you can demonstrate the problem more exactly, like post a database or some code or something? Show what you have that works. Then show what you have that fails, and describe exactly how to make it fail.

What is the code, for instance, for this?
If I jump to Field2 using setfocus from any other field it highlights the whole field.
 
This is just a test database,

My main database I use the keyboard & do not want to use the mouse.

I want to be able to press Tab (usually) or enter from a variety of controls, go to a memo field and be ready to type, without deleting everything (because its is selected) I cannot use the natural tab order in all situations.

Of course in this example DB I could use natural tab order but using the setfocus because that is what I normally use.

Perhaps in a different version of access it will work but here in mine it will select & highlight whatever is in Field2 (as per my previous message & .pdf picture)

I have left all the other rubbish in the code,

.. it is just the On Exit from Field1 & the Sub called that is relevant here.

Thanks for any help
 

Attachments

OK, so how does it fail if you remove all other code from the form, and leave only this...
Code:
Private Sub Field2_GotFocus()
    Me.Field2.SelStart = 1
End Sub
 
Hi,

With just that line of code it does work as I would wish with natural TAB order, moving from Field1 TO Field2, it will move to cursor position 1 & not highlight/select & that would be great but as I say I need it to move from somewhere other than the natural TAB order and the second I use...

Private Sub Field1_Exit(Cancel As Integer)
Me!Field2.SetFocus
End Sub

.. it stops working in this way and always selects/highlights the entire field.

Why this should be is the confusing thing, I would naturally assume the GotFocus code would still have he desired effect firing long after the SetFocus but it not longer works.

I have tried for quite some hours in the past & again now.
I have setfocus on other forms, requery'd and as many things as I can think of in desperation. So far the only thing that works it that KeyUp bodge.

Thanks for your consideration.
 
Sel = Selection

The fact that the text is highlighted tells you that selstart is already 1 and sellength = len(Field2).
sellength = 0 means no selection.

The only code you need is

Code:
Private Sub Field2_Enter()
    Field2.SelLength = 0
End Sub
 
Hi,

that does not work either in this setfocus situation

I have generally been using this anyway so assume would always be no selection

With Me.Field2
.SelStart = 1
.SelLength = 0 ' Nothing selected
End With

I have since tried on a different access installation all together, Access 2010 & does the same.

thanks anyway.
 
The problem is that your events are clashing. You are trying to set focus to a control while another control is still exiting.

Code:
Private Sub Field1_Exit(Cancel As Integer)
    Field2.SetFocus
    Field2.SelLength = 0
End Sub

That makes no sense.
 
Sorry that was a typo, I have so many bits of code, most ' rem'd out

Field2 Got Focus

With Me.Field2
.SelStart = 1
.SelLength = 0 ' Nothing selected
End With

I am pretty sure now it is because the TAB or Enter keystroke from the previous control is carried over and still firing on Field2. I see others in slightly different situations trying to deal with this problem quite a lot.

What they do & I have found to work is in on the keypress of Field1,
If Enter or Tab pressed, Cancel the Keystroke (KeyCode = 0) and then Setfocus & code here instead of OnExit.

Just finalzing now, bit easier than my previous idea of using KeyUp & flags.
 
As Minty noted way, way back, if you set the focus programmatically, ...
Code:
Private Sub Field1_Exit(Cancel As Integer)
    Me.Field2.SetFocus
End Sub
...the GotFocus event handler for Field2 does not run, so in that case you need to call the MyField2SetFocus() sub-routine I posted, way, way back...
Code:
private sub MyField2SetFocus()
   with me.field2
      .setfocus
      .selstart = 1
      .sellength = 0
   end with
end sub
...such that if you want to set the focus to the Field2 programmatically, you call the sub-routine....
Code:
Private Sub Field1_Exit(Cancel As Integer)
    MyField2SetFocus
End Sub

So now, either your Field2_GotFocus() handler runs, or, in all OTHER cases, call MyField2SetFocus(). Now, how does it fail?
 
Hi,

Thanks for the "This adds a date to a textbox and selects the year part." example.

Is does not quite correspond to what I am trying to do as when you mouse click, all is fine, it is when forcing move focus via Tab or Enter from keyboard but NOT in the natural tab order.

In your example, say you were Tabbing through a form & wanted it to go to the Text0 (Date Field) from BOTH the command5 & Command2 buttons directly, Command2 natural tab order would be fine but putting SetFocus in OnExit Of command5 would then select the whole field and ignore the code in Text0_Enter.


With ref MarkK I had used the advised subroutine example in database21.accdb earlier in the thread & said it had not worked, it still selected the whole field. For ref. GotFocus in Field2 does fire anyway without the Sub but perhaps not in the manner you refer to, but I have tried all ways.

Are you saying that in the example attached SetFocusSelStart.accdb (pretty much the same as database21 earlier) you can get it to move from Field1 to Field2 using Tab or Enter key and it does not select the whole field.
Of course if you take out the Setfocus it will work but as I say I want to jump to this field from a variety of locations.

I have pretty much got around it now with a routine to handle keypress and effectively making my own OnExit routine. Does make it quite tricky to handle every eventuallity so prefer if it had worked without but come to conclusion carried over keypresses cause issues in this situation.

Thanks anyway to all help
 

Attachments

Users who are viewing this thread

Back
Top Bottom