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

Exit Sub if validation error...

Status
Not open for further replies.

lorirobn

MIS
Mar 15, 2005
450
US
Hi,

I am doing validations for my form in the "Before_Update" event. This event is triggered when command button 'cmdave' is pressed.

In Before_Update, for each control, I have validation code. If a required field is missing, I display an error with msgbox, and exit the sub. However, the problem is, processing goes to the 'end sub' of cmdSave, rather than the 'end sub' of the current event, Before_Update. Do I need to 'shut off' the Access message informing me that the Save was cancelled? (and how is that done?)

My code is:
Code:
If DepartmentID = "" Or IsNull(DepartmentID) Then
   MsgBox "Please select Department from list.", vbOKOnly
   Cancel = True
   Exit Sub
End If

After my msgbox pops up, I then get a second msgbox saying "the DoMenuItem action was cancelled".

thanks in advance...
 
Got part of it solved. Replaced the 'exit sub' with 'GoTo Exit_Form_Before_Update'. But it still goes to the cmdSave exit afterward.
 
What does cmdSave look like?

Privatre Sub cmdSave_Click()
Call DepartmentID_BeforeUpdate
Call Emloyee_BeforeUpdate
...
Docmd.MenuItem....
End Sub

?????
 
cmdSave looks like this:

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70


My Before_Update events are actually Form_BeforeUpdates (should have been more specific above).
 
No problem, maybe I should've read more carefully.

it seems you have a conflict of interest, between the procedures.

Saving a record, initiates the Form Before/After Update events. So if you have to cancel, Before a record can be saved,
obviously, it can't save it.

Maybe using the same code, in the Form_BeforeUpdate,
create a boolean function, called eg; "FormIntegrity"

In the cmdSave_Click put

If FormIntegrity = True Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
Else
msgBox "required fields have not been populated"
End Sub



Or consider another way to check
for field integrity, field by field maybe?.
 
the boolean function is a good idea...

if I check field by field, it would be in each control's Before_Update event, correct? My understanding is that if it's a required field, and no data is entered, then the Before_Update event would not be activated. Is that not true? That's why I didn't bother putting it there in the first place.

I thought there was a way to turn off Access' message, which would solve the problem.

Tomorrow I will play around with the boolean function...

thanks, zion7!
Lori
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top