SelStart and SelLength have no effect

evanscamman

Registered User.
Local time
Today, 15:19
Joined
Feb 25, 2007
Messages
274
I'm trying to select the contents of a text box like this:

ItemID.SetFocus
ItemID.SelStart = 0
ItemID.SelLength = len(ItemID)
End Sub

The SelStart and SelLength have no effect no matter how I have tried to use them. I have tried SelStart = 0, 1, 2, 3, etc... The position of the cursor never changes. Pressing TAB in the form (ItemID is the only tabbed control) selects the text in ItemID, so I'm considering using SendKeys... but I know that is 2nd rate programming.

Please help!
I'm using Access 2003.
Evan
 
1. make sure ItemID is also not the name of your field. If it is, change the text box to txtItemID.

2. Then, use
Code:
With Me
.txtItemID.SetFocus
.SelStart=0
.SelLength = Len(Me.txtItemID)
End With

And if that doesn't work, what event are you using this on? It might not be on the right event.
 
Bob,

I tried what you suggested with no luck. .SelStart and .SelLength do not appear to be properties of the form - as far as I am able to tell.

I also tried this:

txtItemID.Setfocus
txtItemID.SelStart = 0
txtItemID.SelLength = len(txtItemID)
End Sub

It appears to briefly select the text, and then lose the selection.
I checked my code for an event that was being triggered after the selection - causing it to lose the selection, but the .SelLength is the last line of code that runs.

Any ideas?

Evan
 
Got It!

Problem solved.

When the user hit enter, if the text they had typed in didn't meet certain criteria, I wanted it to be selected. The problem is that the enter key happens AFTER the AfterUpdate event, and so the selection was lost.

Solved it by trapping and canceling the 'Enter' key and then manually triggering the AfterUpdate event:

Private Sub ItemID_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 And Shift = 0 Then 'Enter Key
ItemID = ItemID.Text
ItemID_AfterUpdate
DoCmd.CancelEvent
End If
End Sub

Thanks Bob for the help,
Evan
 
What event are you placing this code in? The sub header is missing in all the posts do far.
 
The .SelStart and .SelLength code was in the ItemID_AfterUpdate event.
Since the enter key happens after the event - that's why my selection was being lost.
 

Users who are viewing this thread

Back
Top Bottom