I have a form that opens to collect a value entered by the user in the field labeled, "txtSoilPh". When clicking on "Add" command button, the Before Update event checks to determine if the value is within the correct range. If it is not, a message box opens advising user of the required range. When I close the message box, the insert statement continues to execute and updates the database anyway. I am still learning VB for Access so any help would be appreciated. Below is the code that I have so far for the form.
Code:
'------------------------------------------------------------
' cmdAdd_Click
'
'------------------------------------------------------------
Private Sub cmdAdd_Click()
On Error GoTo cmdAdd_Click_Err
'Turn Off Warnings
DoCmd.SetWarnings False
'Append the Test Results Table with the Value in the Soil Ph Text Box
DoCmd.RunSQL "INSERT INTO [TestResults]" & _
" VALUES (Forms![frmAddWP]![txtYr-Sample] , Forms![frmAddWP]![txtSampleID], 'WP', Forms![frmAddWP]![txtSoilPh])"
'Turn Message Box asking if you want to update the record back on
DoCmd.SetWarnings True
'Close the Message Box
DoCmd.Close , ""
cmdAdd_Click_Exit:
Exit Sub
cmdAdd_Click_Err:
MsgBox Error$
Resume cmdAdd_Click_Exit
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Check for WP range between 4 and 8 and Give Error If in range, then Hide the Message Box
If Forms![frmAddWP]![txtSoilPh] < 4 Or Forms![frmAddWP]![txtSoilPh] > 8 Then
MsgBox "WP Range Must be Between 4 and 8"
Me.Undo
txtSoilPh.SetFocus
End If
End Sub
Private Sub Form_Close()
Forms!frmRptCrosstab.Requery
End Sub
Private Sub Form_Current()
txtSoilPh.SetFocus
End Sub