Force user to a text box.

Hi Experts,

I have a form and it includes 3 text boxes.

Name:
Email:
Phone number:

and I have a button.

Now,
1. I want to force the user to fill the name text box first.
2. If the user did not write any thing in the name text box and click any where in the form he get a message telling him he must first write the name.  (e.g. if the user click the email text box without writing the name, then he should get the warning message and the type crouser go to the Name text box"

I mean I want to focus on the name text box first.

What is the code for doing that?

Thanks
otaibisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RogueSolutionsCommented:
Add a LostFocus event for your Name textbox, so

Private Sub txtName_LostFocus()
   If Trim(txtName.Text)="" Then
      MsgBox "Please fill in Name first"
      txtName.SetFocus
   End If
End Sub

That will force them to have something in Name.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RogueSolutionsCommented:
To focus on the Name textbox when the form first loads you set the TabIndex property of the Name textbox to be 0

You can't call a SetFocus in the form's load event (in case you try that).

0
Erick37Commented:
You can use the Validate event:

Option Explicit


Private Sub Form_Load()
    Show
    DoEvents
    Text1.SetFocus
End Sub

Private Sub Text1_Validate(Cancel As Boolean)
    If Text1.Text = "" Then
        MsgBox "Please enter something in Text1"
        Cancel = True 'force back to Text1
    End If
End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

EDDYKTCommented:
Private Sub Form_Load()
Text2.Enabled = False
Text3.Enabled = False
End Sub

Private Sub Text1_Change()
Text2.Enabled = IIf(Len(Text1.Text), True, False)
Text3.Enabled = IIf(Len(Text1.Text), True, False)
End Sub
0
mlmccCommented:
You can set the focus on the Activate event.

mlmcc
0
AsifBangashCommented:
Dear u must use the LostFocus event for it.

Private Sub name_LostFocus()

If Text1 = "" Then
Text1.SetFocus
End If

End Sub

Private Sub email_LostFocus()

If Text2 = "" Then
Text2.SetFocus
End If

End Sub

Private Sub phone_LostFocus()

If Text3 = "" Then
Text3.SetFocus
End If

End Sub

Is that ur question, ur comments show us that problem if any problem then post ur comments about it plz.

Thanks.

0
otaibisAuthor Commented:
hi

I will try the ideas you've posted here and then come back to you.

thanks all
0
Shane Russell2nd Line Desktop SupportCommented:
I would do something simmiliar to what this user suggested :

Comment from RogueSolutions
Date: 11/30/2004 07:41AM PST
 Comment  


Add a LostFocus event for your Name textbox, so

Private Sub txtName_LostFocus()
   If Trim(txtName.Text)="" Then
      MsgBox "Please fill in Name first"
      txtName.SetFocus
   End If
End Sub

That will force them to have something in Name.

Except I would do the following because this way the additional text controls namely ( email and phone number) are disabled , hence they cant type into them until the original one ( Name ) is filled out and then the next control will get focus ready for them to type the next part :

Private Form_Load()
txtEmail.enabled = false
txtPhoneNumber.enabled = false
End Sub

Private Sub txtName_LostFocus()
   If Trim(txtName.Text)="" Then
      MsgBox "Please fill in Name first"
      txtName.SetFocus
   Else
   txtEmail.enabled = true
   txtEMail.SetFocus
   End If
End Sub

Private Sub txtEmail_LostFocus()
   If Trim(txtEmail.Text)="" Then
      MsgBox "Please fill in Name first"
      txtEmail.SetFocus
   Else
   txtPhoneNumber.enabled = true
   txtPhoneNumber.SetFocus
End If
0
Shane Russell2nd Line Desktop SupportCommented:
My Form Load should be :

Private Sub Form_Load()
txtEmail.enabled = false
txtPhoneNumber.enabled = false
End Sub

sorry about that !
0
AsifBangashCommented:
but this is work properly.
0
stingkhanCommented:
AsifBangash is right
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.