Hi everyone. I have been unable to set the focus back to a control (i.e., textbox) after validating. Once the textbox fails the validation event, I pop an error message, but then I can't seem to set the focus back there. Any help is appreciated.
Jyschaefer,
The code that you have will work, however, you may run into a problem if you have two controls that require validation, and the user clicks on the second control, which also fails validation.
Private Sub Text1_LostFocus()
If Text1.Text <> WHATIWANTHERE Then
Text1.Text = ""
MsgBox "ERROR"
Text1.SetFocus
End If
Enb Sub
Private Sub Text2_LostFocus()
If Text2.Text <> WHATIWANTHERE Then
Text2.Text = ""
MsgBox "ERROR"
Text2.SetFocus
End If
End Sub
This can cause you to bounce indefinately between the two controls.
Good Point Cajun,
When I used it, I was checking to verify if a number was in a number field, text was in a text field. I started them all off with If text1 = "" then exit sub.
Once again not the cleanest code, but I'm a Network Admin so I'm not supposed to create excellent code.
Well, I have a control array, so there is an Index parameter passed to the sub. But I had not noticed that there was a Cancel paramter too. Based on CajunCenturion's suggestion I set the Cancel to True, and that succeeds at keeping the focus.
Private Sub txtDSA_Validate(index As Integer, Cancel As Boolean)
If Len(txtDSA(0)) < 4 Then
Cancel = True
MsgBox ("Your DSA name must have at least four characters."
End If
End Sub
So, thank you CajunCenturion. Now, can you tell me what is happening here?
I have a masked edit box on a different form. There the code does not work.
Private Sub medDrvr_Validate(index As Integer, Cancel As Boolean)
If Not IsNumeric(medDrvr(4)) Then
Cancel = True
MsgBox "You must enter a valid number."
End If
End Sub
First, I see a possible bug in your program. You have
If Len(txtDSA(0)) < 4 Then
in your code. This will always check the value of the first control, regardless of which control in the array currently has the focus, and the focus will always remain with the current control, never letting you get back to the first control to correct the problem. I would suggest that this conditional should read as follows:
If Len(txtDSA(Index)) < 4 Then
On the other hand, if this validation only applies to the 0th control, and not to any others, then you might want to try the following code
If Index = 0 then
if len(txtDSA(0)) < 4)
cancel = true
msgbox "error ..."
end if
end if
Now, as to what is happening - only Microsoft knows for sure (and in some circles, that may be debateable), but ...
When you click on another control, or tab to the next control, that control causes the validation event of the previous control to be fired.
Since the cancel parameter is set to true, the focus remains with the previous control and nothing else happens.
If the Cancel event were to remain false, then the lost_focus and got_focus events of the two corresponding controls would be fired.
CajunCenturion,
Thanks for your input. In fact, that code was only to validate the 0th control, and I do check the index as you suggest. I had simplified the code for posting to the forum. Thanks for clarifying what the Cancel parameter is doing. Any guesses as to why the code does not leave the focus on the masked edit control as per my last post??
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.