Highlighting error data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi everyone,

I have a data entry form which validates data entered on the form. Anytime
an error occurs a message is displayed and control remains on at the current
control on the form.

What I would like to do is the select/highlight the invalid data so that
when the user begins to reenter valid data, the invalid data is replaced.

Any ideas how I can accomplish this?

Many thanks

Will
 
Hi.

First, give the control the focus:
Me.ControlName.SetFocus

Then, highlight the contents of the control:
Me.ControlName.SelStart = 0
Me.ControlName.SelLength = Len(Me.ControlName)

I hope this helps. If not, please provide more information regarding how
you are validating the data.

-Michael
 
Hi Michael,

Thank you for responding to my post.

I tried your suggestion, but the data was not selected/highlighted, instead
the cursor was positioned after the last character of the invalid entry.
Here's the code....

**************************
Private Sub fileno_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_fileno_BeforeUpdate

Dim dyr As String
Dim cyr As String
cyr = Mid(Date, 1, 4)
dyr = Mid(fileno, 5, 4)

If dyr > cyr Then
mtxt1 = "File number "
mtxt2 = " is invalid. Year "
mtxt3 = " should be equal to or less than current year "
MsgBox mtxt1 & fileno & mtxt2 & dyr & mtxt3 & cyr
Cancel = True
Me.fileno.SetFocus
Me.fileno.SelStart = 0
Me.fileno.SelLength = Len(Me.fileno)
End If


Exit_fileno_BeforeUpdate:
Exit Sub

Err_fileno_BeforeUpdate:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_fileno_BeforeUpdate
End Sub
*****************************

Any other suggestions??

Many thanks again


Will
 
Hi.

Not sure, as this method works for me. However:

I was unaware that your validation code is in the BeforeUpdate event of the
control in which you want to highlight data. So, there is no need to set the
focus to the control, and in fact, doing so may cause a Run Time Error 2108
(which means your If...Then code isn't running). Eliminate the
"Me.fileno.SetFocus" line.

Is your custom error message ("File number x is invalid...") being
displayed, or is it the error message from your "Err_fileno_BeforeUpdate"
section? Or, is it a system error message? If anything other than the first
one, your If...Then code is not running.

-Michael
 
Hi Michael,

I removed the line and it's working now..

Thank you so much!!

Appreciated.

Will
 

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

Back
Top