Do not allow form to close on validation error

D

dsc2bjn

I created an input form and added validation code as the form is close;
however, the error message do not keep the form from closing.

What do I need to do?

Private Sub save_Click()

On Error GoTo Err_save_Click
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Tracking Number] Is Null")) Then
MsgBox "A Value for 'Tracking Number' is Required.", vbOKOnly, "Required
Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Source] Is Null")) Then
MsgBox "A Value for 'Deficiency Source' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Null")) Then
MsgBox "A Value for 'Deficiency Description' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Not Null and [Forms]![frmOther Weakness
Corrective Actions (Edit/Update Entry)]![Validation Indicator] Is Null")) Then
MsgBox "A Value for 'Validation Indicator/Exit Criteria' is Required.",
vbOKOnly, "Required Value Missing"

Else
DoCmd.Close
End If

Exit_save_Click:
Exit Sub

Err_save_Click:
MsgBox Err.Description
Resume Exit_save_Click

End Sub
 
B

boblarson

Put your validation code in the form's BEFORE UPDATE event and if everything
is okay, just let it go. If not use

Cancel = True

to cancel the update and return your user to the form.

I typically will also add a message box to ask if they want to continue with
the record so they can escape if they choose not to.
--
Bob Larson
Access MVP
Access World Forums Administrator
Utter Access VIP

Tutorials at http://www.btabdevelopment.com

__________________________________
 
D

dsc2bjn

I tried putting just the Validation code in the Before Update section. It
still closed the form without having the requried data elements to consitute
a "valid" record.

I tried adding the Cancel=True as well. The form still closes.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Tracking Number] Is Null")) Then
MsgBox "A Value for 'Tracking Number' is Required.", vbOKOnly, "Required
Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Source] Is Null")) Then
MsgBox "A Value for 'Deficiency Source' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Null")) Then
MsgBox "A Value for 'Deficiency Description' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Not Null and [Forms]![frmOther Weakness
Corrective Actions (Edit/Update Entry)]![Validation Indicator] Is Null")) Then
MsgBox "A Value for 'Validation Indicator/Exit Criteria' is Required.",
vbOKOnly, "Required Value Missing"
End If
Cancel = True

End Sub

boblarson said:
Put your validation code in the form's BEFORE UPDATE event and if everything
is okay, just let it go. If not use

Cancel = True

to cancel the update and return your user to the form.

I typically will also add a message box to ask if they want to continue with
the record so they can escape if they choose not to.
--
Bob Larson
Access MVP
Access World Forums Administrator
Utter Access VIP

Tutorials at http://www.btabdevelopment.com

__________________________________


dsc2bjn said:
I created an input form and added validation code as the form is close;
however, the error message do not keep the form from closing.

What do I need to do?

Private Sub save_Click()

On Error GoTo Err_save_Click
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Tracking Number] Is Null")) Then
MsgBox "A Value for 'Tracking Number' is Required.", vbOKOnly, "Required
Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Source] Is Null")) Then
MsgBox "A Value for 'Deficiency Source' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Null")) Then
MsgBox "A Value for 'Deficiency Description' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Not Null and [Forms]![frmOther Weakness
Corrective Actions (Edit/Update Entry)]![Validation Indicator] Is Null")) Then
MsgBox "A Value for 'Validation Indicator/Exit Criteria' is Required.",
vbOKOnly, "Required Value Missing"

Else
DoCmd.Close
End If

Exit_save_Click:
Exit Sub

Err_save_Click:
MsgBox Err.Description
Resume Exit_save_Click

End Sub
 
C

Chris O''''Neill

It looks to me like your first 3 IF/THEN/END IF statements only do a MsgBox
telling the user something is wrong. Only the last IF/THEN/ELSE/END IF
statement prevents the form from closing if that criteria isn't met.

Maybe try something like this:

Dim intIsError as integer

intIsError = 0

IF (put first evaluation criteria here) THEN
MsgBox (put your "There's an Error!" message here)
intIsEror = 1
EN IF

Do this for each of the validations you want to perform and then end with this

IF intIsError = 0 THEN
DoCmd.Close
END IF

In other words, the form will only close if intIsError = 0 and if there's a
problem you set intIsError to not equal zero.

Hope that helps...

Regards, Chris

P.S. You have spaces in your form and field names which can cause major
headaches. I recommend that you change "frmOther Weakness Corrective Actions
" to "frmOtherWeaknessCorrectiveActions" or something like that, and do this
for all form, field and control names.
 
C

Chris O''''Neill

Of course, you MAY want to be sure you spell intIsError correctly each time
you use it! (I misspelled it in my example! LOL!!!)

Regards, Chris

Chris O''''Neill said:
It looks to me like your first 3 IF/THEN/END IF statements only do a MsgBox
telling the user something is wrong. Only the last IF/THEN/ELSE/END IF
statement prevents the form from closing if that criteria isn't met.

Maybe try something like this:

Dim intIsError as integer

intIsError = 0

IF (put first evaluation criteria here) THEN
MsgBox (put your "There's an Error!" message here)
intIsEror = 1
EN IF

Do this for each of the validations you want to perform and then end with this

IF intIsError = 0 THEN
DoCmd.Close
END IF

In other words, the form will only close if intIsError = 0 and if there's a
problem you set intIsError to not equal zero.

Hope that helps...

Regards, Chris

P.S. You have spaces in your form and field names which can cause major
headaches. I recommend that you change "frmOther Weakness Corrective Actions
" to "frmOtherWeaknessCorrectiveActions" or something like that, and do this
for all form, field and control names.

dsc2bjn said:
I created an input form and added validation code as the form is close;
however, the error message do not keep the form from closing.

What do I need to do?

Private Sub save_Click()

On Error GoTo Err_save_Click
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Tracking Number] Is Null")) Then
MsgBox "A Value for 'Tracking Number' is Required.", vbOKOnly, "Required
Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Source] Is Null")) Then
MsgBox "A Value for 'Deficiency Source' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Null")) Then
MsgBox "A Value for 'Deficiency Description' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Not Null and [Forms]![frmOther Weakness
Corrective Actions (Edit/Update Entry)]![Validation Indicator] Is Null")) Then
MsgBox "A Value for 'Validation Indicator/Exit Criteria' is Required.",
vbOKOnly, "Required Value Missing"

Else
DoCmd.Close
End If

Exit_save_Click:
Exit Sub

Err_save_Click:
MsgBox Err.Description
Resume Exit_save_Click

End Sub
 
B

boblarson

I'm guessing it has to do with how you are checking the values. What is the
Eval part supposed to do? Why not just:

If IsNull(Me.[Tracking Number) Then...

or actually better (to check for an empty string too):

If Len(Nz(Me.[Tracking Number]),"") = 0 Then ...

If the code is on the form it is checking the values of, you don't need the
full form reference, just Me. will do.
--
Bob Larson
Access MVP
Access World Forums Administrator
Utter Access VIP

Tutorials at http://www.btabdevelopment.com

__________________________________


dsc2bjn said:
I tried putting just the Validation code in the Before Update section. It
still closed the form without having the requried data elements to consitute
a "valid" record.

I tried adding the Cancel=True as well. The form still closes.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Tracking Number] Is Null")) Then
MsgBox "A Value for 'Tracking Number' is Required.", vbOKOnly, "Required
Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Source] Is Null")) Then
MsgBox "A Value for 'Deficiency Source' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Null")) Then
MsgBox "A Value for 'Deficiency Description' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Not Null and [Forms]![frmOther Weakness
Corrective Actions (Edit/Update Entry)]![Validation Indicator] Is Null")) Then
MsgBox "A Value for 'Validation Indicator/Exit Criteria' is Required.",
vbOKOnly, "Required Value Missing"
End If
Cancel = True

End Sub

boblarson said:
Put your validation code in the form's BEFORE UPDATE event and if everything
is okay, just let it go. If not use

Cancel = True

to cancel the update and return your user to the form.

I typically will also add a message box to ask if they want to continue with
the record so they can escape if they choose not to.
--
Bob Larson
Access MVP
Access World Forums Administrator
Utter Access VIP

Tutorials at http://www.btabdevelopment.com

__________________________________


dsc2bjn said:
I created an input form and added validation code as the form is close;
however, the error message do not keep the form from closing.

What do I need to do?

Private Sub save_Click()

On Error GoTo Err_save_Click
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Tracking Number] Is Null")) Then
MsgBox "A Value for 'Tracking Number' is Required.", vbOKOnly, "Required
Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Source] Is Null")) Then
MsgBox "A Value for 'Deficiency Source' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Null")) Then
MsgBox "A Value for 'Deficiency Description' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Not Null and [Forms]![frmOther Weakness
Corrective Actions (Edit/Update Entry)]![Validation Indicator] Is Null")) Then
MsgBox "A Value for 'Validation Indicator/Exit Criteria' is Required.",
vbOKOnly, "Required Value Missing"

Else
DoCmd.Close
End If

Exit_save_Click:
Exit Sub

Err_save_Click:
MsgBox Err.Description
Resume Exit_save_Click

End Sub
 
D

dsc2bjn

That worked great.


Thanks!!!

Chris O''''Neill said:
Of course, you MAY want to be sure you spell intIsError correctly each time
you use it! (I misspelled it in my example! LOL!!!)

Regards, Chris

Chris O''''Neill said:
It looks to me like your first 3 IF/THEN/END IF statements only do a MsgBox
telling the user something is wrong. Only the last IF/THEN/ELSE/END IF
statement prevents the form from closing if that criteria isn't met.

Maybe try something like this:

Dim intIsError as integer

intIsError = 0

IF (put first evaluation criteria here) THEN
MsgBox (put your "There's an Error!" message here)
intIsEror = 1
EN IF

Do this for each of the validations you want to perform and then end with this

IF intIsError = 0 THEN
DoCmd.Close
END IF

In other words, the form will only close if intIsError = 0 and if there's a
problem you set intIsError to not equal zero.

Hope that helps...

Regards, Chris

P.S. You have spaces in your form and field names which can cause major
headaches. I recommend that you change "frmOther Weakness Corrective Actions
" to "frmOtherWeaknessCorrectiveActions" or something like that, and do this
for all form, field and control names.

dsc2bjn said:
I created an input form and added validation code as the form is close;
however, the error message do not keep the form from closing.

What do I need to do?

Private Sub save_Click()

On Error GoTo Err_save_Click
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Tracking Number] Is Null")) Then
MsgBox "A Value for 'Tracking Number' is Required.", vbOKOnly, "Required
Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Source] Is Null")) Then
MsgBox "A Value for 'Deficiency Source' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Null")) Then
MsgBox "A Value for 'Deficiency Description' is Required.", vbOKOnly,
"Required Value Missing"
End If
If (Eval("[Forms]![frmOther Weakness Corrective Actions (Edit/Update
Entry)]![Deficiency Description] Is Not Null and [Forms]![frmOther Weakness
Corrective Actions (Edit/Update Entry)]![Validation Indicator] Is Null")) Then
MsgBox "A Value for 'Validation Indicator/Exit Criteria' is Required.",
vbOKOnly, "Required Value Missing"

Else
DoCmd.Close
End If

Exit_save_Click:
Exit Sub

Err_save_Click:
MsgBox Err.Description
Resume Exit_save_Click

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top