Validation During BeforeUpdate Event

  • Thread starter Thread starter Greenface
  • Start date Start date
G

Greenface

Guest
Question - How do you do data validation during the BeforeUpdate event? How do you halt the event without getting an error message?

I have tried the following:

If (Me.Control.Value <> "") Then
'Do nothing.
Else
Msgbox "Please enter a value for Control."
Cancel = True
End If



When Cancel = True is executed, I get the following message:
You can't save this record at this time. DatabaseName may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to close the database object anyway?

I can't seem to halt or "cancel" the BeforeUpdate event without getting this error message. After hours of looking for this answer, any suggestions would be appreciated.
 
I would think you should set this in the table as a required fld...???
 
Thanks for replying, Ken. I didn't set it as required for a number of reasons. Also, I posed the question with a generic example conditional statement; some statements will not be testing for zero-length strings but instead for other values.
 
I have this work on several of my forms:

If Isnull(yourControl) then
Msgbox "your message", vbOkOnly, "Required information"
Cancel = True
Docmd.GotoControl "yourControl"
Exit Sub
End If


I guess that GotoControl action keeps focus back on the same control and thus keeps the form from being updated.
 
BeforeUpdate event of the Form or a Control on the Form?
 
I put the codes under the Form's BeforeUpdate event. I think only in very special case you can put it under the Control's BeforeUpdate event. The BeforeUpdate and AfterUpdate events of a Control will not be triggered if you bypass the control.
 

Users who are viewing this thread

Back
Top Bottom