I have a windows form with controls bound to an Access database. When data is entered/changed in a field I want to 1)validate that the data is numeric and 2) if it is format the data show that it is displayed as 99.99.
When the code in the mycontrol_validating event is as follows:
If IsNumeric(mycontrol.Text) Then
mycontrol.text = FormatNumber(mycontrol.text, 2)
else
MessageBox.Show("The value you entered is not numeric", "Invalid Data Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
mycontrol.Focus()
Exit Sub
End If
It will trap the error and display the message if the data is invalid, but if it is numeric it does not correctly format it (i.e. instead of 1.00 it will display as 1)
I tried moving the code to the mycontrol_validated and
what happened then was if invalid data had been entered it reverted back to the old, valid value before the mycontrol_validated event was called. If the data was numeric it correctly formatted it.
The code works as I want if I put the code to check for invalid data in the mycontrol_validating event and the formatting in the mycontrol_validated event.
Can anyone explain this behavior. I would like to have an understanding of what's actually going on so that I don't have to rely on trial and error.
When the code in the mycontrol_validating event is as follows:
If IsNumeric(mycontrol.Text) Then
mycontrol.text = FormatNumber(mycontrol.text, 2)
else
MessageBox.Show("The value you entered is not numeric", "Invalid Data Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
mycontrol.Focus()
Exit Sub
End If
It will trap the error and display the message if the data is invalid, but if it is numeric it does not correctly format it (i.e. instead of 1.00 it will display as 1)
I tried moving the code to the mycontrol_validated and
what happened then was if invalid data had been entered it reverted back to the old, valid value before the mycontrol_validated event was called. If the data was numeric it correctly formatted it.
The code works as I want if I put the code to check for invalid data in the mycontrol_validating event and the formatting in the mycontrol_validated event.
Can anyone explain this behavior. I would like to have an understanding of what's actually going on so that I don't have to rely on trial and error.