Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 - Force user input

Status
Not open for further replies.

Omono

MIS
Apr 20, 2001
24
US
Hello,

On a form I have multiple text boxes, all of which must contain data so that a calculation can be made and put into the answer text box. I have code that permits only numbers to be entered into the text boxes, however, I do not have code to prevent the boxes from being left empty. I have placed what I thought was the correct code in a number of places but none have worked. I would like a message box to pop up as soon as the user bypasses the box, whether the Tab or Enter-key is used. After the user acknowledges the msgbox, they are returned to the empty field. This is the code:

Private Sub bd_keyPress(KeyAscii As Integer)
If KeyAscii > 45 And KeyAscii < 58 Or KeyAscii = 8 Or KeyAscii = 13 Then
If KeyAscii = 13 Then
SendKeys &quot;{tab}&quot;
KeyAscii = 0
End If
Else
KeyAscii = 0
End If
If bd.Text = &quot; &quot; Then
MsgBox &quot;Each field requires data,&quot; + vbOKOnly, vbExclamation, &quot;Error&quot;
bd.SetFocus
End If
End Sub

Thanks in advance...
ONeal
 
Checking the value of the text box in a Keypress event will give you troubles, because the key press event is fired before the character is entered into the textbox.

I would suggest that you perform your empty check in the Validate event instead. That event is fired immediately prior to the control losing focus.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
HI..

I believe that if you set the Validate property of the text boxes to true, and place appropriate code in the Validate event of each text box, you will get the desired effect.

Also, you could have a form level validation procedure. When you go to do the calculation (through a cmdButton i assume), loop through the text boxes and prompt user if any of the text boxes have a text value = &quot;&quot;. (text1.text = &quot;&quot;)

Hope this helps. To get what you want, you have to go through the crap, but to get through the crap you have to know what you want... [rockband]
 
Thanks for the quick response CajunCenturion and Dazz22. However, I don't understand how to validate. Is that coded or set in textbox properties?
Thanks...
O'Neal
 
Validate is an event of an editbox

Private Sub Text1_Validate(KeepFocus As Boolean)

If (Len(Text1.Text) = 0) Then
KeepFocus = True
MsgBox &quot;Please insert a value in this field&quot;
End if

End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
The &quot;Validate&quot; event for the textbox will be not be fired until {b]another control[/b] with &quot;CausesValidation&quot; is encountered. &quot;CauseValidation&quot;=true for the textbox does not fire the event.

I use a central validation procedure, besides the usual KeyPress validation, that is called when the user wants to leave the page or otherwise have the progam consider that input is complete. My users don't mind being restricted to a page because of validation but being restricted to a &quot;box&quot; is not an option. Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Use the Validate event!
This works like the BeforeUpdate in the Access!
Dont forget to set the Cancel button to not evaluate the cancel!!!

If You want to implement the ESC key You have to set the KeyPreview of the form and in the (even in the KeyPress event - code 27=escape) one of the Key events You just call the Cancel button Click.

 
Thanks again CajunCenturion. The code you provided works just like I want. Thank you....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top