Checkbox reset - prevent loop

PaulZak

Board Regular
Joined
Aug 22, 2006
Messages
105
I have a checkbox on form. When user checks or unchecks it (changes it), I provide a msg prompt yes/no asking if user wants to proceed. If the user selects NO, I want to revert the checkbox back to original value (checked or unchecked).

For example: User checks box that was unchecked, user selects not to proceed when prompted, checkbox returns to unchecked value.

Problem is I get a single loop when code returns the box to original value (unchecked) and the message prompt appears again.
1) [BeforeUpdate] event: I am grabbing the original value and storing in a variable.
2) [Click] event: I'm running Yes/No prompt and code the resets the box to original value (chkgox.value = x). (I've also tried placing it in [AfterUpdate] event, instead but not any better.)

The only way I can see to solve this is to create/use a "runcode" variable (yes/no flag) to prevent the [Click] event's code from running the second time (after checkbox value is set back to original value by code).

Maybe I'm missing some obvious other approach. Is there a simpler way (i.e., using different events)???
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Can you post the code you have?

If you're using a worksheet event, you should include something along the lines of:

Code:
Application.EnableEvents = False

at the start and

Code:
Application.EnableEvents = True

at the end.
 
Upvote 0
Thanks for the quick reply. Checkbox and code exist in a UserForm. I did try your idea (which sounded familiar) but it didn't work. See following. Any other ideas???

If MsgBox("Your changed the checkbox" & vbCr & vbCr & _
"Do you want to proceed?", vbYesNo, "CHANGE USER TYPE?") = vbNo Then
Application.EnableEvents = False 'Tried per suggestion
Me.CkBox_RunStdUser.Value = gbOriginalUserTypeCkBoxValue
Application.EnableEvents = True
Exit Sub
End If
 
Upvote 0
Enabling or disabling events won't work on a userform. With the exit sub statement, I don't see why it would loop.
 
Upvote 0
I've stepped through code. It loops as soon as the code runs that changes checkbox back to original value (because user does not want to proceed). So I guess it's as if the user unchecked the box and so VBA sees it as another click. I agree this seems odd; there's an update or change but no actual "click" involved.

So, back to my goal. Isn't this a reasonable design standard: User clicks checkbox, app asks if user meant to do that, user response to Yes/No msg with no ("Opps, my bad") and the app returns box back to original value/state? (Otherwise, user clicks no and subsequent code doesn't run, which is correct, but box stays checked which is confusing. Also, this doesn't allow user to check box later, if they are ready at that time.)
 
Last edited:
Upvote 0
What exactly are you trying to do?

Perhaps there is another approach.

eg not doing the check using the checkbox's own events
 
Upvote 0
Along Norie's line, this is kind of a sticky practice. If you think about programs you use daily, how many times do you get warned when you click a checkbox? It's kind of intrusive.

But, to answer your question, I assume you're using the Click event? Why not tie your Yes/no question to the Mouse Down event? Head the problem off before the change even gets made.
 
Upvote 0
I think Chris has a good idea there:)


I was trying to think of another checkbox event you could use but came up blank.
 
Upvote 0
Brilliant, ChrisM! Using this event works as desired.

I don't use this event much so it wasn't on my radar.

Thanks, all.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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