Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically Disable Click Event or Private Sub in MS Word 1

Status
Not open for further replies.

xentaur

Programmer
Nov 17, 2003
35
0
0
AU
Task:
4 checkboxes. The checking/unchecking of each box needs to update the values of the other three.

Problem:
the code for each checkbox that changes the value of the other three is firing the onClick event for the other three checkboxes.

EnableEvents=False is not recognised in word, only excel.

Have tried also using checkbox form fields instead of MS Forms Checkbox Control but this presents other problems as the macro event on a formfield checkbox will fire only on enter or exit. If the user checks it the form will fire once with the OnEnter but unless they move to another field before changing that value the macro wont fire again.

Checkbox controls can provide an immediate result, but unless I can programmatically disable the click events there is a serious amount of vicious circling going on.

Code:
Private Sub chk01_click()
With ThisDocument
.chk01.Value = True: .chk02.Value = False: .chk03.Value = False: .chk04.Value = False
End With
End Sub

Private Sub chk02_click()
With ThisDocument
.chk01.Value = False: .chk02.Value = True: .chk03.Value = True: .chk04.Value = False
End With
End Sub

Private Sub chk03_click()
With ThisDocument
.chk01.Value = False: .chk02.Value = True: .chk03.Value = True: .chk04.Value = False
End With
End Sub

Private Sub chk04_click()
With ThisDocument
.chk01.Value = False: .chk02.Value = True: .chk03.Value = False: .chk04.Value = True
End With
End Sub

any assistance greatly appreciated.

Cheers
 
Someone tought me this trick a while ago. Instead of using the Application.EnableEvents flag in Excel, set up a variable that holds a Long data type. Then, when you want to disable the events, add 1 to it. When you want to re-enable, subtract one. You then wrap every sub with a check:

Code:
Dim lEvents as Long

Sub Test()
lEvents = lEvents + 1
ThisDocument.chk01.Value = False
lEvents = lEvents - 1
End Sub

Private Sub chk01_click()
If lEvents > 0 Then
   'do nothing
Else
lEvents = lEvents + 1
With ThisDocument
.chk01.Value = True: .chk02.Value = False: .chk03.Value = False: .chk04.Value = False
End With
lEvents = lEvents -1
End If
End Sub

The beauty of this method is that no event code will run until you get back to zero. They first key is making sure that every event routine checks to see if it should work or not. The second is deciding if you should increment the lEvents counter in an event code or not.

Hope this helps,

Ken Puls, CMA
 
Sorry, I got that second one backwards. Doh!

Should be:
Code:
Private Sub chk01_click()
If lEvents > 0 Then
   'run your event code
   lEvents = lEvents + 1
   With ThisDocument
     .chk01.Value = True: .chk02.Value = False: .chk03.Value = False: .chk04.Value = False
   End With
   lEvents = lEvents -1
Else
   'Counter is greater than 0, so events turned off
   'do nothing
End If
End Sub

Ken Puls, CMA
 
Wait a minute... no I didn't! Sorry xentaur.. it's been a long day here and I'm turning into an idiot!

If lEvents is greater than zero, then the events have been turned off. If lEvents = 0, then events should run.

Also, this will work in Word, Excel or whatever. I'm primarily an Excel programmer, but have used this technique in many apps.

I'm going to bed now, I think.

Ken Puls, CMA
 
Nice Ken.

Learn something every day. I did not know that setting a control .Value fired the Click event for the control. Mind you, often I do not have anything explicitly written for the Click event, so nothing happens. This also means that the _Change event is also being fired. I can understand Chanmge being fired, but why Click??

Gerry
My paintings and sculpture
 
Absolutely Brilliant!

I only wish swearing were permitted here as I don't feel I can properly express my sincere gratitude without an expletive or two.

Great to know someone who can properly discipline a misbehaving form.

Cheers

 
er, what you want is called a radio button.

a checkbox control is not only reinventing the wheel, but could confuse (admittedly rather dense) users.


mr s. <;)

 
Hi again, Xentaur,

Thanks for your feedback on this. Your enthusiasm encouraged me to make another article out of it for my site:


It starts with a discussion about managing Excel's ScreenUpdating property, but shows at the end how to adapt it to the userform event method. Kind of backwards from the way it was developed, but I liked my ScreenUpdating example better. :)

Cheers!

Ken Puls, CMA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top