Results 1 to 5 of 5

Thread: [RESOLVED] Text Box not clearing

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    161

    Resolved [RESOLVED] Text Box not clearing

    I have the following code. When the Form launchs sometimes the text box's dont clear on form startup? Can anyone see why?


    Code:
    Dim Address As Worksheet
    
    ' Clear Control Each Individual Control Source Range
    Private Sub cmdClear1_Click()
    Me.txtAddress1 = ""
    End Sub
    
    Private Sub cmdClear2_Click()
    Me.txtAddress2 = ""
    End Sub
    
    Private Sub cmdClear3_Click()
    Me.txtAddress3 = ""
    End Sub
    
    Private Sub cmdClear4_Click()
    Me.txtAddress4 = ""
    End Sub
    
    Private Sub cmdClear5_Click()
    Me.txtcity = ""
    End Sub
    
    Private Sub cmdClear6_Click()
    Me.txtcounty = ""
    End Sub
    
    Private Sub cmdClear7_Click()
    Me.txtpostcode = ""
    End Sub
    
    Private Sub cmdClear8_Click()
    Worksheets("Address").Range("B8").Value = ""
    End Sub
    
    ' Load CatID Panel
    Private Sub cmdEnterCont_Click()
         Unload Me
            Sheets("Address").Select
      
    End Sub
    
    ' Shows CmdClear box for required Text box
    Private Sub txtAddress1_Enter()
     cmdClear1.Enabled = (txtAddress1.Text <> "")
     cmdClear1.Visible = True
     cmdClear2.Visible = False
     cmdClear3.Visible = False
     cmdClear4.Visible = False
     cmdClear5.Visible = False
     cmdClear6.Visible = False
     cmdClear7.Visible = False
     cmdClear8.Visible = False
    End Sub
    
    
    
    
    Private Sub txtAddress2_Enter()
     cmdClear2.Enabled = (txtAddress2.Text <> "")
     cmdClear1.Visible = False
     cmdClear2.Visible = True
     cmdClear3.Visible = False
     cmdClear4.Visible = False
     cmdClear5.Visible = False
     cmdClear6.Visible = False
     cmdClear7.Visible = False
     cmdClear8.Visible = False
    End Sub
    
    
    Private Sub txtDAddress3_Enter()
     cmdClear3.Enabled = (txtAddress3.Text <> "")
     cmdClear1.Visible = False
     cmdClear2.Visible = False
     cmdClear3.Visible = True
     cmdClear4.Visible = False
     cmdClear5.Visible = False
     cmdClear6.Visible = False
     cmdClear7.Visible = False
     cmdClear8.Visible = False
    End Sub
    
    
    Private Sub txtAddress4_Enter()
     cmdClear4.Enabled = (txtAddress4.Text <> "")
     cmdClear1.Visible = False
     cmdClear2.Visible = False
     cmdClear3.Visible = False
     cmdClear4.Visible = True
     cmdClear5.Visible = False
     cmdClear6.Visible = False
     cmdClear7.Visible = False
     cmdClear8.Visible = False
    End Sub
    
    Private Sub txtcity_Enter()
     cmdClear5.Enabled = (txtcity.Text <> "")
     cmdClear1.Visible = False
     cmdClear2.Visible = False
     cmdClear3.Visible = False
     cmdClear4.Visible = False
     cmdClear5.Visible = True
     cmdClear6.Visible = False
     cmdClear7.Visible = False
     cmdClear8.Visible = False
    End Sub
    
    Private Sub txtcounty_Enter()
     cmdClear6.Enabled = (txtcounty.Text <> "")
     cmdClear1.Visible = False
     cmdClear2.Visible = False
     cmdClear3.Visible = False
     cmdClear4.Visible = False
     cmdClear5.Visible = False
     cmdClear6.Visible = True
     cmdClear7.Visible = False
     cmdClear8.Visible = False
    End Sub
    
    
    
    Private Sub txtpostcode_Change()
    Worksheets("Address").Range("B8").Value = "'" & txtpostcode
    End Sub
    
    Private Sub txtpostcode_Enter()
     cmdClear7.Enabled = (Worksheets("Address").Range("B8").Value <> "")
     cmdClear1.Visible = False
     cmdClear2.Visible = False
     cmdClear3.Visible = False
     cmdClear4.Visible = False
     cmdClear5.Visible = False
     cmdClear6.Visible = False
     cmdClear7.Visible = True
     cmdClear8.Visible = False
    End Sub
    
    
    Private Sub txtCountry_Enter()
    
     cmdClear1.Visible = False
     cmdClear2.Visible = False
     cmdClear3.Visible = False
     cmdClear4.Visible = False
     cmdClear5.Visible = False
     cmdClear6.Visible = False
     cmdClear7.Visible = False
     cmdClear8.Visible = True
    End Sub
    
    
    Private Sub UserForm_Activate()
    
    
    
    Me.txtAddress1 = ""
    Me.txtAddress2 = ""
    Me.txtAddress3 = ""
    Me.txtAddress4 = ""
    Me.txtcity = ""
    Me.txtcounty = ""
    Worksheets("Address").Range("B8").Value = ""
    Me.txtCountry = ""
    cmdClear1.Visible = True
    cmdClear2.Visible = False
    cmdClear3.Visible = False
    cmdClear4.Visible = False
    cmdClear5.Visible = False
    cmdClear6.Visible = False
    cmdClear7.Visible = False
    cmdClear8.Visible = False
        
    End Sub
    
    ' Selects the Correct CATID sheet
    Private Sub UserForm_Initialize()
    Sheets("address").Select
    Me.txtAddress1 = ""
    Me.txtAddress2 = ""
    Me.txtAddress3 = ""
    Me.txtAddress4 = ""
    Me.txtcity = ""
    Me.txtcounty = ""
    Worksheets("Address").Range("B8").Value = ""
    Me.txtCountry = ""
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    
        If CloseMode = vbFormControlMenu Then
            MsgBox "Please use the Next button to close the form", vbCritical
            Cancel = True
        End If
    
    End Sub

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Text Box not clearing

    Clear all textboxes at design time or
    Change Private Sub UserForm_Activate() to Private Sub UserForm_Initialize()
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Text Box not clearing

    And why do you have separate buttons for each textbox? Why not clear them all with just one button click?

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Text Box not clearing

    Going thru each textbox in design time and clearing them can be a pain sometimes.

    The best way is to use code to loop and clear off all textboxes as shown below

    Code:
    Private Sub UserForm_Initialize()
        Dim cCont As Control
        
        'loop thru all textboxes and clear them
        For Each cCont In Me.Controls
            If TypeName(cCont) = "TextBox" Then
                cCont.Text = ""
            End If
         Next cCont
    End Sub
    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    161

    Re: [RESOLVED] Text Box not clearing

    Used that and seems to Clear everything as expected. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width