INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

SelStart SelLength issue

SelStart SelLength issue

(OP)
Hey helpful people,

I'm trying to highlight the text in a textbox control if the user enters something incorrect. I put this code (and more) in the AfterUpdate event.

        With Me.txtCheckNum
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
 
When I step through the code, I can see that the text in the textbox is getting highlighted (just as I want). But as soon as the code hits End Sub, the the text is unhighlighted and the cursor ends up at the beginning of the text in the textbox. Can anyone clue me as to what I'm doing wrong that the text doesn't stay highlighted?

Thanks!

RE: SelStart SelLength issue

I would try set the focus to another control and then back to txtCheckNum. I believe you won't need to use SelStart or SelLength.

Duane
Hook'D on Access
MS Access MVP

RE: SelStart SelLength issue

(OP)
Thanks, Duane. I tried that, but that didn't seem to do it. But I think I may have solved the riddle. Seems that I can't do this in the AfterUpdate event, but rather have to do it in the BeforeUpdate event. I thought I needed it in the AfterUpdate as I didn't think it would know the value entered BeforeUpdate. But appears it does. I moved everything to the BeforeUpdate event, and now it works to keep the field highlighted. However, when using the AfterUpdate event, I was locking the other fields on the form so the user couldn't just continue anyway. It wouldn't let me lock the other fields when I moved the code to BeforeUpdate, but it *would* let me disable the fields and that works just as well. So, I believe I've got it doing what I was hoping for now.

RE: SelStart SelLength issue

Quote:

Seems that I can't do this in the AfterUpdate event, but rather have to do it in the BeforeUpdate event.
That's correct! Any validation done on a Control, immediately after the data is entered, has to be done in the Control's BeforeUpdate event. If the validation fails, you can cancel the update using...Cancel = True...and the Focus will automatically remain on that Control, until the appropriate data is entered.

Since the Focus will remain on that Control, until the appropriate data is entered, there is no need to Lock or Disable other Controls.

Here's a simple example of this kind of thing:

CODE -->

Private Sub TargetControlName_BeforeUpdate(Cancel As Integer)

 If Not IsNumeric(Me.TargetControlName) Then
  MsgBox "This Field Must be Numeric!"
  Cancel = True
 End If

End Sub 
You could, I believe, replace the MessageBox with other actions, such as highlighting the data.

Hope this helps!

There's always more than one way to skin a cat!

All posts/responses based on Access 2003/2007

RE: SelStart SelLength issue

(OP)
All good info! Thanks much!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close