PDA

View Full Version : Solved: Set Focus current field



davidboutche
03-12-2010, 08:51 AM
I'm trying to validate a field's input and if it doesn't meet my criteria to msgbox, remove the contents and then set the focus to itself. However, the focus just moves on to the next field. Where am I going wrong?

Private Sub DOBBOX_afterupdate()
If Len(DOBBOX.Text) <> 10 Then
DOBBOX.Text = ""
MsgBox "You must complete the DOB field in the correct format dd/mm/yyyy ie 17/11/1978"
Else
If CalcAge(DOBBOX.Text) < 18 _
And UserForm1.Combo_Box_01 = "Simple Caution" Then
MsgBox ("You can't issue a simple caution to an under 18")
End If
If CalcAge(DOBBOX.Text) > 18 _
And UserForm1.Combo_Box_01 = "Youth Final Warning" Then
MsgBox ("You cannot issue a Youth Final Warning to someone over 18")
End If
If CalcAge(DOBBOX.Text) > 18 _
And UserForm1.Combo_Box_01 = "Youth Reprimand" Then
MsgBox ("You cannot issue a Youth Reprimand to someone over 18")
End If
End If
DOBBOX.SetFocus
End Sub

lucas
03-12-2010, 09:24 AM
the setfocus must be within the if statement that you want it to work for. Just before the end if.

fumei
03-12-2010, 09:38 AM
Sorry, but that will not work.

_AfterUpdate fires, well, after update. This occurs when the focus IS shifting to the next control. Setting focus to the originating control does happen...and then focus shifts to the next control.

In other words, AfterUpdate processes before the actual focus change...and then the focus DOES change.

"BEFORE I shift to the next control, do this, and then shift focus."

The last part - shifting focus to the next control - still happens.

davidboutche
03-15-2010, 02:48 AM
Sorry, but that will not work.

_AfterUpdate fires, well, after update. This occurs when the focus IS shifting to the next control. Setting focus to the originating control does happen...and then focus shifts to the next control.

In other words, AfterUpdate processes before the actual focus change...and then the focus DOES change.

"BEFORE I shift to the next control, do this, and then shift focus."

The last part - shifting focus to the next control - still happens.

I think I understand. So can you suggest a solution? Or am I better using a validation with in conjunction with a command button when the form is processed?

fumei
03-15-2010, 11:02 AM
The easiest way is do your error-trapping in conjunction with a commandbutton when the form is processed.

davidboutche
03-16-2010, 05:34 AM
Ok thanks. In the end I did that but also added a star that appears if the data is invalid on exit of the box.