-
What is the significance of Validate() event in VB? Can you please specify an example as to where this event can be used?
In the programs I have created, I have written the validations in the Change() and KeyPress() events in case of text fields and in Click() event in case of buttons. So, where does the Validate() event fit in?
Anna
-
Validate event
Hi
As a single example, you can use it in conjunction with bound (to a .mdb in this case) textboxes and a data control (where you don't allow the user access to the data control itself)
It uses the datControl functions to fire the validate event.
e.g.
'****************************************************
' Purpose: To validate input and display message
'****************************************************
Private Sub datControl_Validate(Action As Integer, Save As Integer)
Dim pstrmessage As String
Dim pintReturn As Integer
If Action = vbDataActionUpdate Then
If txtCatagory.Text = vbNullString Or txtDescription.Text = vbNullString _
Or txtSalesUnit.Text = vbNullString Then
pstrmessage = "Required field -You must enter a value"
Action = vbDataActionCancel
End If
If Not IsDate(txtLastInventory.Text) Then
pstrmessage = "Invalid Date - Date format is " & Date
Action = vbDataActionCancel
End If
If Action = vbDataActionCancel Then
pintReturn = _
MsgBox(pstrmessage, vbOKOnly, "Input error")
End If
End If
End Sub
Dave
-
the validate event does just that. it fires before the lost focus event to give you a chance to validate and cancel the shift of focus if need be. It is used with the causesvalidation property. if causesvalidation is set to true(the default), when this control is about to gain focus it will cause the validate event of the control that is losing the focus to fire. You can then validate and if you need to, cancel the shift of focus. I shows some strange behavior at times so you should experiment.