2010-10-12, 12:20 #1
- Join Date
- Nov 2004
- Wilmington, North Carolina, USA
- Thanked 0 Times in 0 Posts
I'm trying to use a button's 'click' event to reset all the text boxes on my form. I've tried many iterations of the following:
Dim txt As Control For Each txt In Me.Controls If TypeOf txt Is TextBox Then txt.Text = "" Next
It doesn't do anything. If I substitute the clearing action with MsgBox("test"), I still don't get anything, so I feel like for some reason the code isn't actually looking at the collection of controls on the form, but I don't know how to fix it. I could simply list the 27 text boxes and set them all = Nothing, but that's bad practice.____________________________
"If you spend more on coffee than on IT security, then you will be hacked. What's more, you deserve to be hacked." -Richard Clarke
Subscribe to our Windows Secrets Newsletter - It's Free!
Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!
+ Get this BONUS — free!
Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!
2010-10-12, 15:09 #2
- Join Date
- Mar 2004
- Manning, South Carolina
- Thanked 704 Times in 642 Posts
Dim txt As Control For Each txt In Me.Controls If Left$(txt.name,2) = "tb" Then txt.Text = "" Next
2010-10-25, 01:54 #3
- Join Date
- Dec 2000
- New York, NY
- Thanked 27 Times in 26 Posts
Your original code works just fine when I test it here (as does Retired Geek's).
If you're substituting a simple MsgBox statement, and the message box doesn't appear when you click the button, that doesn't mean that the controls code isn't working - it means that the code in the button Click procedure isn't running at all.
The most common cause for code in a button Click procedure to not run, is if the name of the button, as referenced in the procedure signature, is not identical to the actual name of the button.
One way this can happen is if you create a new button - which gets a default name like CommandButton1 - and you then create a button click procedure for that button - so the procedure will be named "Private Sub CommandButton1_Click()"
If you later go and rename the button to something meaningful like "cmdClearTextboxes", but don't change the name in the Click procedure's signature, then the Click procedure code won't run when you click the button.
Any chance something like that is happening?