calculation runs when I dont want it to!

starmanMike
02-04-2008, 03:34 PM
Hi
I have an error-handler in the lost_focus event of a text box called txtParas. If the figure is out of bounds (in this case 1000 or more) I want the focus put back in this box again. This does happen, but for some reason another calculation (which is written to the next text box called txtMags, and is in that text box's lost_focus event also) gets performed. I only want that calculation performed when the figure in txtParas is acceptable!

This traps the error, and is in txtParas.text_LostFocus:

Select Case theunit
Case "mas"
spcs = Val(txtParas.Text) / 1000
If Val(txtParas.Text) >= 1000 Then
MsgBox (must), vbExclamation, (wrong)
txtParas.SelStart = 0
txtParas.SelLength = Len(txtParas.Text)
txtParas.SetFocus
Exit Sub
End If
...other cases here...
end select
end sub

But even though it breaks out of the sub and shows the message box correctly, the calculation in the next text box is still performed, even though focus is back with txtParas. Why is the other code in txtMags being run, even though its in txtMags' lost_focus event, and we haven't even been there yet?
Help! I know it's got to be something simple...

JPB
02-04-2008, 05:14 PM
Without having all of the code and form layout, I can only venture a guess. Assuming that txtMags is the next control in the tab order after txtParas, then when txtParas loses focus, txtMags gains it. When you move the focus back to txtParas, the LostFocus event fires for txtMags. Instead of using the LostFocus event for txtParas, use the Validate event. It allows you to cancel the focus from moving to any other control:


Private Sub txtParas_Validate(Cancel As Boolean)
Select Case theunit
Case "mas"
If Val(Me.txtParas.Text) >= 1000 Then
MsgBox (must), vbExclamation, (wrong)
txtParas.SelStart = 0
txtParas.SelLength = Len(txtParas.Text)
txtParas.SetFocus

Cancel = True ' Prevents focus from leaving txtParas on invalid input
Exit Sub
End If
End Select
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum