Disabling events when code is running

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,347
I have a userform with several togglebuttons. Each togglebutton has some code that runs using the ToggleButton1_MouseMove procedure - (the code involves activating a sheet and geting a list of values for entry into a textbox).

Once the 'Go' button is clicked, the main procedure runs to get data from an Access database, process it and write the result tio a new workbok. However, if the user moves the cursor over one of the togglebuttons while the main prcedure is running, an error occurs, as the ToggleButton1_MouseMove attempts to run. I want to arrange it so that this code is disabled when the main procedure is running. I guess I may be able to use Application.EnableEvents = False, but I wondor if there is a better way than this. If I set it to False, I need to make sure that it's set back to True at the end, and this has the problem that if there is an untrapped error, and the code errors out, then the user will be left in the Application.EnableEvents = False state, which I want to avoid.

Any ideas how to achieve this easily?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Well, you could always re-enable events in your error handling code to avoid this error.

Richard
 

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,347
Yes but despite best efforts to predict all errors that might occur there is always the spectre of something entirely unpredictable or uncontrollable occuring, and it's just about impossible to catch everything. I really would like to just turn these events off. One way might be to hide the cursor, but I can't see how to do that. Another is to set the buttons to enabled = false but that's messy also as I have many buttons, some if which are only ever enabled for me (as the administrator), and some of which are enabled base on the user's selections. I want to avoid having to swithch everything off and back on again if possible.
 

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,347
Soted! As it happens, the togglebutoons are all in a frame. I have simply set the frame to enabled=false at the start, then back to enabled = true at the end. In the event of an untrapped error, the form would unload anyway, and when reloaded, the frame' s default is enabled = true. Thanks for your suggestion anyway.
Regards
 

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,347
Sorted! As it happens, the togglebutoons are all in a frame. I have simply set the frame to enabled=false at the start, then back to enabled = true at the end. In the event of an untrapped error, the form would unload anyway, and when reloaded, the frame' s default is enabled = true. Thanks for your suggestion anyway.
Regards
 

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,347
Sorted! As it happens, the togglebutoons are all in a frame. I have simply set the frame to enabled=false at the start, then back to enabled = true at the end. In the event of an untrapped error, the form would unload anyway, and when reloaded, the frame' s default is enabled = true. Thanks for your suggestion anyway.
Regards
 

Forum statistics

Threads
1,136,651
Messages
5,677,004
Members
419,667
Latest member
MegEri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top