I have a form with a DblClick Event that opens another form to enter data and update. The double click works fine and the other form opens with the focus set on the field requiring the input.
When I enter a number that is not within the range and click Update, the OnClick event for the update button executes an update SQL command and then the BeforeUpdate event runs a validation. If the entry is not within the specified range, a message box reminds the user of the required range. When the user clicks OK on the message box, the message box closes but the form also closes.
I want the user to click "OK" on the message box and the message box close but leave the form open with the set focus on the same field it was on when it first opened. The code for the update button on click and the before update is below:
Code:
Private Sub cmdUpdate_Click()
On Error GoTo cmdUpdate_Click_Err
'Turn Off Warnings
DoCmd.SetWarnings False
'Update the Test Results Table with the Value in the Soil Ph Text Box
DoCmd.RunSQL "UPDATE [testResults]" & _
"SET [Reported Conc (Calib)] = Forms![frmUpdateWP]![txtSoilPh]" & _
"WHERE [TestYear] = 'Forms![frmUpdateWP]![txtYr-Sample]' And [Sample ID] = 'Forms![frmUpdateWP]![txtSampleID]'"
'Close the Message Box
DoCmd.Close , ""
'Turn Message Box asking if you want to update the record back on
DoCmd.SetWarnings True
cmdUpdate_Click_Exit:
Exit Sub
cmdUpdate_Click_Err:
MsgBox Error$
Resume cmdUpdate_Click_Exit
End Sub
Below is the code for the before update event:
Code:
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![frmUpdateWP]![txtSoilPh] < 4 Or Forms![frmUpdateWP]![txtSoilPh] > 8 Then
MsgBox "WP Range Must be Between 4 and 8"
'Forms![frmUpdateWP]![txtSoilPh].Undo
Cancel = True
Me.txtSoilPh.SetFocus
Exit Sub
End If
End Sub
This has me stumped. I am still a novice with VBA. I think that it has to do with where the DoCmd.Close is in the cmd_UpdateClick event. If my code needs to change, please show me how it should change. Thanks for the help.