.setfocus not going to proper field

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
 
F

fredg

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.
 
B

Brad Pears

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
 
F

fredg

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.
 

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

Top