Hi all,
Wondering if any of you have seen this before and if you have any suggestions...it's driving me nuts.
I have code behind an "Ok" button (cmdOK_Click) on a form that simply saves the record if the form is dirty then closes the form. If the data validation check (behind the Form_BeforeUpdate procedure) cancels the save, an error (3021) is generated, which I trap and ignore in the cmdOK_Click procedure. This works fine when I step through the code in debug mode, but when I run the code normally (i.e., without any break points set), I get different errors (2455, 3075), seemingly at random. Why would the code behave differently when in debug mode vs when running in "normal" mode? Code below.
Thanks,
Dean
Code:
Private Sub cmdOK_Click()
On Error GoTo ErrorHandler
bOkClicked = True
If Me.Dirty Then
Me.Dirty = False
End If
Call CloseForm
ExitHere:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 3021 'Save canceled by data validation code
bOkClicked = False
Resume ExitHere
Case Else
MsgBox Err.Number & ": " & Err.Description, vbExclamation
Call LogError("frmProjectNew.cmdOK_Click", Err.Number, Err.Description)
Resume ExitHere
End Select
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim varReturn As Variant
'Data validation code here
Select Case True
Case IsNull(Me.Title) Or Len(Me.Title) = 0
MsgBox "You must specify a project title before saving.", vbExclamation + vbOKOnly
Cancel = True
Me.Title.SetFocus
Exit Sub
Case IsNull(Me.PrimaryWorkTypeID)
MsgBox "You must specify a primary work type before saving.", vbExclamation + vbOKOnly
Cancel = True
Me.PrimaryWorkTypeID.SetFocus
Exit Sub
End Select
'Check if form is dirty and prompt to save changes
If Me.Dirty And Not bOkClicked Then
varReturn = MsgBox("Do you want to save your changes?", vbExclamation + vbYesNoCancel)
If varReturn = vbNo Then
Me.Undo
Exit Sub
ElseIf varReturn = vbCancel Then
Cancel = True
Exit Sub
End If
End If
'Log changes
Call AuditTrail_Update(Me, Me.Controls(conPrimaryKey).Value)
End Sub