|
本帖最后由 红尘如烟 于 2009-6-23 00:41 编辑
写了一个通用函数:
- Public Function JoinField(TableName As String, FieldName As String, WhereCondition As String, _
- Optional Delimiter As String = " ") As String
- On Error GoTo ErrorHandler
- 'TableName 必需,表名
- 'FieldName 必需,要合并记录的字段名
- 'WhereCondition 必需,分组条件表达式
- 'Delimiter 可选,分隔符,默认为空格符
- Dim rst As New ADODB.Recordset
- Dim strSQL As String
- strSQL = " SELECT " & FieldName & _
- " FROM " & TableName & _
- " WHERE " & WhereCondition & _
- " ORDER BY " & FieldName
- rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
- Do Until rst.EOF
- If JoinField = "" Then
- JoinField = rst(FieldName)
- Else
- JoinField = JoinField & Delimiter & rst(FieldName)
- End If
- rst.MoveNext
- Loop
- rst.Close
- Set rst = Nothing
- ExitFunction:
- Exit Function
- ErrorHandler:
- MsgBox Err.Number & Err.Description
- Resume ExitFunction
- End Function
复制代码 在查询中的运用实例:- SELECT DISTINCT姓名,处方名, JoinField("表1","药物","[姓名]='" & [姓名] & "' AND [处方名]='" & [处方名] & "'",",") AS 药物
- FROM 表1
- ORDER BY 姓名,.处方名;
复制代码 |
|