Clear TextBox when another TextBox is selected

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,
Im not sure if im aproaching this the correct way so could you advise please.

I open my userform which has 5 TextBoxes & a ListBox.
If i click in a TextBox & type a partial value say AB the results are then shown in the ListBox.

It is now that i wish to click in a different TextBox.
Upon clicking in the other TextBox i wish for the other TextBoxes to be all cleared.
Once ive managed to get this working i will then look to also clear the values in the ListBox

I have tried the below but nothing happens at all when i select the other TextBox

Rich (BB code):
Private Sub TextBoxName_Click()
If TextBoxName.Value <> "" Then
TextBoxReg.Value = ""
TextBoxVehicle.Value = ""
TextBoxKeyCode.Value = ""
TextBoxChassisNumber.Value = ""
End If
End Sub

Thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi
If you mean change _Click to _Enter that also did nothing
I mean i click another textbox but the other text box wasnt cleared
 
Upvote 0
The below works but not how i like.

Ive added the code in Red.
I click a textbox & enter a value.
I then click another textbox & enter a value.
The results are shown in the ListBox Then i see the value in the previous TextBox cleared.

So it works BUT i was expecting once i click the next textbox the previous would clear its value Then load results in ListBox

Rich (BB code):
Private Sub TextBoxName_Change()
  TextBoxReg.Value = ""
  TextBoxVehicle.Value = ""
  TextBoxKeyCode.Value = ""
  TextBoxChassisNumber.Value = ""
  Application.ScreenUpdating = True

  TextBoxName = UCase(TextBoxName)
  Dim r As Range, f As Range, cell As String, added As Boolean
  Dim sh As Worksheet
  
  Set sh = Sheets("DATABASE")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 2
    .ColumnWidths = "240;240"
    If TextBoxName.Value = "" Then Exit Sub
    Set r = Range("A6", Range("A" & Rows.Count).End(xlUp))
    Set f = r.Find(TextBoxName.Value, LookIn:=xlValues, LookAt:=xlPart)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        added = False
        For i = 0 To .ListCount - 1
          Select Case StrComp(.List(i), f.Value, vbTextCompare)
            Case 0, 1
              .AddItem f.Value, i
              .List(i, 1) = f.Offset(, 1).Value
              .List(i, 2) = f.Row
              added = True
              Exit For
          End Select
        Next
        If added = False Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Offset(, 1).Value
          .List(.ListCount - 1, 2) = f.Row
        End If
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      TextBoxSearch = UCase(TextBoxSearch)
      .TopIndex = 0
      Else
      MsgBox "NO ITEM WAS FOUND USING THAT INFORMATION", vbCritical, "DATABASE SHEET ITEM SEARCH"
      TextBoxName.Value = ""
      TextBoxName.SetFocus
    End If
  End With
End Sub
 
Upvote 0
Putting the clearing in the Enter Event works for me. I made a test Userform with 4 Textboxes. When it opens, it puts some text in each box.
If I click on Textbox4 (and there is text preexisting in it - which seems to be what you desired), the other three textboxes will clear. If there is no text in tTextbox4, nothing happens.


VBA Code:
Private Sub TextBox4_Enter()
If Me.TextBox4.Value <> "" Then
    Me.TextBox1.Value = ""
    Me.TextBox2.Value = ""
    Me.TextBox3.Value = ""
End If
End Sub

Private Sub UserForm_Initialize()
For X = 1 To 4
    Me.Controls("TextBox" & X).Value = "Test for TextBox" & X
Next X
End Sub

When userform opens:
1643470498936.png

When I click on Textbox4:
1643470531441.png

Now I cleared out Textbox4, add some things to the other textboxes and clicked in Textbox4 , which is now empty - nothing happens.
1643470638977.png
 
Upvote 0
Ive changed the names from 1-4 as shown below but when i try to open the userform i am told could not find the specified object.
What did i do wrong here.



Rich (BB code):
Private Sub UserForm_Initialize()
    For x = 1 To 4
    Me.Controls("TextBox" & x).Value = "Test for TextBox" & x
Next x
End Sub
Private Sub TextBoxName_Enter()
If Me.TextBoxReg.Value <> "" Then
    Me.TextBoxVehicle.Value = ""
    Me.TextBoxKeyCode.Value = ""
    Me.TextBoxChassisNumber.Value = ""
End If
End Sub
 
Upvote 0
Take out the Initialize sub - that was for my sample userform.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top