Home > Back-end >  Enabling and disabling textbox
Enabling and disabling textbox

Time:10-27

I am completely new to VBA so I am sorry if this seems like a silly question. I'm trying to have the TextBox next to the "Other date:" set so that you can only write in it if the "other date" button is selected.

https://img.codepudding.com/202110/4e1de0d0353b492ba25e40a14f495eff.png

so far this is what I have which obviously is not working :)

Private Sub otherDateTextBox_Change()

If Me.todayButton.Value = True Then
    Me.otherDateTextBox.Locked = True
ElseIf Me.tomorrowButton.Value = True Then
    Me.otherDateTextBox.Locked = True
ElseIf Me.otherDateButton.Value = True Then
    Me.otherDateTextBox.Locked = False

End If

End Sub

Any help would be appreciated

CodePudding user response:

You are using the wrong event since the state of otherDateTextBox is determined by the value of otherDateButton so you should use the Change event of otherDateButton.

You can use the Enabled property to "lock" the text box so that it is not selectable and not editable (will show as grey out)

Private Sub otherDateButton_Change()
     otherDateTextBox.Enabled = otherDateButton.Value
End Sub

CodePudding user response:

Use the below code to disable the TextBox when the form is loaded/activated

Private Sub UserForm_Activate()
    TextBox1.Enabled = False
End Sub

So it will be disabled by default. After that add the below code to enable the TextBox when Option1 is True or False.

Private Sub OptionButton1_Change()
    If OptionButton1.Value = True Then
        TextBox1.Enabled = True
    Else
        TextBox1.Enabled = False
    End If
End Sub

Also you can clear out the text in TextBox before disabling it by

Private Sub OptionButton1_Change()
    If OptionButton1.Value = True Then
        TextBox1.Enabled = True
    Else
        TextBox1.Text = ""
        TextBox1.Enabled = False
    End If
End Sub
  • Related