check box won't uncheck

C

Chuck

I've got some checkboxes on a user form that check/uncheck and
enable/unenable depending on each other's values.

Trouble is, in the following code the .chkAndByPost.Value won't change to
false even though the conditions are met for it to do so and the following
line of code (.chkAndByPost.Enabled = False) runs correctly.

Can anyone tell my why my code won't change .chkAndByPost.Value to False in
the following code? Many many thanks for any feedback...

Sub tickIsToFax()

With objForm

.txtToFaxNumber.Enabled = .chkIsToFax.Value

If .chkIsToFax.Value = True Then
.chkAndByPost.Enabled = True
.txtToFaxNumber.BackColor = RGB(256, 256, 256) 'white
.txtToFaxNumber.SetFocus
Else
Select Case .chkIsToEmail.Value
Case True
.chkAndByPost.Enabled = True
Case False
' Even though this code runs,
' the value won't change to False
.chkAndByPost.Value = False
.chkAndByPost.Enabled = False
End Select
.txtToFaxNumber.BackColor = RGB(192, 192, 192) 'grey
End If

End With

End Sub
 
J

Jean-Guy Marcil

Chuck was telling us:
Chuck nous racontait que :
I've got some checkboxes on a user form that check/uncheck and
enable/unenable depending on each other's values.

Trouble is, in the following code the .chkAndByPost.Value won't
change to false even though the conditions are met for it to do so
and the following line of code (.chkAndByPost.Enabled = False) runs
correctly.

Can anyone tell my why my code won't change .chkAndByPost.Value to
False in the following code? Many many thanks for any feedback...

Sub tickIsToFax()

With objForm

.txtToFaxNumber.Enabled = .chkIsToFax.Value

If .chkIsToFax.Value = True Then
.chkAndByPost.Enabled = True
.txtToFaxNumber.BackColor = RGB(256, 256, 256) 'white
.txtToFaxNumber.SetFocus
Else
Select Case .chkIsToEmail.Value
Case True
.chkAndByPost.Enabled = True
Case False
' Even though this code runs,
' the value won't change to False
.chkAndByPost.Value = False
.chkAndByPost.Enabled = False
End Select
.txtToFaxNumber.BackColor = RGB(192, 192, 192) 'grey
End If

End With

End Sub

I tried your code "as posted" by creating 3 checkboxes and one textbox. I
used your control names and added a command button to call "tickIsToFax".
Then I checked some of the textboxes and debugged the code step by step.
Everything ran as expected.
There is probably something else going on.
How are you calling tickIsToFax?
Do you have Click events for each of the controls, in particular the
chkAndByPost?
Finally, is the code you posted just a small part of the relevant form
code?
(I am asking also because I see that you set the backcolor of the textbox
to white if it is enabled, but if it gets disabled by the first line of the
code you do not switch it back to grey. So I am guessing that you have more
code and that maybe this other code is responsible for the behaviour you
observed.)

By the way, I think it is better to use colour constants instead of RGB,
unless you are also specifying the dialog box face colour. If you are not,
the dialog box takes the default "Button Face" colour from the system. When
you disable control, normally its default colour will be the same as the
dialog box face. If, like on my system, the user has changed the default
colour scheme, chances are that the Button Face colour is not grey. So when
you disable a control and force it to grey, it will look strange on those
customized system. The button face colour constant is;
&H8000000F
You can get all the constants by selecting a colour from the property pane
in the VBE when you select a control. If you select, let's say Disabled
Text, you get a hex number. Use that in your code, in this case: &H80000011.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chuck

Hi Jean-Guy

The code I posted is indeed part of a lot of other code and there does seem
to be some sort of conflict with the change and/or click events for
..chkAndByPost as well as a related checkbox.

I'm not sure why but I was able to solve my problem by breaking out the
..chkAndByPost enabling/disabling and value changing into a separate sub, like
this:

Sub tickIsToFax()

With objForm

.txtToFaxNumber.Enabled = .chkIsToFax.Value

If .chkIsToFax.Value = True Then
.txtToFaxNumber.BackColor = &H80000005 'window background
.txtToFaxNumber.SetFocus
Else
.txtToFaxNumber.BackColor = &H8000000F 'button face
End If

End With

'this sub successfully changes
'.chkAndByPost.Value
EnableDisableAndByPost

End Sub

Sub EnableDisableAndByPost()

With objForm

If .chkIsToFax.Value = True Or _
.chkIsToEmail.Value = True Then
.chkAndByPost.Enabled = True
End If

If .chkIsToFax.Value = False And _
.chkIsToEmail.Value = False Then
.chkAndByPost.Value = False
.chkAndByPost.Enabled = False
End If

End With

End Sub

Again, I don't know why that works but there must be some logical reason for
it. My head hurts from trying to figure it out so I'm just going to let it
go for now but if you can see any reason why breaking the behaviour into a
separate sub would work, please let me know.

Thanks for the tip about colour constants -- looks much better!

Chuck
 
J

Jean-Guy Marcil

Chuck was telling us:
Chuck nous racontait que :
Hi Jean-Guy

The code I posted is indeed part of a lot of other code and there
does seem to be some sort of conflict with the change and/or click
events for .chkAndByPost as well as a related checkbox.

I'm not sure why but I was able to solve my problem by breaking out
the .chkAndByPost enabling/disabling and value changing into a
separate sub, like this:

Sub tickIsToFax()

With objForm

.txtToFaxNumber.Enabled = .chkIsToFax.Value

If .chkIsToFax.Value = True Then
.txtToFaxNumber.BackColor = &H80000005 'window background
.txtToFaxNumber.SetFocus
Else
.txtToFaxNumber.BackColor = &H8000000F 'button face
End If

End With

'this sub successfully changes
'.chkAndByPost.Value
EnableDisableAndByPost

End Sub

Sub EnableDisableAndByPost()

With objForm

If .chkIsToFax.Value = True Or _
.chkIsToEmail.Value = True Then
.chkAndByPost.Enabled = True
End If

If .chkIsToFax.Value = False And _
.chkIsToEmail.Value = False Then
.chkAndByPost.Value = False
.chkAndByPost.Enabled = False
End If

End With

End Sub

Again, I don't know why that works but there must be some logical
reason for it. My head hurts from trying to figure it out so I'm
just going to let it go for now but if you can see any reason why
breaking the behaviour into a separate sub would work, please let me
know.

It is hard to tell without seeing the whole beast, but I know that if some
controls have Change or Click events and that you refer to the value of
those controls in an Initialize event (or some other event), then it is as
if the control had been clicked or changed and the corresponding event is
fired. I recently built a fairly complicated/dynamic dialog box, Not knowing
how to avoid that behaviour, I decided to declare a public Boolean at the
form level, set it to false at the beginning of the initialize event, then I
proceeded to set my controls as I wanted them. For each control event that
was called indirectly by the Initialize event I added a "If Not MyBoolean
Then Exit Sub" so that those events would not fire during the initialize
part. Then at the end of the initialize event I set my Boolean to true.
I don't know if this is the right way of doing things, but it did prevent
all the Click/Change etc. events from firing during the initialize period.
Sometimes in the code instead of setting a control value to true or false, I
called the Click event directly so it also did all the
colour/enabling/Locking I needed done when a control changed value.
Thanks for the tip about colour constants -- looks much better!

Glad I could help.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chuck

The problem checkboxes did have subs at their click events which called subs
in modules outside the form module. When I disabled those on-click calls to
outside subs and moved the condition testing code to the change event within
the form module itself, that seemed to help cut down on flickering I was
getting when clicking the checkboxes and I suspect it also has something to
do with why my revised code works now. I considered the boolean approach as
well and am glad to hear it works for you. I'll try it out myself to see if
it helps streamline my code (I'm trying to keep code out of the form module
just to see if it's work-able as a design principle).
 
C

Chuck

Follow up in case anyone's interested (long shot I know): I deleted the code
from the checkboxes' click events and moved the code that was in the change
events to subs in a separate module. No problems with the checkboxes doing
what I want. So there was some odd conflict going on with the click events
in combination with the code that I extracted into the EnableDisableAndByPost
sub (see below). For whatever reason it works now and I've got all my code
(except for application.run statements) out of my form module. Happy day.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top