View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rob Bovey Rob Bovey is offline
external usenet poster
 
Posts: 811
Default Error trapping for the Form_Activate() event

Hi Andrew,

<<Playing with this a little bit more I found that if I create my own ShowMe
procedure in the form (like what you did to get Initialise to return a
boolean) and it contains an error then it does cascade back, eg.

Yes, and in general this is the architecture I recommend. It gives you
complete control over everything that happens until you're sure the form is
ready to go.

<<You say you never put any code that might throw an error into the
UserForm_Activate event. Does this mean you would provide a separate
procedure in the form to display the data rather than doing it from the
Activate event?

That's correct. For single forms this would be the custom Initialize
method I talked about in the book. In the case of forms that need to display
data returned from other forms I create a separate procedure for each
additional case where data needs to be transferred from one form to another
or simply use custom property procedures if the data to be transferred is
very simple.

Here's a basic example of using a property procedure to pass a text
string from one form to another:

ftp://ftp.appspro.com/FormDemo.zip

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

wrote in message
ups.com...
Thanks Rob for such a quick reply! Playing with this a little bit more
I found that if I create my own ShowMe procedure in the form (like what
you did to get Initialise to return a boolean) and it contains an error
then it does cascade back, eg.

Public Sub ShowMe()
dim lng as Long
lng=1/0
Me.Show
End Sub

What I am actually doing in the Activate event is updating a listbox
based on data already passed to the form. It's unlikely that an error
will occur but it doesn't fit the "non-trivial" description. You say
you never put any code that might throw an error into the
UserForm_Activate event. Does this mean you would provide a separate
procedure in the form to display the data rather than doing it from the
Activate event? (I can't use the Initialize event because other forms
will change the values).

BTW I found this chapter really enlightening and have read through it a
number of times. My code looks quite a lot different as a result!

Best regards,
Andrew


Rob Bovey wrote:
Hi Andrew,

Ack! You've discovered a nasty bug in my error handling chapter. I
personally never put any code that might throw an error into the
UserForm_Activate event so I never ran across this (or have long since
forgotten if I ever did). That led to the horrible untested assumption on
my
part that errors in UserForm_Activate would behave like errors in
UserForm_Initialize.

After doing a bit of testing it seems as if you have two choices.
Either
use On Error Resume Next to blow past anything that goes wrong or treat
UserForm_Activate as if it were an entry point procedure and be prepared
to
handle any potential errors inside the error handler for that procedure.
This bug will get fixed in the next edition of the book. Thanks for
pointing
it out.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

wrote in message
oups.com...
In the recent (& extremely useful) Bullen, Bovey, Green book it is
recommended not to put error trapping in the form Initialize or
Activate events but instead to let the error be handled by the
procedure which loaded the form or displayed it. This seems to make
good sense but if an error occurs in my Activate event it does not
cascade back to the calling procedure but instead gives me an error
message. Am I missing something here?

(If the error occurs in the Initialize event then this is handled as
desired).

Thanks,
Andrew