I'm using a report to create a printout showing each manufacturing operation of a job along with information relevant to that operation. When there is a foil stamping operation a list of the foils used is to be printed. To create this list I've written a function - actually I copied a function that builds a list of the inks used in the printing operation and modified the code.
[I'm trying to anticipate questions. Please let me know if I've included too much info!]
When I run the report:
#Error
prints where I expect to see:
Foil: {foil1} ~ {foil2} ~ {foil3}
(The 3 foils in the job/order I'm using to test the report).
Here's information about the database, queries and report:
In a table ORDERS is a field ORDER_NO, it's an Integer.
qry1: Field: ORDERNO: ORDER_NO
This query collects information for each order of the job. (A job is a collection of one or more orders that are produced at the same time.)
I have to "convert" ORDER_NO to ORDERNO because the ERP database named the field for the JOB_NUMBER as ORDER_NO in one of the primary tables being used in the queries.
qry2: Field: ORDERNO Table: qry1
The values retrieved are correct, (right justified in the column) when I run the query
rpt1 has qry2 as the data source.
The text box that prints the foil list has:
Control Source: =IIf([MACH_NO] In (240,241,245,280,281,285,290,291,295),"Foil: " & FoilList([ORDERNO]),"")
When I remove ' & FoilList([ORDERNO])' the "Foil:" does print.
When I add back the FoilList stuff the output shows "#Error".
Module: Public Function FoilList(OrdNo As Integer) As String
I set a breakpoint at: Set db = CurrentDb, it's never reached - report is printed.
Here's the coding for the function:
Public Function FoilList(OrdNo As Integer) As String
Dim db As Database, rs As Recordset, strSQL As String, strList As String
Dim strDesc As String, strLastDesc As String
'Creates ~ delimited list of Foil Descriptions
'Return reference to current database.
Set db = CurrentDb
strSQL = "SELECT film_cst.film_desc from leaf"
strSQL = strSQL & " inner join orders on orders.spec_no = leaf.spec_no"
strSQL = strSQL & " inner join film_cst on film_cst.film_code = leaf.film_code"
strSQL = strSQL & " where orders.order_no = " & OrdNo & " and film_cst.film_type = 'L'"
strSQL = strSQL & " order by film_cst.film_desc"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
rs.MoveLast
rs.MoveFirst
Do While Not rs.EOF
strDesc = rs("film_desc")
'No Duplicates
If strDesc <> strLastDesc Then
If Len(strList) = 0 Then
strList = Trim(strDesc)
Else
strList = strList & " ~ " & Trim(strDesc)
End If
'Store the last description value
strLastDesc = strDesc
End If
rs.MoveNext
Loop
FoilList = strList
Else
FoilList = ""
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
--------------------
The SQL statement works (tested outside of Access). Unless, at the end of the WHERE clause the 'L' (single quotes) are not acceptable.
[I'm trying to anticipate questions. Please let me know if I've included too much info!]
When I run the report:
#Error
prints where I expect to see:
Foil: {foil1} ~ {foil2} ~ {foil3}
(The 3 foils in the job/order I'm using to test the report).
Here's information about the database, queries and report:
In a table ORDERS is a field ORDER_NO, it's an Integer.
qry1: Field: ORDERNO: ORDER_NO
This query collects information for each order of the job. (A job is a collection of one or more orders that are produced at the same time.)
I have to "convert" ORDER_NO to ORDERNO because the ERP database named the field for the JOB_NUMBER as ORDER_NO in one of the primary tables being used in the queries.
qry2: Field: ORDERNO Table: qry1
The values retrieved are correct, (right justified in the column) when I run the query
rpt1 has qry2 as the data source.
The text box that prints the foil list has:
Control Source: =IIf([MACH_NO] In (240,241,245,280,281,285,290,291,295),"Foil: " & FoilList([ORDERNO]),"")
When I remove ' & FoilList([ORDERNO])' the "Foil:" does print.
When I add back the FoilList stuff the output shows "#Error".
Module: Public Function FoilList(OrdNo As Integer) As String
I set a breakpoint at: Set db = CurrentDb, it's never reached - report is printed.
Here's the coding for the function:
Public Function FoilList(OrdNo As Integer) As String
Dim db As Database, rs As Recordset, strSQL As String, strList As String
Dim strDesc As String, strLastDesc As String
'Creates ~ delimited list of Foil Descriptions
'Return reference to current database.
Set db = CurrentDb
strSQL = "SELECT film_cst.film_desc from leaf"
strSQL = strSQL & " inner join orders on orders.spec_no = leaf.spec_no"
strSQL = strSQL & " inner join film_cst on film_cst.film_code = leaf.film_code"
strSQL = strSQL & " where orders.order_no = " & OrdNo & " and film_cst.film_type = 'L'"
strSQL = strSQL & " order by film_cst.film_desc"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
rs.MoveLast
rs.MoveFirst
Do While Not rs.EOF
strDesc = rs("film_desc")
'No Duplicates
If strDesc <> strLastDesc Then
If Len(strList) = 0 Then
strList = Trim(strDesc)
Else
strList = strList & " ~ " & Trim(strDesc)
End If
'Store the last description value
strLastDesc = strDesc
End If
rs.MoveNext
Loop
FoilList = strList
Else
FoilList = ""
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
--------------------
The SQL statement works (tested outside of Access). Unless, at the end of the WHERE clause the 'L' (single quotes) are not acceptable.