?
Solved

Cancel Event - Ms Access/VBA

Posted on 2011-10-16
5
Medium Priority
?
1,205 Views
Last Modified: 2012-05-12
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
 
     






0
Comment
Question by:drivers
5 Comments
 
LVL 12

Expert Comment

by:pdebaets
ID: 36977720
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
 
LVL 8

Accepted Solution

by:
VipulKadia earned 1000 total points
ID: 36977741
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
 
LVL 85
ID: 36978882
Or just use Else:

If IsNull(NewAmount) Then
  Msgbox "blah blah"
Else
  SaveFunction
End If
0
 
LVL 1

Author Comment

by:drivers
ID: 36983153
Thanks everyone, i'll get back after some testing.
0
 
LVL 1

Author Comment

by:drivers
ID: 36988564
Exit sub did the trick.. so points goes to VipulKadia
Thanks everyone
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question