J
janf
I am trying to get the top 3 values from a table using Allen Browne's
"ConcatRelated" function, but there seems to be something I don't get right.
The property of the field in the table is "text", but it holds (always)
three or (sometimes) two numbers (or it is empty (not 0)). (The same "error"
shows if the field property is set to "number".
The line of code (4) below is OK, if I can sort it descending. I can limit
the text field in the report to show what i want, but i need only the three
highest values.
Can someone help, please.
janf
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:
' http://allenbrowne.com/bug-16.html
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.
'1
strSql = "SELECT TOP 3 " & strField & " FROM " & strTable
' returns <empy> (nothing)
'2
' strSql = "SELECT TOP 3 " & val(strField) & " FROM " & strTable
' returns "0 | 0 | 0" (3 records)
'3
' strSql = "SELECT " & val(strField) & " FROM " & strTable
' returns "0 | 0 | 0 | 0 | 0 | 0" (all records)
'4
' strSql = "SELECT " & strField & " FROM " & strTable
' returns "223 | 231 | 229 | 240 | 215 | 238" (all
records)
' WANTED: "240 | 238 | 231" (Top 3 records)
If strWhere <> vbNullString Then
strSql = strSql & " WHERE " & strWhere
End If
If strOrderBy <> vbNullString Then
strSql = strSql & " ORDER BY " & strOrderBy
End If
In query:
Expr1: ConcatRelated("[25-sk]";"Resultater";"ID = " & [M-ID])
' strField = "[25-sk]" (Properties: text, 3 chrs, input mask = 009;0;_)
' strTable = "Resultater"
' strWhere = "ID = " & [M-ID]
"ConcatRelated" function, but there seems to be something I don't get right.
The property of the field in the table is "text", but it holds (always)
three or (sometimes) two numbers (or it is empty (not 0)). (The same "error"
shows if the field property is set to "number".
The line of code (4) below is OK, if I can sort it descending. I can limit
the text field in the report to show what i want, but i need only the three
highest values.
Can someone help, please.
janf
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:
' http://allenbrowne.com/bug-16.html
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.
'1
strSql = "SELECT TOP 3 " & strField & " FROM " & strTable
' returns <empy> (nothing)
'2
' strSql = "SELECT TOP 3 " & val(strField) & " FROM " & strTable
' returns "0 | 0 | 0" (3 records)
'3
' strSql = "SELECT " & val(strField) & " FROM " & strTable
' returns "0 | 0 | 0 | 0 | 0 | 0" (all records)
'4
' strSql = "SELECT " & strField & " FROM " & strTable
' returns "223 | 231 | 229 | 240 | 215 | 238" (all
records)
' WANTED: "240 | 238 | 231" (Top 3 records)
If strWhere <> vbNullString Then
strSql = strSql & " WHERE " & strWhere
End If
If strOrderBy <> vbNullString Then
strSql = strSql & " ORDER BY " & strOrderBy
End If
In query:
Expr1: ConcatRelated("[25-sk]";"Resultater";"ID = " & [M-ID])
' strField = "[25-sk]" (Properties: text, 3 chrs, input mask = 009;0;_)
' strTable = "Resultater"
' strWhere = "ID = " & [M-ID]