OnClose event

aziz rasul

Active member
Local time
Today, 14:33
Joined
Jun 26, 2000
Messages
1,935
When I close a form and I have created a duplicate primary key, I get the general message telling me that 'I can't save the record at this time', blah, blah. I DON'T GET AN ERROR NUMBER.

I have a message box which I have created in Access which I use when an error appears e.g.

Code:
    If Err = 3022 Then
        DoCmd.OpenForm "frmTimedMessageBox"
        Forms!frmTimedMessageBox.TimerInterval = 3500
        Forms![frmTimedMessageBox]!lblMessage.Caption = "A record already exists with the same primary key 'Distributor Ref' value."
        Exit Sub
    End If

How can I display "frmTimedMessageBox" instead of obtaining the Access error dialog box?
 
I tried to catch an error once that i knew was happening, knew the code and everything but couldnt get it to display my msgbox. so what i did was removed the catch (Err= ###) and it worked fine. im sure it was the wrong way to do it but i couldnt find a way to make it work!!
 
Use the OnError event of the form
 
...removed the catch (Err= ###) ...

Can you explain?
 
Rich, I tried your suggestion and my personal msgbox form appeared. However the Access dialog boxes also appeared.

Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)

    DoCmd.OpenForm "frmTimedMessageBox"
    Forms!frmTimedMessageBox.TimerInterval = 3500
    Forms![frmTimedMessageBox]!lblMessage.Caption = "A record already exists with the same primary key 'Distributor Ref' value."

End Sub
 
To disallow the default messagebox, use

Response = acDataErrContinue

within the routine
 
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = YourNumber Then
'Display Error Message
MsgBox "Whatever"
'Prevent the standard error message from showing
Response = acDataErrContinue
'Set focus to a control off the form
txtPMID.SetFocus
Else
'If another error is applied then show the standard error message
Response = acDataErrDisplay
End If
 
Thanks all. I got it to work. When I close the form, only my message box appears. Fantastico.

Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)

    DoCmd.OpenForm "frmTimedMessageBox"
    Forms!frmTimedMessageBox.TimerInterval = 4000
    Forms![frmTimedMessageBox]!lblMessage.Caption = "A record already exists with the same primary key 'Distributor Ref' value. The record was not saved."
    
    Response = acDataErrContinue

End Sub
 

Users who are viewing this thread

Back
Top Bottom