Cancel Event - Ms Access/VBA

Hi Experts

I have a bound form which has a save button with existing Save function call (written by someone else). I have recently added few new fields to the form and added a code, sample below:

if isnull(NewAmount) then
   msgbox "NewField required"
   docmd.cancelevent
Endif

Call Savefunction()              'existing Save function

How do i cancel the Call of Savefunction() if Isnull logic is true? currently although i get error message but save still go through and closes the form (which is the function of existing Savefunction call).

Thanks
 
     






LVL 1
driversAsked:
Who is Participating?
 
VipulKadiaConnect With a Mentor Commented:
If you have written your code in SUB PROCEDURE then modify your above code as follows :
if isnull(NewAmount) then
   msgbox "NewField required"
   docmd.cancelevent
   Exit sub
Endif

or If you have written your code in FUNCTION then modify your above code as follows :
if isnull(NewAmount) then
   msgbox "NewField required"
   docmd.cancelevent
   Exit Function
Endif

0
 
pdebaetsCommented:
First, remove the "if isnull(..." block from your code above, then try putting this code in your form BeforeUpdate event procedure:

if isnull(NewAmount) then
   msgbox "NewField required. Press <ESC> to undo your changes."
   cancel = true
Endif

Open in new window

0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Or just use Else:

If IsNull(NewAmount) Then
  Msgbox "blah blah"
Else
  SaveFunction
End If
0
 
driversAuthor Commented:
Thanks everyone, i'll get back after some testing.
0
 
driversAuthor Commented:
Exit sub did the trick.. so points goes to VipulKadia
Thanks everyone
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.