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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating form text boxes after a button is clicked

Status
Not open for further replies.

outonalimb

Technical User
Oct 7, 2003
454
0
0
GB
Hi,

My VB is fairly limited so I hope you guys can help.

All I want to do is validate text boxes on a form so that when a user hits my 'Apply' button, it checks to make sure that there is an entry in each of the text boxes.

I need to do this for both text values and number values. So for example, a text box must contain a number entry between 1 and 5. If it doesn't, it needs to prompt the user that a valid value should be entered.

This is probably really easy but I appreciate any help.

Regards,


 
The usual way to do simple data validation is to use the Validate event for the textbox. It is fired immediately before the Lost_focus event, and you can use it to test the data, send an error message and prevent the textbox from losing focus. You need to make sure that the control which will get focus next has it's CausesValidation property set to True

[tt]Private Sub Text1_Validate(Cancel As Boolean)
If Len(Text1.text) = 0 Then
MsgBox "Put text in"
Cancel = True
End If
End Sub
[/tt]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Thanks for the advice. However, I was looking for something that would check the text boxes when an 'Apply' button is clicked.

The above example works but once my form is loaded I am forced to enter some data in the text box that the code is applied to and am not even able to click my 'Cancel' button until this is done.

Here is some code I have written to try and validate the form once the Apply button is clicked. It seems to work when the button is clicked once but if it is clicked twice the program proceeds to run without the validation.

I am confused! Can anyone help?

Private Sub ValidateForm(Cancel As Boolean)

If txtAddress1.Text = "" Then
MsgBox "You must enter a value in Address 1" & vbCrLf & _
Err.Description, vbExclamation, "Data entry error"
Cancel = True
txtAddress1.SetFocus
End If

If Not IsNumeric(txtPropertyValue.Text) Then
MsgBox "You must enter a numeric value in Property Value" & vbCrLf & _
Err.Description, vbExclamation, "Data entry error"
End If

End Sub
 

Code:
sub command1_click

  if validateform = true then
    call your_next_sub
  end if

end sub

Function ValidateForm As boolean

  validateform = true
  If txtAddress1.Text = "" Then
    MsgBox "You must enter a value in Address 1" & vbCrLf & _
    Err.Description, vbExclamation, "Data entry error"
    Validateform = false
    txtAddress1.SetFocus
  End If
  
  If Not IsNumeric(txtPropertyValue.Text) Then
    MsgBox "You must enter a numeric value in Property Value" & vbCrLf & _
    Err.Description, vbExclamation, "Data entry error"
    validateform = false
  End If

End function

You may also want to add in:

Code:
  If (val(txtpropertyvalue) <= 0) or (val(txtpropertyvalue) >= 6) then
    msgbox("Must be between 1 and 5")
    validateform = false
  end if

~*Gwar3k1*~
&quot;To the pressure, everything's just like: an illusion. I'll be losing you before long...&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top