操作步驟:
1.先在模塊中定義了函式:
Public Function GroupConcat(sColumn As String, sTable As String, Optional sCriteria As String, Optional sDelimiter As String = ",")
On Error GoTo ErrHandler
Dim rs As New ADODB.Recordset
Dim sSQL As String
Dim sResult As String
sResult = ""
sSQL = "select " & sColumn & " from " & sTable
If sCriteria <> "" Then
sSQL = sSQL & " where " & sCriteria
End If
rs.Open sSQL, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
Do While Not rs.EOF
If sResult <> "" Then
sResult = sResult & sDelimiter
End If
sResult = sResult & rs.Fields(0).Value
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
GroupConcat = sResult
Exit Function
ErrHandler:
If rs.State <> adStateClosed Then
rs.Close
End If
Set rs = Nothing
GroupConcat = Err.Number & " : " & Err.Description
End Function
2.寫查詢并執行
SELECT T_Person_Course.Name, GroupConcat('Course','T_Person_Course','Name=' & "'" & Name & "'") AS Courses
FROM T_Person_Course
GROUP BY name
執行完查詢,course原欄位型別是“備注”,資料長度較長,合并后變成了“文本”,導致資料截斷,請教大神怎么解決?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/62360.html
標籤:Access
