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

Problem with lostfocus loop

Status
Not open for further replies.

rickpro

Programmer
Sep 11, 2002
7
CA
Hey, I am in school and learning VB6. I have run into a problem that I can't seem to solcve. I need to make sure all the fields are filled in before the user can process with the command button. I thought it would work if I just put an if stat in the lostfocus command of that text field. I have to set the focus back to that field so the user can fill it in. When I have 2 or more fields the message box that I have coming up just keeps coming up and does not go away until I end task on the prog. Any thoughts? Here is the code I am unsing:

Private Sub txtName_LostFocus()
If txtName = "" Then 'check to see if it's filled
strBox = MsgBox("Please fill in the name field.", vbOKOnly, "Empty Field")
txtName.SetFocus
intCheck = 1 'set check to 1 to allow cmd visable
Else
intCheck = 0
End If

End Sub

The code works fine if I don't set the focus back to txtName....

Thanks for any thoughts!
 
I suggest you use the Text_Validate event!
something like
Code:
Private Sub txtName_Validate(Cancel as Boolean)
If txtName = "" Then
'check to see if it's filled
    strBox = MsgBox("Please fill in the name field.", _
vbOKOnly, "Empty Field")
    Cancel = True
    intCheck = 1        'set check to 1 to allow cmd visable
Else
    Cancel = False
    intCheck = 0
End If

End Sub
Quite how you use intCheck is up to you to implement!
Youw will also need to ensure that the .CausesValidation property is set to true at designtime fro either the next text box in the tab sequence or if it is the only text box the command button for any action.

That should solve it! if not post back and /or check MSDN for validate event


Matt
 
Thanks for the help, after a little tinkering I got it to work!

Much thanks to you!

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top