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

How to control focus when validate event is used 1

Status
Not open for further replies.

balajee

Programmer
Jun 29, 2000
134
NZ
Hi

I have an application in VB6 using ADODC. I am tryng to Add a record to the database. I have ADD,DELETE,CHANGE,CANCEL,SAVE and EXIT buttons on the form. When adding a new record I am disabling ADD,DELETE,CHANGE. I have put the following code in validate event of a text control. If I want to cancel the ADD operation I click on CANCEL button. But the validate event gets fired even if try to click on any button. Is there any way that I can click on CANCEL, EXIT buttons if I do not want the ADD operation without having the validate even fired.

Private Sub txtLastName_Validate(Cancel As Boolean)
Dim sCheckLastName As String
If bAddNew = True Or bEdit = True Then
sCheckLastName = txtLastName.Text
If Len(Trim(sCheckLastName)) = 0 Then
MsgBox "Last Name cannot be blank.", _
vbExclamation + vbOKOnly, "Data Entry"
Cancel = True
End If
End If
End Sub

[sig][/sig]
 
Sometimes the validate event is not the right place for some types of data validation. Consider this. Lets say that a user was using your program and you need the txtLastName filled in. If the user does not tab to the txtlastname box the txtlastname_validate will not fire and possibly create a database error on the update. So I would suggest using the validate event only for checking if the data was within the required paramaters such as that the length dosn't exceed lets say 50 characters that was set up in your database or maybe that the first letter is capitalized or that the name does not contain certain characters etc. and other data validation is done before the .addnew and .update by calling a validation sub.
Just my 2 cents. [sig]<p>David Paulson<br><a href=mailto: > </a><br><a href= > </a><br> [/sig]
 
Hi balajee, i am of the same opinion as david. Yes indeed these validations before an add or and update are generally done in a seperate procedure(normally) and then used. So, try this and u will find it really simple to make it a habit. For this problem you could use something like

if txtlastname = &quot;&quot; then
msgbox &quot;&quot;
txtlastname.setfocus
exit sub (or exit function)
end if

all the best...vijay [sig][/sig]
 
Oh, theres also another way. You can set the CausesValidation of the command button to False. This does not fire the validate event of the textbox when focus changes to the command button [sig]<p>David Paulson<br><a href=mailto: > </a><br><a href= > </a><br> [/sig]
 
you might want to check out a similar informative discussion at :^) thread222-7494 [sig]<p> Mark Saunders
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top