Runtime error when i open a form (1 Viewer)

vbaInet

AWF VIP
Local time
Today, 19:26
Joined
Jan 22, 2010
Messages
26,374

spikepl

Eledittingent Beliped
Local time
Today, 20:26
Joined
Nov 3, 2010
Messages
6,142
#21 That was my suspicion too, so I just wanted to verify it. If that is the case I'd recommend that OP read the links provided.
 

rick roberts

Registered User.
Local time
Today, 19:26
Joined
Jan 22, 2003
Messages
160
this is the routine in its entireity

Private Sub cmdLetter1_Click()
'Send a message if tickbox is already ticked
On Error GoTo PROC_ERR

If CheckLetter1 = True Then
MsgBox "The tickbox is ticked which indicates that a letter or email has been sent." _
& Chr(13) & "If you wish to send another please untick the box. ", vbOKOnly, Warning
End If

PROC_ERR:
MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
End

'Check whether to email or post
Email.SetFocus
'Stop error if email isnt sent
On Error GoTo Error

'Email
If Email.Text <> "" Then
SendEmail 'Function
Error:
Resume Next

Else
'Post
'Had to use this routine to make the report print in colour
Dim stDocName As String
stDocName = "rptCustLetter1"
DoCmd.OpenReport stDocName, acViewPreview
With Reports(rptCustLetter1).Printer
.ColorMode = acPRCMColor
End With
'end of colour enforcement
DoCmd.OpenReport "rptCustLetter1", acViewNormal
DoCmd.Close
End If
'Tick to indicate letter is sent
SentStage1 = Date
CheckLetter1.Value = True

Calculator 'Function
DoCmd.Restore
End Sub

the error appears after the OK button is clicked in the message box
 

vbaInet

AWF VIP
Local time
Today, 19:26
Joined
Jan 22, 2010
Messages
26,374
Your error handling is wrong. Have you read the links provided?
 

rick roberts

Registered User.
Local time
Today, 19:26
Joined
Jan 22, 2003
Messages
160
now im beginning to feel paranoid - ive read the links and dont see what im doing wrong other than the error handling is placed in the wrong part of the code
 

vbaInet

AWF VIP
Local time
Today, 19:26
Joined
Jan 22, 2010
Messages
26,374
No cause for alarm :) A simple error handling example is as follows:
Code:
Private Sub cmdLetter1_Click()
On Error GoTo PROC_ERR
'Send a message if tickbox is already ticked
 
[COLOR=red]--- Your normal code here ----[/COLOR]
 
[COLOR=blue]PROC_ERR_EXIT[/COLOR]:
    Exit Sub
 
PROC_ERR:
    Msgbox Err.Number & ": " & Err.Description
    Resume [COLOR=blue]PROC_ERR_EXIT[/COLOR]
 
End Sub
As you can see, your normal code should not be placed under your error label, that's where you handle errors. Also, note the blue lines of code, can you see what it's doing?
 

rick roberts

Registered User.
Local time
Today, 19:26
Joined
Jan 22, 2003
Messages
160
i put my error handler just below where i know the error occurs so as not to confuse it with the rest of the code -- are you saying it should go as the last line? - i tried this but it doesnt give an error message of any kind and when i OK the message it carries on the procedure and prints the letter - which it shouldnt if the box is ticked -- should there be an End instead of Resume?

Private Sub cmdLetter1_Click()
'Send a message if tickbox is already ticked
On Error GoTo PROC_ERR

If CheckLetter1 = True Then
MsgBox "The tickbox is ticked which indicates that a letter or email has been sent." _
& Chr(13) & "If you wish to send another please untick the box. ", vbOKOnly, Warning
End
End If

'Check whether to email or post
Email.SetFocus
'Stop error if email isnt sent
On Error GoTo Error

'Email
If Email.Text <> "" Then
SendEmail 'Function
Error:
Resume Next

Else
'Post
'Had to use this routine to make the report print in colour
Dim stDocName As String
stDocName = "rptCustLetter1"
DoCmd.OpenReport stDocName, acViewPreview
With Reports(rptCustLetter1).Printer
.ColorMode = acPRCMColor
End With 'end of colour enforcement

DoCmd.OpenReport "rptCustLetter1", acViewNormal
DoCmd.Close
End If
'Tick to indicate letter is sent
SentStage1 = Date
CheckLetter1.Value = True

Calculator 'Function
DoCmd.Restore

PROC_ERR_EXIT:
Exit Sub

PROC_ERR:
MsgBox Err.Number & ": " & Err.Description
Resume PROC_ERR_EXIT

End Sub
 

vbaInet

AWF VIP
Local time
Today, 19:26
Joined
Jan 22, 2010
Messages
26,374
I clearly wrote a line that reads, "--- Your normal code here ---" in the code I gave you above.
 

rick roberts

Registered User.
Local time
Today, 19:26
Joined
Jan 22, 2003
Messages
160
no but when that procedure is satisfied then i presume the program stops there since it isnt asked to do any more?
th message box appears to inform of the situation then closes and the program goes no further??
 

vbaInet

AWF VIP
Local time
Today, 19:26
Joined
Jan 22, 2010
Messages
26,374
And I've mentioned it over and over again, ALL your functions/subs need error handling.
 

rick roberts

Registered User.
Local time
Today, 19:26
Joined
Jan 22, 2003
Messages
160
thanks for your help so far - but i was kind of looking for an answer - rather than replies
 

vbaInet

AWF VIP
Local time
Today, 19:26
Joined
Jan 22, 2010
Messages
26,374
An answer to what exactly?

How many lines of code do you have in your database?
 

rick roberts

Registered User.
Local time
Today, 19:26
Joined
Jan 22, 2003
Messages
160
there are 14 excluding one liners that recalc and set proper case etc.
 

Users who are viewing this thread

Top Bottom