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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set focus is not working 1

Status
Not open for further replies.

inncoggnitto

Programmer
Joined
Jun 19, 2001
Messages
77
Location
US
i have a form with numerous text boxes. i am validating data on lost focus. i want to return the user back to the box if the data is not valid (i.e., an out of range number). i don't want to use keep focus since a blank might be a valid input. for example on the LostFocus for a text box

Code:
Private Sub txtLongDecimal_LostFocus()
        If txtLongDecimal.Text <> &quot;&quot; And IsNumeric(txtLongDecimal.Text) = True Then
        If txtLongDecimal.Text > 92 And txtLongDecimal < 107 Then
            txtLongDecimal.Text = Round(txtLongDecimal.Text, 5)
            txtLongDecimal.SetFocus
        Else
            MsgBox &quot;This value must be >= 93 and <= 106 degrees.&quot;
            txtLongDecimal.Text = &quot;&quot;
            txtLongDecimal.SetFocus
        End If
    ElseIf txtLongDecimal.Text <> &quot;&quot; And IsNumeric(txtLongDecimal.Text) = False Then
          MsgBox &quot;This value must be numeric and must be >= 93 and <= 106 degrees.&quot;
          txtLongDecimal.Text = &quot;&quot;
          txtLongDecimal.SetFocus
    Else
        txtLongDecimal.Text = &quot;&quot;
        txtLongDecimal.SetFocus
    End If
End Sub

does not set the focus back to the text box. the cursor still moves to the next box. i have tried setting focus to any other control and still it does not work, just moves to the next control in the tab order.

suggestions are welcomed.
 
Your code

txtLongDecimal.Text = &quot;&quot;
txtLongDecimal.SetFocus

should do what you want it to do (I've used identical code for a while now). Without setting this all up on a form and running it, I'm guessing the problem lies elsewhere, specifically in you conditional statements.

You're comparing text with numbers, i.e. If txtLongDecimal.Text > 92

You should be saying something like If Val(txtLongDecimal.Text)>92

While VB SOMETIMES lets you get away with using textbox entries as numbers without converting them, it does not ALWAYS let you do this (I am told the new VBNet will NEVER let you do it) and so text shold always be converted to numbers before using in calculations or in conditional testing.

Hope this helps.

&quot;It's got to be the going,
not the getting there that's good!&quot;
-Harry Chapin
 
Stupid question: can I ask why you are validating using LostFocus? That is, are you using VB 5.0? Because VB 6.0 has a Validate event with a Cancel parameter. If you don't want the user to move focus, you set Cancel = True:

Private Sub Text1_Validate(Cancel As Boolean)
' If the value is not a date, keep the focus, unless the user
' clicks Help.
If Not IsDate(Text1.Text) Then
Cancel = True
MsgBox &quot;Please insert a date in this field.&quot;, , &quot;Text1&quot;
End if
End Sub

You also set the CausesValidation property on the controls you want to move to. OK buttons will have a CausesValidation=True and so fire the event. Cancel buttons have CausesValidation=False.

If there is an system event which doesn't fire off the Validate event on its own (for instance, Form_Unload), you can call the Form.ValidateControls method which will fire off the Validate for the last visited control on the form.

This will be a lot simpler than using LostFocus, believe me.
 
One suggestion is to call the setfocus function before the call to the messagebox.

In two cases, you make a call to msgbox, and then call SetFocus. Change these two snippets to the following:


txtLongDecimal.Text = &quot;&quot;
txtLongDecimal.SetFocus
MsgBox &quot;This value must be >= 93 and <= 106 degrees.&quot;

Hope this helps
 
I pasted your code into a test project, and it always set the focus back to the txtLongDecimal text box, regardless of the input. I recommend setting a breakpoint at the beginning of your subroutine to see if it is being called.

If the subroutine is not being called, use the following procedure. Run the project from the VB environment. Set the focus to the text box you want to test and make your test entry. Perform a Ctrl + Break to pause the project. Press the F8 key to continue the project in the stepwise mode. Click on the icon for your form in the task bar to re-display the form. Press Tab or click on another text box to find out what subroutine is executing when control to your text box is lost.

Hope this helps.
 
i am actually using the validate function (the lost focus was just a quick example). i have been trying everything i can think of to get the focus back to where i want it. i do not want to use keepfocus as it will not let the useres tab through the fields if there is no data to put into it if i set keepfocus = true. this form has over 80 fields on it (mostly address information) and i am doing validation on about half of them.

the following is a shortened example that i just tried. the focus does not return to the text box. i allways goes to the next control in the tab order (in this case a button). everywhere i have tried it in the code on any control it always sets focus to the next control.

Private Sub txtLongDecimal_Validate(Cancel As Boolean)
txtLongDecimal.Text = &quot;&quot;
txtLongDecimal.SetFocus
End Sub
 
Within a Validate Function, I don't believe that you should you the SetFocus method. Rather, you assigned True to Boolean Parameter. I also, for code readability, rename the paramer to keepfocus

Private Sub control_Validate(KeepFocus As Boolean)

If <edit check fails> then
KeepFocus = True
end if

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top