I have 4 forms that use a similar error handling to prevent duplicate entries (see first code). Three of the four forms are working as hoped but the fourth isn't. The three that work are identical function-wise but the fourth differs. The AfterUpdate() code in the 3 working forms is simple (i.e., it only makes the subform and a couple option buttons visible) if the On Form Error() event doesn't fire. The fourth form has a While loop attached to an AfterUpdate() - the While loop must stay because it semi-populates the subform and associated table. So, the problem that I'm running into is that the While loop runs, thus tries creating a record that potentially has a duplicate (Error 3022) record. I tried to incorporate the error handling directly into the AfterUpdate() for this form (see second code below) but it isn't working. Instead, it gives me the default access error code (versus what I'm wanting it to do) and sends me to debugging. Essentially, I want it to check for the duplicate errors, undo the form and setfocus back to the first combobox if there is a duplicate transect number trying to be created.
I'm not well versed in VBA but I've tried to use Exit Sub in the On Error GoTo Error which doesn't seem to run the While loop but doesn't undo the form and SetFocus back to the beginning, instead it just goes to the next tabstop item.
Normal Form_Error that works in the three forms
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const conErrDuplicateTransect = 3022
Select Case DataErr
Case conErrDuplicateTransect 'duplicate transect within a trip
MsgBox "Transect " & TransectNum & " has already been entered." & vbCr & "Please check transect number and try again.", vbInformation, "Duplicate Transect Number"
Me.Undo
Me.frm_BeltSurveyData_Subform.Visible = False
Me.Notes.Visible = False
Me.TransectNum.SetFocus
Response = acDataErrContinue
End Select
End Sub
Attempts to integrate error trapping into AfterUpdate using On Error GoTo
Code:
Private Sub Observers_AfterUpdate()
On Error GoTo Error 'error duplicate records
If Me.NewRecord Then
DoCmd.RunCommand acCmdSaveRecord 'create record in tbl_LineSurvey
Dim x As Double 'add Transect Point 0.5 - 25.0, and LineSurveyID to tbl_LineSurveyData
x = 0.5
While x <= 25
CurrentDb.Execute "INSERT INTO [tbl_LineSurveyData]([LineSurveyID], [TransectPoint]) " & _
"VALUES (" & [Forms]![frm_LineSurvey]![LineSurveyID] & "," & x & ")", dbFailOnError
x = x + 0.5
Wend
Me.frm_LineSurveyData_subform.Requery 'reload the subform to dislpay populated transect point number
Me.frm_LineSurveyData_subform.Visible = True 'make the subform visible
Me.Notes.Visible = True 'make notes field visible
Me.newLineTransect.Visible = True 'make Add New Transect Line button visible
End If
Error:
Dim DataErr As Integer
Const conErrDuplicateTransect = 3022
Select Case DataErr
Case conErrDuplicateTransect 'duplicate transect within a trip
MsgBox "Transect " & TransectNum & " has already been entered." & vbCr & "Please check transect number and try again.", vbInformation, "Duplicate Transect Number"
Me.Undo
Me.frm_LineSurveyData_subform.Visible = False
Me.Notes.Visible = False
Me.TransectNum.SetFocus
End Select
Exit Sub
End Sub