I have a form with a command button to email a report.
The criteria for the report is based on the ProjectID on the form that has the email button.
This may be all wrong but it is what I've come up with so far. See code below for the command button.
Initially I had a problem with error code 2501.
I tried fixing it with the ErrHandler. (I don't think this really worked due to the problem below.)
If I click it once, and choose not to send the email, everything is fine.
If I click it again, the report stays open in the background and I will have to close it manually.
What do I need to fix in the code?
Second time I click the command button to email the report, I get this.
The criteria for the report is based on the ProjectID on the form that has the email button.
This may be all wrong but it is what I've come up with so far. See code below for the command button.
Initially I had a problem with error code 2501.
I tried fixing it with the ErrHandler. (I don't think this really worked due to the problem below.)
If I click it once, and choose not to send the email, everything is fine.
If I click it again, the report stays open in the background and I will have to close it manually.
What do I need to fix in the code?
Second time I click the command button to email the report, I get this.
Code:
Private Sub cmdbuttonEMAIL_Click()
On Error GoTo ErrHandler
Dim reportName As String
Dim criteria As String
Dim lngResult As String
DoCmd.RefreshRecord 'This saves a dirty record prior to opening the report.
lngResult = "ALCON," & vbCrLf & "Attached is a PA Request for:" & vbCrLf & "" & vbCrLf & _
" Bergen Number: " & Me![BergenNumber] & "." & vbCrLf & _
" Project Name: " & Me![ProjectName] & "." & vbCrLf & "" & vbCrLf & _
"Let me know if you need anything else."
reportName = "R_PANumRequest"
criteria = "[ProjectID] =" & ProjectID & ""
'Need to open and close report in order for this to work.
DoCmd.OpenReport reportName, acViewPreview, , criteria, acHidden
DoCmd.SendObject acSendReport, reportName, acFormatPDF, , , , "PA Request for - " & _
"" & Me![LocationNumber] & "", lngResult
'Need to close the report.
DoCmd.Close acReport, reportName, acSaveYes
ErrHandler_exit:
Exit Sub
ErrHandler:
If Err = 2501 Then
'User canceled the message - ignore this
Else
'Report the error
MsgBox Err.Description, vbExclamation
Resume ErrHandler_exit
End If
End Sub