Loading...
Saturday, April 27, 2013

Solution for Property not Found error on BeforeUpdate event

Property not Found error will occur when you want to cancel the value update on BeforeUpdate event in MS Access Form. When you set the Cancel = True you will always have this error. There is nothing seriously affects your code, the only thing is that the message is just annoying, and this also show unprofessional way of coding if you let users see this error.


Alright, let's get down to business, shall we?

There are 2 ways to solve this problem, the easy one, and the tricky one with a bit of work to be done. And also, both way have a little different end result. I have to leave the choices for you to make.

Solution #1


The first solution for Property not Found error is very easy and painless, and there is only one line of code to be done. The key is you can't use Cancel = True on BeforeUpdate event, but what you need is Docmd.CancelEvent. Below is the example code to solve this problem.


Private Sub objTricky_BeforeUpdate(Cancel As Integer)
 If condition is true Then
  Docmd.CancelEvent
 End If
End Sub
The end result of this solution: You have to pass the condition to continue. Otherwise, it will keep on with the event cancellation until the condition is success.


Solution #2

To get rid of Property not Found error in in the second way  is a bit tricky. To solve this very problem, there are 3 little things to be made.

  1. Declare one global value on the form.
  2. Get rid of that Cancel = True code on BeforeUpdate event.
  3. You will need Me.Undo to save your problem, and you have to place the key solution on AfterUpdate event.
There you are, your problem of Property not Found should be resolved.

Let me show you how this works, in case my message doesn't convey well (my bad). Let me name my object (control or form) as objTricky and global variable as gv_continue for the sake of this example.

Originally, I would have my code like this when I have this problem:

Private Sub objTricky_BeforeUpdate(Cancel As Integer)
 If condition is true Then
  Cancel = True
 End If
End Sub
To solve this problem, I came up with the modification version of the original code :

Dim gv_continue as Boolean
Private Sub objTricky_BeforeUpdate(Cancel As Integer)
 If condition is true Then
  gv_continue  = False
 Else
  gv_continue  = True
 End If
End Sub
Private Sub objTricky_AfterUpdate()  If Not gv_continue Then   ''--Add your message here if you like   Me.Undo  End If End Sub
The end result of this solution: The will undo all of the update you have made earlier when the condition fails.


That's it folks, pretty easy right? As I said the choices is yours, #1 or #2. I hope you will be able to resolve your Property not Found error as I have resolved mine.





0 comments:

Post a Comment

 
TOP