0

I have an Access form with a command button and a textbox. The button is disabled when the form starts.

I want to enable it when the user enters text and disable it when user removes text.

I want code similar to if txt = "" then button.enabled = false else button enabled = true. I tried that code, but it didn't work.

Gustav
  • 53,498
  • 7
  • 29
  • 55
  • https://stackoverflow.com/questions/29775254/how-disable-a-button-if-there-is-no-text-in-multiple-textboxes-in-vb is for multiple textboxes but you will get the idea. – Tim Williams Sep 23 '20 at 01:10
  • or https://www.teachexcel.com/excel-tutorial/2043/disable-enable-buttons-in-userforms – Tim Williams Sep 23 '20 at 01:16

2 Answers2

0

You'll want to look at the TextBox.Change event as seen here: https://learn.microsoft.com/en-us/office/vba/api/access.textbox.change

Something like the following should point you in the right direction:

Private Sub TextBox1_Change()
    If (TextBox1.Text = vbNullString) Then
        CommandButton1.Enabled = False
    Else
        CommandButton1.Enabled = True
    End If
End Sub

And I personally like using vbNullString when checking for empty strings. You can find more about this by looking at this post: Is there any difference between vbNullString and ""?

Alex
  • 76
  • 2
0

Consider the AfterUpdate event of the textbox:

Private Sub YourTextBox_AfterUpdate()

    Me!YourButton.Enabled = (Nz(Me!YourTextBox.Value) <> "")

End Sub
Gustav
  • 53,498
  • 7
  • 29
  • 55