0
votes

I have a main form with several tab controls with subforms in them. On the first tab or subform I have a "checkbox 1", which shall be:

  • checked if "textbox 1" is not empty
  • unchecked if "textbox 1" is empty

The code is directly put into the Class Object of "subform 1", that's why I thought I could use Me.

Here is my code, but I always get error messages :(

Private Sub Ctltextbox_1_AfterUpdate()
    If Len(Ctltextbox_1.Value) = 0 Then
        Me.checkbox_1.Value = 0
    Else
        Me.checkbox_1.Value = -1
    End If
End Sub

That way I am getting the

Run-time error '2448': You can't assign a value to this object.

On the line that attempts to assign -1 to Me.checkbox_1.Value.

2
What's the error message you get? That may help us. Also, you can always explicitly reference the subform itself. - Scott Holtzman
It is not clear what you need. Check the check box if the text box is not empty and uncheck when the text box is empty? - Wiktor Stribiżew
Right now with the "Me." way: You can"t assign a value to this object. It's always debugging on "Me.checkbox_1.Value= -1 ". - Karen
@Wiktor: Exactly. The code is also working this way on a mainform, but not in the subform. - Karen
I would avoid the underscore in identifier names - if you noticed, VBA uses that character to identify procedures that implement an interface or handle an event. Seems a -1 literal isn't valid. Is there a vbChecked constant in scope? Have you tried True instead? - Mathieu Guindon

2 Answers

0
votes

Try this. It is working for me. I moved it to the Ctltextbox_1_Change action. This will call the function whenever it is changed and will catch when you have nothing in the box as well. I also changed your values to True and False.

Please try this and let me know.

Private Sub Ctltextbox_1_Change()
    If Len(frmSub.Ctltextbox_1.Value) = 0 Then
        frmSub.CheckBox_1.Value = False
    Else
        frmSub.CheckBox_1.Value = True
    End If
End Sub

you can also solve this in one line as mentioned below.

Private Sub Ctltextbox_1_Change()
    frmSub.CheckBox_1.Value = IIf((Len(frmSub.Ctltextbox_1.Value) = 0), False, True)
End Sub
0
votes

Ok, the simple answer is: you can not use the expression Len()= 0 for a not numeric field! So my working code for the subform is now:

Private Sub Textbox1_AfterUpdate()
If IsNull(Textbox1.Value) = False Then
    Me.checkbox1.Value = True
Else
    Me.checkbox1.Value = False
End If

End Sub

Thank you all for your help! :)