TextBox's BeforeUpdate errors

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I am trying to write some code to make sure that data in a certain text box
is compliant with certain rules. This is the code:

-------------------------- code starts here --------------------------
Private Sub sparNum_BeforeUpdate(Cancel As Integer)

If Me!sparNum < 1 Then
MsgBox "Il campo 'Numero' può contenere solo valori numerici
positivi.", vbExclamation, "Valore non valido"
Cancel = True
End If
End Sub

-------------------------- code ends here ---------------------------
the sparNum textbox is a required field, numeric, integer, and no other
validation rules exist.
the problem is:
after the MsgBox fires, the flow goes to the form_error routine, 'cause an
error 2116 has been generated. I cannot get rid of it, and I am also not able
to clear the textbox, after the errors the negative number is still there (so
what is the Cancel for if setting it to True does not delete the value), even
me!sparNum.undo does not clear it (me!undo actually does, but clears also all
the other fileds ...) and if aI try to put some code to delete it, there are
several errors...
What am I doing wrong?
And do you know where I can find a detailed list of the possible error
numbers with some explainations?

any help very much appreciated.
thanks
Massimo
 
max said:
Hi all,

I am trying to write some code to make sure that data in a certain text box
is compliant with certain rules. This is the code:

-------------------------- code starts here --------------------------
Private Sub sparNum_BeforeUpdate(Cancel As Integer)

If Me!sparNum < 1 Then
MsgBox "Il campo 'Numero' può contenere solo valori numerici
positivi.", vbExclamation, "Valore non valido"
Cancel = True
End If
End Sub

-------------------------- code ends here ---------------------------
the sparNum textbox is a required field, numeric, integer, and no other
validation rules exist.
the problem is:
after the MsgBox fires, the flow goes to the form_error routine, 'cause an
error 2116 has been generated. I cannot get rid of it, and I am also not able
to clear the textbox, after the errors the negative number is still there (so
what is the Cancel for if setting it to True does not delete the value), even
me!sparNum.undo does not clear it (me!undo actually does, but clears also all
the other fileds ...) and if aI try to put some code to delete it, there are
several errors...
What am I doing wrong?

The Cancel argument ONLY cancels the update. To clear the entry you need ...

Me.sparNum.Undo
 
The Cancel argument ONLY cancels the update. To clear the entry you need ...
Me.sparNum.Undo


Hi Rick

before I had tried Me!sparNum.Undo and didn't work.
Now I've tried Me.sparNum.Undo and does not work either.
And if try with something like me!sparNum.text = "" I get a runtime error...

any idea?
 
max said:
Hi Rick

before I had tried Me!sparNum.Undo and didn't work.
Now I've tried Me.sparNum.Undo and does not work either.
And if try with something like me!sparNum.text = "" I get a runtime error...

any idea?

Are you issuing both the Cancel and the Undo?
 
Rick Brandt said:
Are you issuing both the Cancel and the Undo?

if I put
Cancel = True
Me!sparNum.Undo
or
Me!sparNum.Undo
Cancel = True

the result is that the negative number is selected but not deleted, and an
error 2116 is generated and trapped in the form_error routine. I spent a lot
of time reserching everywhere what this number means, but no luck.
no idea...
 
max said:
if I put
Cancel = True
Me!sparNum.Undo
or
Me!sparNum.Undo
Cancel = True

the result is that the negative number is selected but not deleted, and an
error 2116 is generated and trapped in the form_error routine. I spent a lot
of time reserching everywhere what this number means, but no luck.
no idea...

I have never used the OnError event of a form and can only assume that it is
interfering somehow.

In my test...

Cancel = True
Me!sparNum.Undo

....both cancelled the update and reverted the TextBox to its previous value.
 
ok, since that I cannot do without the form_error routine cause I put a lot
of code in it, I think I can be satisfied like this, cause in that routine I
can handle the following errors not to show multiple msgboxes, and the good
thing is that altough the negative number stays in the textbox, when i put
the undo line in the code, the new record is not dirty anymore (strange)
which means that the user can click on the "discard new record" button to go
back to the view mode. without the other errors I got without the undo line.
Trying to be more clear:
without Me!sparNum.Undo >> the user cannot leave the field (neither click
the button) until he presses ESC or types a valid number.
with Me!sparNum.Undo >> the number is still there but it is possible to
leave the field or press any other button even if the field in question is
required and showing a non-valid number.

so in the end is not too bad...

thanks a lot for your help.

cheers
Massimo
 
Back
Top