设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 7716|回复: 1
打印 上一主题 下一主题

[模块/函数] 自定义函数 - 字段字符串聚合连接函数 DCONCAT

[复制链接]
跳转到指定楼层
1#
发表于 2009-2-28 10:02:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
DCONCAT 函数

可以使用 DCONCAT 函数连接指定的一组记录(域)中的一组值的字符串。
可以在 Visual Basic、宏、查询表达式或者计算控件中使用 DCONCAT 函数。
(ADO/DAO不支持)。

例如,可以在查询中使用 DCONCAT 函数。
select customerID, DCONCAT('orderID','orders','customerID=' & customerID
from customers

DCONCAT(expr, domain, [criteria],[delimiter])
DCONCAT 函数具有下列参数。

参数     说明
expr      表达式,用于标识要计算其记录数的字段。它可以是标识表或查询中
          字段的字符串表达式,也可以是对该字段中的数据执行计算的表达式。
          在 expr 中,可以包含表中字段的名称、窗体上的控件、常量或函数。
          如果 expr 包含一个函数,那么它可能是内置或用户定义的函数,但
          不是另一个域聚合函数或 SQL 聚合函数。
domain    字符串表达式,用于标识组成域的一组记录。它可以是表名称或不需
          要参数的查询的查询名称。
criteria  可选字符串表达式,用于限制对其执行 DCONCAT 函数的目标数据的范
          围。例如,criteria 通常等价于 SQL 表达式中的 WHERE 子句,但它
          没有单词 WHERE。如果 criteria 被省略,那么 DCONCAT 函数将针对
          整个域计算 expr。任何包含在 criteria 中的字段必须也是 domain
          中的字段;否则 DCONCAT 函数将返回 错误信息。
delimiter 可选字符串表达式,用于分隔各记录的字符串值。默认值为逗号"," 。



函数实现



'*******************************************************************************
' DCONCAT(expr, domain, [criteria],[delimiter])
'
' Function:  to concate the columns string like group_concat() in Access.
' Parameter:
'             expr      : string, expression which can recognized by SQL.
'             domain    : string, the row source, can be another query.
'             criteria  : the ceritera which will be treated as the where clause.
'             delimiter : delimiter between the column value, default is ",".
'
' Return:     string, a String of concated columns,
'                     if err, return the err code + desc.
'
' history:
'   2009-Feb-28 ACMAIN New Creation
'
'*********************************************************************************


Public Function DCONCAT(sExpr As String, sDomain 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 " & sExpr & " from (" & sDomain & ")"
    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
   
    DCONCAT = sResult
   
    Exit Function
   
ErrHandler:
    If rs.State <> adStateClosed Then
        rs.Close
    End If
    Set rs = Nothing
   
    DCONCAT = Err.Number & " : " & Err.Description
   
   
End Function




VBA 中类似EXCEL 中的MAX() 函数实现


******************
*  一切皆有可能  *
******************

.
ACMAIN - Access论坛回贴准则(个人).
.

.
QQ群 48866293 / 12035577 / 7440532 / 13666209
http://forum.csdn.net/BList/OtherDatabase .
http://www.accessbbs.cn/bbs/index.php .
http://www.accessoft.com/bbs/index.asp .
http://www.access-programmers.co.uk/forums .
http://www.office-cn.net .
.
http://www.office-cn.net/home/space.php?uid=141646 .

本帖被以下淘专辑推荐:

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享 分享淘帖1 订阅订阅
2#
发表于 2009-4-9 10:12:33 | 只看该作者
谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-27 09:18 , Processed in 0.096701 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表