我很高兴使用Allen Browne的VBA:
Option Compare Database
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.
strSql = "SELECT " & strField & " FROM " & strTable
If strWhere <> vbNullString Then
strSql = strSql & " WHERE " & strWhere
End If
If strOrderBy <> vbNullString Then
strSql = strSql & " ORDER BY " & strOrderBy
End If
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
这允许用户使用ConcatRelated("variable", "table", criteria)
我目前的需要我认为需要修改VBA,我对此一无所知,或者我目前没有想到的一些SQL。
我目前qryDoseDays
SubjectNumber
,RECDATE
来自tblDump
的SQL:
SELECT d.SubjectNumber, d.RECDATE, Count(d.RECTIME) AS DailyDoses
FROM (SELECT dd.SubjectNumber, dd.RECDATE, dd.RECTIME FROM tblDump AS dd LEFT JOIN tblL ON dd.SubjectNumber=tblL.SubjectNumber WHERE dd.RECDATE<>tblL.[2]) AS d
GROUP BY d.SubjectNumber, d.RECDATE
HAVING Count(d.RECTIME)>2
ORDER BY d.SubjectNumber, d.RECDATE;
因此,有变量SubjectNumber
,RECDATE
和RECTIME
。我需要按RECTIME
和SubjectNumber
分组的每行连接RECDATE
个值。我似乎只能将其功能用于SubjectNumber
或RECDATE
,但不能同时使用这两种功能。
答案 0 :(得分:0)
将您的SubjectNumber
和RECDATE
值作为第三个参数( strWhere )传递给ConcatRelated()
。
SELECT
q.SubjectNumber,
q.RECDATE,
q.DailyDoses,
ConcatRelated
(
"RECTIME",
"tblDump",
"SubjectNumber=" & q.SubjectNumber
& " AND RECDATE="
& Format(q.RECDATE, "\#yyyy-m-d\#")
) AS RECTIME_list
FROM qryDoseDays AS q
我假设SubjectNumber
是数字数据类型,RECDATE
是日期/时间类型。如果这些假设中的任何一个是错误的,那么您将不得不调整SQL来处理实际的数据类型。