View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Mudd[_2_] Mudd[_2_] is offline
external usenet poster
 
Posts: 5
Default Clearing TextBoxes needs 2 clicks

Ken,

I was surprised that you needed two clicks.
To test your problem, I created a form with two text boxes and a command
button to clear them using similar code to that which you supplied (I didn't
use a With construct but that worked just the same anyway);
Private Sub CommandButton1_Click()
TextBox1.Value = ""
TextBox2.Value = ""
End Sub
This cleared both text boxes with one click.
Have you stepped through the macro with the debugger?
Perhaps something is happening when your code is entering null strings to
the spreadsheet cells.


Ken McLennan wrote in message
.. .
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