View Full Version : Open Report from Form using Dlookup


RayEee
11-29-2008, 05:59 AM
I have a button on a form that when clicked should select which report to open based on the results of a query. I am getting an error "you canceled the previous operation". Can someone tell me what I doing wrong?

Thanks

Private Sub cmdOpenArmCalcReport_Click()
On Error GoTo Err_cmdOpenArmCalcReport_Click

Dim lngArmInd As Long
Dim strReportName As String

lngArmInd = DLookup("[arm_plan_id]", "qryModArmId")
If lngArmInd = 0 Then
strReportName = "rptModCalcFixed"
Else
strReportName = "rptModCalcArm"
End If

DoCmd.OpenReport "strReportName", acViewPreview

Exit_cmdOpenArmCalcReport_Click:
Exit Sub

Err_cmdOpenArmCalcReport_Click:
MsgBox Err.Description
Resume Exit_cmdOpenArmCalcReport_Click

End Sub

stopher
11-29-2008, 07:03 AM
I'd check the spelling of the arguments for the Dlookup function particularly the fieldname.

If you comment out the On Error line you'll get a fix on which line is giving you grief.
hth
Chris

gemma-the-husky
11-29-2008, 08:15 AM
or - you would get the cancelled message if the report opening didnt work either because the reports underlying query failed, or there was nodata, and the report was set to close onnodata

in that case its error 2501 (i think), and you can trap this to avoid getting the message

RayEee
11-29-2008, 11:44 AM
I found the solution on another post, my syntax in the criteria of the Dlookup was wrong. Here's what worked.

lngArmInd = DLookup("ArmId", "qryModArmId", "Loan_Number =""" & Me.txtLoanNo & """")