View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ken McLennan[_3_] Ken McLennan[_3_] is offline
external usenet poster
 
Posts: 90
Default Clearing TextBoxes needs 2 clicks

G'day there again, One & All.

I'm back again with the latest problem to catch me out.

I have a simple userform with 2 Texboxes (TextBox1 & TextBox2)
which are used to enter string values to 2 cells on my sheet. That bit
works fine, but I also have a CommandButton titled "Clear" which is
supposed to clear the values and enter null strings ("") to those cells.

The command button does what I want, however I need to click it
twice to clear the two textboxes & cells. The first click clears the
first textbox, and the second click clears the next. The code I'm using
is this:

Private Sub CommandButton3_Click()

With UserForm1
.TextBox1.Value = ""
.TextBox2.Value = ""
End With

End Sub

I've tried different variations including:

Private Sub CommandButton3_Click()

Dim ctrl As Control
For Each ctrl In Controls
If Left(ctrl.Name, 7) = "TextBox" Then
ctrl.Default = ""
End If
Next

End Sub

(This gave a type mismatch error at the ctrl.Default = "" line,
which surprised me a little as the Help file tells me that .Value is the
default for a textbox).

I think the 'With' routine is probably the better way to go, but I
can't figure out why I need one click per textbox.

Has anyone come across this before? or am I missing something so
obvious I should hide under the bedclothes to avoid the embarassment?

See ya
And thanks for all the help I keep getting

Ken McLennan
Qld, Australia