Problem with mousedown handler for a checkbox

tfurnivall

New Member
Joined
Jan 15, 2012
Messages
48
BACKGROUND
I have an application which allows a user to create various reports. The underlying data for the application is always available for update. When the data has changed, the data is no long 'valid' for reporting until a particular procedure (the Analyze) procedure has been run. I have a configuration item (ReportDataIsValid) that I can set to true or false from anywhere in the application. This allows the state of the data to be preserved across different invocations of the application. When the application is opened, the state of this item is checked, and the application interface is configured appropriately:

Code:
if ReportDataIsValid then
   '   Make Report tabs visible, and set the configuration check box to 'true'
else
   '   Make the report tabs invisible and set the configuration check box to 'false'
endif

Similarly, every time the Analyze procedure is run it sets the configuration item and the checkbox to true, and every time an Update procedure is run. it sets the configuration item, and the checkbox to false. So far, so good. It works!

CONFESSION
Being the sort of developer who likes to allow myself the luxury of a short cut, I want to be able to IN-validate (or RE-validate) the data without running either an Update or the Analyze procedure.

This led to the following code for the checkbox in the Configuration form:
Code:
Private Sub chkValidReportData_Click()

'   We can click to IN-validate the report data, but we can ONLY
'   validate the report data by running the Analyze procedures.
'   If we click the control 'into' a true state, then immediately set it to false.

If Not ProcessEvents Then
   Exit Sub
End If
ProcessEvents = False
'   Now set the things we want without fear of events being handled
ThisWorkbook.SetValidReportData (False)
mpDashboard.Pages("pgReports").Visible = False
Me.chkValidReportData = False
'   Resume event handling
ProcessEvents = True

End Sub

So, any time the button is clicked, regardless of the state of the little check-mark on the screen, the following things happen:
1) The configuration item is set to false, so the next time the application starts up it will maintain this 'not-valid' state
2) The Report tab on the main form selector is set to invisible. I.e. the reports can not be accessed.
3) The little checkmark is cleared.

All of this is done with event handling turned off, so that we do not get into a race condition. It works.

THE REAL PROBLEM
Now for the heart of the problem. I want to be able to RE-validate the data condition. After all, it really hasn't changed, I'm simply developing and testing different aspects of the application. So, to RE-validate, I want to use Ctrl/Alt and then a click on the checkbox control (chkReportDataIsValid). No provision for testing the state of the Ctrl and Alt keys in a regular click, so I switch to MouseDown for the same event. This is a little more complex:
Code:
Private Sub chkValidReportData_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

'   Allows us to see if we are doing a special "RE-validate Reports" (for testing purposes

Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Worksheets("Configuration")

Debug.Print "Valid Reports:     "; Me.chkValidReportData, "Shift="; Shift
If Not ProcessEvents Then
   Exit Sub
End If

ProcessEvents = False
If Shift = 6 Then   '   Ctrl + Alt
   ThisWorkbook.SetValidReportData (True)
   mpDashboard.Pages("pgReports").Visible = True
   Me.chkValidReportData.Value = True
   Debug.Print "Set Valid Reports: "; Me.chkValidReportData.Value
Else
   ThisWorkbook.SetValidReportData (False)
   mpDashboard.Pages("pgReports").Visible = False
   Me.chkValidReportData.Value = False
   Debug.Print "Set Valid Reports: "; Me.chkValidReportData.Value
End If
ProcessEvents = True

End Sub

This is exactly the same code if we do not have the 'magic' shft value, and exactly the reverse if we do. For Ctrl/Shift-Click the folloing happens:

1) The configuration item is set to True
2) The Reports tab is made visible (and thus accessible)
3) The little check mark is turned on in the control.

Again, all of this is with event handling disabled. (I've also commented out the code in the original handler, so there is no contention there!

However, IT DOESN'T WORK!

Here is what I get in the Immediate window when I try it - using two clicks on each state of the control (twowith no magic shift, two WITH a magic shift):
Code:
Valid Reports:     True     Shift= 0 
Set Valid Reports: False
Valid Reports:     True     Shift= 0 
Set Valid Reports: False
Valid Reports:     True     Shift= 6 
Set Valid Reports: True
Valid Reports:     False    Shift= 6 
Set Valid Reports: True

I'm missing something somewhere, but I can't figure it out. Any takers?

Tony
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Have you stepped through the code to try and determine exactly what's happening?
 
Upvote 0
Yes - if I use debug and step through, it all works fine. I have tried adding a pause of about 1 second (.00001) after setting the checkbox, but it doesn't work consistently.

This, of course, only makes it more frustrating. It's almost certainly a timing issue, but bypassing event handlers should remove that as part of the problem. I'm hoping that someone can find something glaringly obvious, rather than hypothesizing about deep-down internals issues :(

T.
 
Upvote 0
Tony

It's pretty hard to help with this sort of thing without a workbook.

Any chance you could upload it somewhere like Box.net and posting a link?
 
Upvote 0

Forum statistics

Threads
1,216,574
Messages
6,131,499
Members
449,653
Latest member
aurelius33

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
Back
Top