Help on list box query (1 Viewer)

jrantala

New member
Local time
Today, 00:09
Joined
Nov 1, 2017
Messages
6
Hello from Minnesota in the US,

I have a query that returns all rows for a given asset and the respective fuel types that can be combusted in the asset.

That is, asset 1 can use distillate oil #2, distillate oil #6 and natural gas. There are three records in the query results, one for each fuel/asset combination. Asset 2 might have 2 fuels, and asset 3 only 1.

I want the form to display any and all fuels that are associated when asset 1 is displayed. I don't know how to tell either the query or the box to concatenate each row to form a single results.

The screen literal and text box contents would look like:
Asset 1: distillate oil #2, distillate oil #6, natural gas
Asset 2: distillate oil #2, natural gas
Asset 3: diesel

I'm struggling with the correct expression and syntax to join the results from multiple rows together.

Can you help me?

Julie
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:09
Joined
Aug 30, 2003
Messages
36,123
Great minds think alike, some just have faster fingers. :p
 

jrantala

New member
Local time
Today, 00:09
Joined
Nov 1, 2017
Messages
6
Thanks both for your speedy response. I will try it out tomorrow!

Julie
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:09
Joined
Aug 30, 2003
Messages
36,123
No problem, post back if you get stuck.
 

jrantala

New member
Local time
Today, 00:09
Joined
Nov 1, 2017
Messages
6
Hi again!

I am indeed stuck on this again. I think it has to be close, but I don't know where my problem is. I get this error message: "Error 3061: Too few parameters. Expected 1."

Below I have copied:
  • Query SQL code
  • Control box expression for 'Fuel Type'
  • Allen Browne coding for the function

Thanks for any help on getting past this error,

Julie

The SQL from the query is below, I get the appropriate result set from this:
SELECT EmissionsSCC_Asset.AssetID, EmissionsFuelType.FuelTypeDescription, EmissionsSCC.EmissionsFuelTypeID, EmissionsSCC_Asset.AssetTypeID
FROM EmissionsFuelType INNER JOIN (EmissionsSCC LEFT JOIN EmissionsSCC_Asset ON EmissionsSCC.EmissionsSCC_ID = EmissionsSCC_Asset.EmissionsSCC_ID) ON EmissionsFuelType.EmissionsFuelTypeID = EmissionsSCC.EmissionsFuelTypeID;

The expression for the 'FuelTypes' control is:
=ConcatRelated([AssetID],"Qry_Load_Fuels_part2")

The AllenBrowne module text is:
Public Function ConcatRelated(strField As String, _
strTable As String, _
Optional strWhere As String, _
Optional strOrderBy As String, _
Optional strSeparator = ", ") As Variant
On Error GoTo Err_Handler
'Purpose: Generate a concatenated string of related records.
'Return: String variant, or Null if no matches.
'Arguments: strField = name of field to get results from and concatenate.
' strTable = name of a table or query.
' strWhere = WHERE clause to choose the right values.
' strOrderBy = ORDER BY clause, for sorting the values.
' strSeparator = characters to use between the concatenated values.
'Notes: 1. Use square brackets around field/table names with spaces or odd characters.
' 2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
' 3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
' 4. Returning more than 255 characters to a recordset triggers this Access bug:

Dim rs As DAO.Recordset 'Related records
Dim rsMV As DAO.Recordset 'Multi-valued field recordset
Dim strSql As String 'SQL statement
Dim strOut As String 'Output string to concatenate to.
Dim lngLen As Long 'Length of string.
Dim bIsMultiValue As Boolean 'Flag if strField is a multi-valued field.

'Initialize to Null
ConcatRelated = Null

'Build SQL string, and get the records.
strSql = "SELECT EmissionsSCC.EmissionsFuelTypeID"
strSql = strSql & " FROM EmissionsFuelType INNER JOIN (EmissionsSCC LEFT JOIN EmissionsSCC_Asset ON EmissionsSCC.EmissionsSCC_ID = EmissionsSCC_Asset.EmissionsSCC_ID) ON EmissionsFuelType.EmissionsFuelTypeID = EmissionsSCC.EmissionsFuelTypeID"
strSql = strSql & " WHERE EmissionsSCC.EmissionsSCC_ID = EmissionsSCC_Asset.EmissionsSCC_ID and EmissionsFuelType.EmissionsFuelTypeID = EmissionsSCC.EmissionsFuelTypeID and EmissionsSCC_Asset.AssetID = Emissions.AssetID and EmissionsSCC_Asset.AssetTypeID = 'Emissions'"

Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
'Determine if the requested field is multi-valued (Type is above 100.)
bIsMultiValue = (rs(0).Type > 100)

'Loop through the matching records
Do While Not rs.EOF
If bIsMultiValue Then
'For multi-valued field, loop through the values
Set rsMV = rs(0).Value
Do While Not rsMV.EOF
If Not IsNull(rsMV(0)) Then
strOut = strOut & rsMV(0) & strSeparator
End If
rsMV.MoveNext
Loop
Set rsMV = Nothing
ElseIf Not IsNull(rs(0)) Then
strOut = strOut & rs(0) & strSeparator
End If
rs.MoveNext
Loop
rs.Close

'Return the string without the trailing separator.
lngLen = Len(strOut) - Len(strSeparator)
If lngLen > 0 Then
ConcatRelated = Left(strOut, lngLen)
End If

Exit_Handler:
'Clean up
Set rsMV = Nothing
Set rs = Nothing
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
Resume Exit_Handler
End Function
 

Users who are viewing this thread

Top Bottom