Stop warning (1 Viewer)

CEH

Curtis
Local time
Today, 15:24
Joined
Oct 22, 2004
Messages
1,187
This should be a simple one.... But can't seem to find the answer.... I have put a line of code on the open event of a seconadry form.... First form is "Clients" second form is "Contracts" If no the client has no contracts the code goes.....

Private Sub Form_Open(Cancel As Integer)
If IsNull(DateOfSale) Or DateOfSale = " " Then
MsgBox "No Contract Exist"
Cancel = True
End If
End Sub

After the first message box I get a "Access" message box... "The OpenForm action was canceled" ....... Thats fine.... we know that... But we don't need to know that. How can I stop the second message box from opening? Or can I ?
 

ejstefl

Registered User.
Local time
Today, 21:24
Joined
Jan 28, 2002
Messages
378
You can add error handling to the code that opens the form. Find out the error number you are getting, and write something like this:

ErrorSection:

Select Case Err.Number

Case = 123
resume ExitSection

Case Else
msgbox "your desired error message"

End Select
 

CEH

Curtis
Local time
Today, 15:24
Joined
Oct 22, 2004
Messages
1,187
Does the warning box "The OpenForm action was canceled" have an error number? Or is it just a confirmation message responding to my "Cancel = True"
I tried "Docmd.setwarnings False" after the "Cancel = True" but no luck.

I may be thinking wrong here... But it doesn't seem logical that the "The OpenForm action was canceled" message would be classified as as "Error" But then maybe there is no distinction between errors and warnings and messages when it comes to the numbering. :confused:
 

ejstefl

Registered User.
Local time
Today, 21:24
Joined
Jan 28, 2002
Messages
378
Nope, its an error. The reason being that you asked Access to open a form, and the form did not get opened. Therefore, the DoCmd.OpenForm method results in an error.

When you get the error, you can just control-break to go into your code and type ?err.number in the immediate window to get the error number.

Make sure you're using this code from the procedure that opens the form, not the forms on open event.
 
Last edited:

CEH

Curtis
Local time
Today, 15:24
Joined
Oct 22, 2004
Messages
1,187
OK. I see now where the message was coming from. On the first form, the command button to open second form was

Private Sub ViewContracts_Click()
On Error GoTo Err_ViewContracts_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmContractSalessubform"
stLinkCriteria = "[BusinessID]=" & Me![BusinessID]

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ViewContracts_Click:
Exit Sub

Err_ViewContracts_Click:
MsgBox Err.Description
Resume Exit_ViewContracts_Click

End Sub

So if I simply remove "MsgBox Err.Description" the second message does not appear. This may not be "Correct" so to speak.... I can see in other situations that you would want the error box to appear... But for my purposes I have created the custom error message that appears first..... And I know the "cancel OpenForm" was done.... because I told it to with "Cancel = True"
 

ejstefl

Registered User.
Local time
Today, 21:24
Joined
Jan 28, 2002
Messages
378
Yes, removing the "MsgBox Err.Description" will work. However, you will never recieve an error message about any error. That may not matter to you, but if it does you can use my above advice to automatically ignore the cancelled error but report any other error.
 

jkl0

jkl0
Local time
Today, 16:24
Joined
Jun 23, 2006
Messages
192
I think it is error number 2501.

Hope this helps?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 21:24
Joined
Sep 12, 2006
Messages
15,669
I always show my error messages by using

instead of

MsgBox Err.Description

something like

msgbox("Error: " & err.number & " Desc: " & err.description)

that way you know the message number

then you can say

select case err.number of
case 2501: (do nothing just carry on) resume whatever
case else (display an error message)
end select


its a moot point whether 2501 is an ERROR, or just a warning, but it is treated as a trappable error - you also get an error 2501 if you cancel a report with no data.
 

ghudson

Registered User.
Local time
Today, 16:24
Joined
Jun 8, 2002
Messages
6,195
You simply need to trap for the error in your events error handler.

Code:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Form_Open_Error
    
    If IsNull(DateOfSale) Or DateOfSale = "" Then
        MsgBox "No Contract Exist"
        Cancel = True
    End If
    
Form_Open_Exit:
    Exit Sub
        
Form_Open_Error:
    If Err.Number = 2501 Then
        Exit Sub
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume Form_Open_Exit
    End If

End Sub
 

CEH

Curtis
Local time
Today, 15:24
Joined
Oct 22, 2004
Messages
1,187
Thanks
ghudson, I think thats more what I'm looking for....... Still one question on the placement of error handling code.......
Does it matter if it is on the "OnOpen" event of the second form or on the "OnClick" event of the command button that opens the second form?

I am thinking of changing the message box to a "YES" "NO" to add a contract if not there. The code would be similiar... but the question still is.......Which event is it better, (or proper), to put it on? Command button or ON open? And most important...... Why one event over the other?

Thanks
 

ejstefl

Registered User.
Local time
Today, 21:24
Joined
Jan 28, 2002
Messages
378
You need to put the error handling code on the sub that is causing the error. In your case, the sub that is casuing the error is the on_click event, therefore you need to trap the error there.
 

ghudson

Registered User.
Local time
Today, 16:24
Joined
Jun 8, 2002
Messages
6,195
You need to trap for the specific error number in the event that causes the error. All of your subs and functions should have an error handler simular to what I posted above.
 

Users who are viewing this thread

Top Bottom