otaibis
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
You can set the focus on the Activate event.
mlmcc
mlmcc
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.
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.
ASKER
hi
I will try the ideas you've posted here and then come back to you.
thanks all
I will try the ideas you've posted here and then come back to you.
thanks all
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
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
My Form Load should be :
Private Sub Form_Load()
txtEmail.enabled = false
txtPhoneNumber.enabled = false
End Sub
sorry about that !
Private Sub Form_Load()
txtEmail.enabled = false
txtPhoneNumber.enabled = false
End Sub
sorry about that !
but this is work properly.
AsifBangash is right
You can't call a SetFocus in the form's load event (in case you try that).