VB errorchecking on lostfocus event

Posted in Visual Basic 6 at 2:14 am by

Today an error was reported by an end user of my program. They could get a crazy unstopable msgbox hell. After some investigation I found this happens when the user moves the focus from one faulty text field into another one which is also faulty. Both of these textfields have a lostfocus event. In the lostfocus event some checks are done and when they checks are positive a msgbox is shown and the focus is reset to the own textbox. Internally VB triggers a lost focus event when the user leaves the textbox. Then this lostfocus event does a setfocus on it’s own textbox. This triggers the lostfocus event of the other textbox, because by clicking on it the other textbox had the focus. The solution is simpel first do a setfocus and then show a msgbox with the error.

If Not ValidDate(Somefield.Text) Then
msgBox "Some message"
End if