Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelStart SelLength issue

Status
Not open for further replies.

merlizbet

Technical User
Oct 4, 2012
36
US
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.

[pre]
With Me.txtCheckNum
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
[/pre]
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!
 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top