Coding Error

kim40

Registered User.
Local time
Today, 08:47
Joined
Aug 24, 2012
Messages
30
Since I don't know about writing codes I am using NorthWest Database for this part of the project. I saw a previous post and have been able to fix some. I still have problems. I have this part of the code Sub PrintReports(ReportView As AcView)
' This procedure used in Preview_Click and Print_Click Sub procedures.
' Preview or print report selected in the ReportToPrint option group.
' Then close the Print Sales Reports Dialog form.
Dim strReportName As String
Dim strReportFilter As String
Dim lOrderCount As Long

' Determine report filtering
If Nz(Me.lstReportFilter) <> "" Then
strReportFilter = "([CustomerGroupingField] = """ & Me.lstReportFilter & """)"
End If

' Determine reporting time frame
Select Case Me.lstOrderPeriod
Case ByYear
strReportName = "Customer"
lOrderCount = DCountWrapper("*", "Order Analysis", "[Year]=" & Me.cbYear)
Case ByMonth
strReportName = "Customer"
lOrderCount = DCountWrapper("*", "Order Analysis", "[Year]=" & Me.cbYear & " AND [Month]=" & Me.cbMonth)
End Select

If lOrderCount > 0 Then
TempVars.Add "Group By", Me.lstOrderReports.Value
TempVars.Add "Display", DLookupStringWrapper("[Display]", "Customer Reports", "[Group By]='" & Nz(Me.lstOrderReports) & "'")
TempVars.Add "Year", Me.cbYear.Value
TempVars.Add "Month", Me.cbMonth.Value
eh.TryToCloseObject
DoCmd.OpenReport strReportName, ReportView, , strReportFilter, acWindowNormal
Else
MsgBoxOKOnly NoSalesInPeriod

End If
End Sub
But When I try to preview I get 2 errors i Compile error on eh.TryToCloseObject on this part of the error and The epression click you entered as the event produced variable not defined

tHANKS
 
First, please use code tags if you're posting code so identing is preserved (makes it easier to read).

The error on "eh.TryToCloseObject" is expected, there's no "eh" object in your code and TryToCloseObject is not a built in option.

Several things wrong or could be done better in your code, I made some comments/changes in your code.

Code:
Sub PrintReports(ReportView As [COLOR="Red"]String[/COLOR])[COLOR="SeaGreen"] 'As AcView would mean you pass a View to the function but from the rest of your code it's a String.[/COLOR]' This procedure used in Preview_Click and Print_Click Sub procedures.
' Preview or print report selected in the ReportToPrint option group.
' Then close the Print Sales Reports Dialog form.

[COLOR="Red"]On error goto ErrorInThisFunction[/COLOR] [COLOR="Green"]'some kind of error handler to give better error control.[/COLOR]Dim strReportName As String
Dim strReportFilter As String
Dim lOrderCount As Long

' Determine report filtering
   If Nz(Me.lstReportFilter[COLOR="Red"], ""[/COLOR]) <> "" Then [COLOR="Green"]'You missed the replace part of the NZ function[/COLOR]
      strReportFilter = "([CustomerGroupingField] = """ & Me.lstReportFilter & """)"
   End If

' Determine reporting time frame
   [COLOR="Red"]strReportName = "Customer"[/COLOR] [COLOR="Green"]'place here and you only need to change once[/COLOR]
   Select Case Me.lstOrderPeriod
      Case ByYear
 [COLOR="Green"] 'remove       strReportName = "Customer"[/COLOR]
         lOrderCount = DCountWrapper("*", "Order Analysis", "[Year]=" & Me.cbYear)
      Case ByMonth
 [COLOR="Green"] 'remove       strReportName = "Customer"[/COLOR]
         lOrderCount = DCountWrapper("*", "Order Analysis", "[Year]=" & Me.cbYear & " AND [Month]=" & Me.cbMonth)
   End Select

   If lOrderCount > 0 Then
      TempVars.Add "Group By", Me.lstOrderReports.Value
      TempVars.Add "Display", DLookupStringWrapper("[Display]", "Customer Reports", "[Group By]='" & Nz(Me.lstOrderReports) & "'")
      TempVars.Add "Year", Me.cbYear.Value
      TempVars.Add "Month", Me.cbMonth.Value
[COLOR="Red"]' remove eh.TryToCloseObject[/COLOR]
      DoCmd.OpenReport strReportName, ReportView, , strReportFilter, acWindowNormal
   Else
      MsgBoxOKOnly NoSalesInPeriod [COLOR="green"]'This would mean you have a variable NoSalesInPeriod that is not declared (in this function).[/COLOR]   End If

[COLOR="Red"]ExitPrintReports: [COLOR="Green"]'Exit function if no error[/COLOR]
   Exit Sub

ErrorInThisFunction: [COLOR="green"]'Print the error and error number so you can search [/COLOR]
    MsgBox "Error " & Err.Number & ", " & Err.Description
    Resume Exit_btnZoekItemInMateriaal_Click

End Sub[/COLOR]

If you have "Option explicit" on top of your module you need to declare your variables. If the option is set, variable "NoSalesInPeriod" would result in a error variable not defined.
 
Thank you so much I will go through the code and use your suggesstions to rectify. I will tell you how it works
 
I have tried the code but I need a liitle more help. I am not getting any error messages but my report does not open
thanks
 

Users who are viewing this thread

Back
Top Bottom