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

Stopping all Procedures...help. 1

Status
Not open for further replies.
Jan 24, 2002
33
CA
Good Afternoon,

Maybe someone can help me with this one...

It's actually simple (or so it seems) but I can't seem to figur it out.

I have a procedure called cmdCloseForm_Click(). This procedure closes the form. I have included in this procedure a Call Statement to another procedure. The Other procedure, ValidateRecord(), checks certain fields for errors. I've simplified it and it looks something like this:

Private Sub cmdCloseForm_Click()
Call ValidateRecord()
Docmd.Close, acform, "ThisForm"
End Sub

...

Public Sub ValidateRecord()
If isNull(Me.field1) Then
MsgBox "ERROR"
Else
MsgBox "No ERRORS"
End If
End Sub

What I would like my code to do is stop the form from closing if Me.field1 is Null. I tried using an Exit Sub in the ValidateRecord procedure but it only prevents the rest of that procedure from executing, not the code in the cmdCloseForm. the End statement worked but it also deleted all of the information loaded in my variables.
I know I can pass a variable to the cmdCloseForm procedure (like True or False) and include an If statement in the cmdCloseForm procedure but I have quite a few of these examples around my application and I was wondering if there was an easier way around it. I'm sure there's an easier way to do this.

Thanks,
Lawrence

"If you don't succeed - CTRL + ALT + DELETE :) "
 
Lawrence, do it this way:

1. Make second procedure a function (instead of sub) that will return You True if everything is ok and false if something's wrong with field. When You get that answer in first sub - then You act acordingly. So, basically, do it following way:

1st Function looks like:
Code:
Public Function RequiredInteger(ByVal varNumber As Variant) As Boolean
'    Validate a required integer field
'    Criteria: cannot be null, must be a whole number

'    Assume invalid data
     RequiredInteger = False

'    Check criteria
     If IsNumeric(varNumber) Then
          If CDbl(varNumber) = CLng(varNumber) Then
               RequiredInteger = True
          End If
     End If

End Function

From above function you see now that it will return true if value in field is OK (integer number in this case), and will return false if value in field is not correct (null, not number,...).

Now, in Your main cmdCloseForm_Click() Sub, write code as:

Code:
if RequiredInteger(me.field1) = true then
docmd.close
else
'do something else (msgbox,...)
end if

Hope this helped.
 
Thanks for the response alfalf,

Although your way is appropriate for what I need to do, I was trying to avoid passing variables back from the sub procedure to the main procedure using some sort of existing command or method to stop the code from running. I am presently using your method but I was hoping someone knew a shortcut.

Thanks Again,

Lawrence

"If you don't succeed - CTRL + ALT + DELETE
 
I haven't tried this, but just looking at your code, consider this:

Code:
Private Sub cmdCloseForm_Click()
 Call ValidateRecord()
 'Docmd.Close, acform, "ThisForm"   [COLOR=red]move this[/color] 
End Sub

...

Public Sub ValidateRecord()
 If isNull(Me.field1) Then
   MsgBox "ERROR"
 Else
   MsgBox "No ERRORS"
   Docmd.Close, acform, "ThisForm" '[COLOR=red]to here[/color]
 End If
End Sub

If you are using the sub ValidateRecord at other times not wanting this result, make this another sub with a different name like ValidateRecordClose.

Does this do it?
lrott
 
The best way to do this is actually to put the validation code in the Form_BeforeUpdate event procedure. This procedure has a Cancel parameter which you can set to True to stop whatever event caused the procedure to be called. (This event could be trying to close the form, or it could be moving to a different record, saving the record, attempting to tab out of the last field in the form, etc.) If you move the validation code, your cmdClose_Click procedure doesn't have to call it; Access will call it directly.

With your code organized the way it is now, you will not validate the data before the record is stored, unless the record is being stored as a side effect of closing the form. If you move the validation to the BeforeUpdate event (which is what that event is for, by the way), your DoCmd.Close will automatically get canceled for you if the validation decides there is an error. Your code will also be simpler, because you won't have to communicate success or failure between the procedures.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Thank you for your responses,

To answer lrott, I can't put the Docmd.Close within the ValidateRecord procedure because this procedure is used in different instance within the form and I do not want the procedure to close the form every time. I just wanted the code to stop running somehow. I understand that this would be alot simpler when the ValidateRecord is only used before closing the form.

RickSpr, validating in the Before_Update would be wonderful except that I may not have mentioned it before, but some of my validation are contigent upon not just one filed but on a compilation of various fields, such that i have to wait until all textboxes are filled for me to validate them and BeforeUpdate only works if the user has setfocus on a field and entered information, otherwise the user can skip the field so I obviously need to make sure the user enters information in the field befoer moving on. And it would be hard to force the user to go to a specific field before another.

I have no choice but to pass variables from one procedure to another... and even that raises some questions...

Thanks again,
Lawrence

"If you don't succeed, CTRL + ALT + DELETE"

 
You don't seem to realize that there is a Form BeforeUpdate event, and missed that I was indicating that. The Form's BeforeUpdate event occurs when the user indicates he is done with the record by trying to save it, move to another record, or close the form, so that necessarily means he has filled all the fields. Also, it has no dependence nor influence on the focus, although you can move the focus from within it if you wish.

In short, the Form BeforeUpdate event is meant for exactly the kind of cross-field validation that you're trying to do.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top