1
votes

I have a Userform that has a Checkbox. I am able to check the value of it, but it is always False whether it is checked or not.

Update

This is how the UserForm is being called (This is in another UserForm ):

Private Sub AddOutgoingbtn_Click()
    With New AddIncomingForm
        .TopBottom.Value = False
        .Show
        .Repaint
    End With
End Sub

End Update

I created a sub to look for a change in the value like:

Sub TopBottom2_Change()
    With AddOutgoingForm
        If .TopBottom2.Value = True Then TopBottom = True
        If .TopBottom2.Value = False Then TopBottom = False
    End With
End Sub

But no matter what I do the .TopBottom2.Value is always False.

I've put a breakpoint on the With line so that I know it is hitting this Sub, then I step through it each time. I open the UserForm and check the box, step through and the value is False, then I uncheck the box and step through. The value is still False.

I am not setting the value in any other way with VBA. I am checking the value in the UserForms code, not in any other place.

I have an If in a Calculation Module that is looking at this value for when it is true or falses, but it is always false.

Here are all the Properties of the Checkbox:

TopBottom Properties

2
Would be nice if you could edit the question to show exactly how you're displaying the form - as explained in my answer (and the linked blog post), it matters very very much.Mathieu Guindon

2 Answers

1
votes
With AddOutgoingForm

That's referring to the form's default instance, which may or may not be the instance that's currently being displayed.

You have two options:

  1. At the call site, instead of doing this (or something similar):

    With New AddOutgoingForm
        .Show
        '...
    End With
    

    Do this:

    AddOutgoingForm.Show
    '...
    

    That way you'll be working with the default instance and the checkbox value-check should work.

    ...but IMO that's a very very bad idea, because then your form contains code that will only ever work when you're showing the default instance.


  1. Leave the call site alone, and NEVER refer to the default instance of a UserForm inside that form's code-behind. In other words change With AddOutgoingForm for With Me.

    The Me keyword refers to the current instance - and that is what you want. Doing this will make the form work regardless of what the call site does.

    Alternatively, just drop the With block altogether: With Me wouldn't be doing anything useful here.

1
votes

I will assume the checkbox is indeed on your user form so just use me or you could use

AddOutgoingForm.TopBottom2.Value

But let me ask you, where is TopBottom and what is it a Boolean variable or another Checkbox? Also on the same form? The Subs are also all in the form? You have to be calling the form from somewhere so be careful between the worksheet, workbook, module variables. Is TopBottom a Global variable to the whole project (in a module called Global_Variables with Public in front of it perhaps?) You may not have access to TopBottom from inside of your form if you are not passing anything in or out.

Private Sub TopBottom2_Change()
    If (Me.TopBottom2.Value) = True Then 
         Me.TopBottom.Value = True
    Else
         Me.TopBottom.Value = False
    End If
 End Sub

Cheers, -WWC