.setfocus not going to proper field

  • Thread starter Thread starter Brad Pears
  • Start date Start date
B

Brad Pears

I have a 4 char "year" field. When the user enters this value, I am checking
to make sure they are not entering a previous year. I display a warning and
then setfocus back to that field. However, the cursor goes to the next field
instead - which allows them to bypass the error...

What could be causing this??

Thanks,

Brad
 
I have a 4 char "year" field. When the user enters this value, I am checking
to make sure they are not entering a previous year. I display a warning and
then setfocus back to that field. However, the cursor goes to the next field
instead - which allows them to bypass the error...

What could be causing this??

Thanks,

Brad

You've probably used the wrong code or placed it in the wrong event.
Post he actual code and the event you placed it in.
 
I placed this code in the fields "lost focus" event and as well placed in
another fucntion that is called potentially two other fields "on update"
events .

Here is the code...

Private Sub txtBuildingYear_LostFocus()
' You cannot have a building year < current year
If Val(txtBuildingYear) < Val(Format(Year(Now()), vbyear)) Then
MsgBox "You can not have a BUILDING YEAR prior to the current year.
Please re-enter."
txtBuildingYear.SetFocus
End If
 
I placed this code in the fields "lost focus" event and as well placed in
another fucntion that is called potentially two other fields "on update"
events .

Here is the code...

Private Sub txtBuildingYear_LostFocus()
' You cannot have a building year < current year
If Val(txtBuildingYear) < Val(Format(Year(Now()), vbyear)) Then
MsgBox "You can not have a BUILDING YEAR prior to the current year.
Please re-enter."
txtBuildingYear.SetFocus
End If

Place the code in the Control's BeforeUpdate event.
You can use Date() rather than Now(), as you are not interested in a
time value.
Also, if the user is entering just the year, as for example, 2001,
then just compare the value directly, no need for Val().

Private Sub txtBuildingYear_BeforeUpdate(Cancel As Integer)
' You cannot have a building year < current year
If txtBuildingYear < Year(Date()) Then
MsgBox "You can not have a BUILDING YEAR prior to the current year.
Please re-enter."
Cancel = True
End If

Focus will remain in the control.
 
Back
Top