moving within forms

T

TC

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
 
M

Mike H

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
 
T

TC

Hi Mike, many thanks for the reply.

I think perhaps I over did it on the original question, but thanks for
taking the time out to give a full reply. I have all the validation, error
messages etc already in place and working.

field6 of the form is calculated based on the values of field1 and field3,
(if they have values). Therefore if the user has input a value for field3,
then there has to be be a value in field1. So far so good - it all works
without a problem. This is all done within the form, before closing it.

My real problem is that, having validated all the fields within the form, if
field3 is not null and not 0, and field1 is null or is 0, then I want to put
the cursor back into field1. That's where I need help. Unfortunately, and
assuming I'm reading your code correctly, your solution doesn't appear to do
that.

I'm sure it must be simple to move the cursor to a specific field in the
form, (it is in other programming languages I use), but I have a lack of
experience in VB.

TC
 
M

Mike H

Hi,

This checks textbox 3 for a number and if true checks textbox1 is populated
and if it isn't gives the focus to textbox1

Private Sub CommandButton1_Click()
If TextBox3.Value <> vbNullString And _
TextBox3.Value <> 0 _
And TextBox1.Value = vbNullString Then
MsgBox "Text Box 1 cannot be empty with a value in textbox 3"
TextBox1.SetFocus
Else

'do other things

End If

End Sub


Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top