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!

Command button problem

Status
Not open for further replies.

Omono

MIS
Apr 20, 2001
24
0
0
US
I have a form with txtboxes into which data is input. I validate the data; something has to be entered into the txtboxes. After data is entered, either the tab-key or enter-key can be pressed to go to the next txtbox. Once the boxes are filled, the user can press a "calculate" cmdbutton to plug the data into an equation; or press the "reset" button to clear all txtboxes, returning to the first txtbox for new data input; or select the "exit" cmdbutton to close the form. If no data is entered into the field an form pops up indicating that data must be entered to proceed. My problem is that if the txtboxes are empty and I press the "close" cmdbutton, this error form pops up that I must enter data in the field. I have to use the form control box X to close the form. I have no idea where to start with this problem. Any suggestions are welcomed. The following is the routine to validate data in the txtboxes:

Private Sub txtrw_Validate(KeepFocus As Boolean)
If (Len(txtrw.Text) = 0) Then
KeepFocus = True
frmdataerror.Show
End If
End Sub
 
Try setting the focus back after the error form is displayed. Like this:

Private Sub cmdValidate_Click()
If Text1.Text = "" Then
frmerror.Show vbModal
Text1.SetFocus
End If
End Sub
 
One approach would be to set a flag in the "close" to know that you're closing the form.

Then in the validate event you can look at that flag.

Private Sub cmdClose_Click()
bClosing = True
... finish closing form
End Sub

Private Sub txtrw_Validate(KeepFocus As Boolean)
If bClosing = True Then Exit Sub
If (Len(txtrw.Text) = 0) Then
KeepFocus = True
frmdataerror.Show
End If
End Sub
 
Without knowing all your validation rules it's hard to advise, but on the basis of your validate code shown, I would do validation on the command_calculate click event.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks everyone for the suggestions. I favor the one from johnwm - putting the validation code in the cmdCalculate click event. It works great! Thanks again to all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top