Unable to change textbox name without code breaking

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,748
Office Version
  1. 2007
Platform
  1. Windows
Morning,
On my form i have a textbox called txtEmpID
I wish to change its name but when i do the code fails.
In the code shown ive also changed the id = part to match but still fails.
Do you see why please ?


This is the code for my userform.


Rich (BB code):
Private Sub txtEmpID_AfterUpdate()
    Dim id As Integer, rowcount As Integer, foundcell As Range
    
    id = txtEmpID.Value
    
    rowcount = Sheets("G INCOME").Cells(Rows.Count, 13).End(xlUp).Row ' THIS IS COLUMN NUMBER WHERE EMP ID LOCATED
    
    With Worksheets("G INCOME").Range("M1:M" & rowcount) ' THIS IS CELL REFERENCE OF WHERE THE TEXT EMP ID IS LOCATED
        Set foundcell = .Find(what:=id, LookIn:=xlValues)
        
        If Not foundcell Is Nothing Then
            TextBox1.Value = .Cells(foundcell.Row, 2)
            TextBox2.Value = .Cells(foundcell.Row, 3)
            TextBox3.Value = .Cells(foundcell.Row, 4)
            TextBox4.Value = .Cells(foundcell.Row, 5)
            TextBox5.Value = .Cells(foundcell.Row, 6)
        
         Else
            TextBox1.Value = ""
            TextBox2.Value = ""
            TextBox3.Value = ""
            TextBox4.Value = ""
            TextBox1.Value = ""
            
        End If
        
    End With
    
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What is the new name you want to use. Also in your Else statement you use the TextBox1.Value = "" Twice the second time I think it should be TextBox5.

As a suggestion you should name each textbox as well it will help identify what the textbox is doing. Example

Textbox1 name code example txtName
Textbox1 name code example txtJobTitle

You mention it is on a form. Do you mean a UserForm and if so what code are using when the form is initialized.
 
Upvote 0
Ive changed the TextBox 1 to 5 now thanks.

Keen to know why i am unable to change it.
If i was to call it Customer ID what would need to be changed in the code for it to work.

Curious more than anything why im unable to change it
 
Upvote 0
You can't use a name with a space in it.

You would also need to alter the declaration line:

Rich (BB code):
Private Sub ThisIsTheTextboxName_AfterUpdate()
 
Upvote 0
Solution
It fails.
I right click that Text box & name it CustomerID
In the code i changed id = txtEmpID.Value TO id = CustomerID.Value

So when i run the code i enter a value in the textbox, say 20 & jot enter.
Then the other textboxes & supposed to get populated from sheet values but nothing happens.

I change CustomerID back to id = txtEmpID.Value & rename box on form & it then works
 
Upvote 0
Now sorted i forgot that part.
Thanks.

Another small isue with same form but will start new post
 
Upvote 0

Forum statistics

Threads
1,223,931
Messages
6,175,465
Members
452,646
Latest member
tudou

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