[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Force user to a text box.

Posted on 2004-11-30
14
Medium Priority
?
207 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:otaibis
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 5

Accepted Solution

by:
RogueSolutions earned 200 total points
ID: 12706878
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
 
LVL 5

Expert Comment

by:RogueSolutions
ID: 12706903
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
 
LVL 32

Expert Comment

by:Erick37
ID: 12706994
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 12707493
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
 
LVL 101

Expert Comment

by:mlmcc
ID: 12708413
You can set the focus on the Activate event.

mlmcc
0
 
LVL 1

Expert Comment

by:AsifBangash
ID: 12713123
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
 

Author Comment

by:otaibis
ID: 12713192
hi

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

thanks all
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 12721768
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 12721777
My Form Load should be :

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

sorry about that !
0
 
LVL 1

Expert Comment

by:AsifBangash
ID: 12724934
but this is work properly.
0
 

Expert Comment

by:stingkhan
ID: 12733179
AsifBangash is right
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question