Hey,
I wrote some code to validate an inputfield. I want to keep the focus on this field until a correct (numeric) value is inputed. Therefore I use teh beforeUpdate event instead of the afterUpdate event. Some strange things happen (see code below):
1. When I press enter after putting some non-numeric data in the inpBedrag2 field the msgbox is displayed three times in a row. This doesn't happen when the field loses focus by clicking another field (-> msgbox only displays once = correct behaviour)
2. What's really driving me nuts is the fact that the (more or lesse the same sub) works perfectly.
What am I doing wrong? I'm not getting it. Probably I'm doing something very stupid ... but I can't figure it out myself.
Code
Private Sub inpBedrag_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim intReturnWaarde As Integer
If Not IsNull(inpBedrag.Value) And inpBedrag.Value <> "" And RequiredNumber(inpBedrag.Value) Then
inpBedrag.Value = FormatNumber(inpBedrag.Value, 2, vbUseDefault, vbUseDefault, vbTrue)
If (inpBedrag.Value = 0) Then
intAntwoord = MsgBox("Gelieve een bedrag hoger dan 0 in te geven.", vbOKOnly + vbCritical, "Invoerfout")
inpBedrag.Value = Null
Cancel = True
Else
intReturnWaarde = maakBerekening()
End If
Else
intAntwoord = MsgBox("Gelieve een numerieke waarde in te geven.", vbOKOnly + vbCritical, "Invoerfout")
inpBedrag.Value = Null
Cancel = True
Exit Sub
End If
End Sub
Private Sub inpBedrag2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim intReturnWaarde As Integer
If Not IsNull(inpBedrag2.Value) And inpBedrag2.Value <> "" And RequiredNumber(inpBedrag2.Value) Then
inpBedrag2.Value = FormatNumber(inpBedrag2.Value, 2, vbUseDefault, vbUseDefault, vbTrue)
intReturnWaarde = maakBerekening()
Else
intAntwoord = MsgBox("Gelieve een numerieke waarde in te geven.", vbOKOnly + vbCritical, "Invoerfout")
inpBedrag2.Value = Null
Cancel = True
Exit Sub
End If
End Sub
Display More
Thanks in advance,
Jan Coonen