Error in 'Before Update' event ?

M

mscertified

In a 'Before Update' event I have this code:

If Len(Me!Topic) = 1 Then
Me!Topic = "0" & Me!Topic
End If

I'm getting a VB error "function is preventing saving the data in the field"
Is there a better way of making a single digit have a zero in front of it
without forcing the user to enter it?
Thanks.
 
G

Graham Mandeno

Hi Rupert

You cannot change the value of a control in its BeforeUpdate event. You
must (strange as it may sound!) use the AfterUpdate event.

The BeforeUpdate event is used for checking validity of the data that has
been entered and, if necessary, undoing the change or preventing the focus
from leaving the control.

AfterUpdate is used to "tweak" the value that was entered and also to
execute any code as a consequence of the new value.

I must ask: Is your Topic field text or numeric? If it's numeric then you
don't need any code at all - simply set the Format property to "00" and a
single digit number will be *displayed* with a leading zero.
 
K

Klatuu

I don't know if this is a duplicate, I posted, got a login screen, and this
again.
That is too late. Either present a message box to the user to make the
change and cancel the event or better, move your code to the Lost Focus event.
 
Top