Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Focus after validation

Status
Not open for further replies.

alanf

Programmer
Feb 7, 2001
39
US
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.
 
Have you tried the following:


Private Sub Text1_Validate(KeepFocus As Boolean)

If <Fails Validation> Then
KeepFocus = True
MsgBox <Error Message>
End if

End Sub
 
Well, I know it may not be the most P.C. code but here's how I get around it.

Private Sub Text1_LostFocus()
If Text1.Text <> WHATIWANTHERE Then
Text1.Text = &quot;&quot;
MsgBox &quot;ERROR&quot;
Text1.SetFocus
End If

When the control loses focus (click or tab to something else) this runs and catches the error.

Enjoy!

*J*
 
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 = &quot;&quot;
MsgBox &quot;ERROR&quot;
Text1.SetFocus
End If
Enb Sub

Private Sub Text2_LostFocus()
If Text2.Text <> WHATIWANTHERE Then
Text2.Text = &quot;&quot;
MsgBox &quot;ERROR&quot;
Text2.SetFocus
End If
End Sub

This can cause you to bounce indefinately between the two controls.

Good Luck
 
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 = &quot;&quot; then exit sub.

Once again not the cleanest code, but I'm a Network Admin so I'm not supposed to create excellent code. :)

*J*
 
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 (&quot;Your DSA name must have at least four characters.&quot;)
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 &quot;You must enter a valid number.&quot;
End If
End Sub

This fails to send the focus back to the control.
 
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 &quot;error ...&quot;
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.


Good Luck
 
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??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top