View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default moving within forms

Hi,

I assume you have a button on your userform that closes it and does
something with the values. This code would be called by that button. It loops
through each textbox on the form and ensure the value is numeric and in
addition if textbox 3 is populated it checks if textbox1 is

Private Sub CommandButton1_Click()
Dim cCont As Control
For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" Then
With cCont
If Not IsNumeric(.Value) And .Value < vbNullString Then
MsgBox cCont.Name & " value must be numeric"
.Value = vbNullString
End If
End With
End If

'Ensure box 1 is populated if box 3 has a value
If cCont.Name = "TextBox3" And cCont.Value < _
vbNullString And TextBox1.Value = vbNullString Then
MsgBox "Text Box 1 cannot be empty with a value in textbox 3"
End If
Next cCont
End Sub

Mike

"TC" wrote:

Hi All.

I have a user form that acts as an inputbox, and there are 6 fields withing
the form.
None of the fields require any input - they can be left blank, but if a
value is input, it must be numeric. However, if there is a value entered into
say field3 then a value IS required for field1. So if field1 is blank, I give
an error message. What I need to do, once the error message is closed, is
place the cursor back into field1 so the user can type something in. I can't
find any code within VB that will do that!

thanks
TC