Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Error trapping for the Form_Activate() event

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Error trapping for the Form_Activate() event

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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Error trapping for the Form_Activate() event

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Error trapping for the Form_Activate() event

Thanks a lot. That definately looks like the route to go.

Cheers,
Andrew

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
trapping charts event PLP Charts and Charting in Excel 2 June 21st 07 01:18 AM
Error Trapping from WSH Tom Chau Excel Discussion (Misc queries) 1 August 25th 06 04:21 AM
Error Trapping Andrew Excel Programming 1 April 5th 05 07:23 AM
Trapping Excel Close Event Bob J.[_2_] Excel Programming 3 December 6th 03 08:57 PM
Trapping Excel Close event Bob J[_2_] Excel Programming 3 December 5th 03 11:56 PM


All times are GMT +1. The time now is 06:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"