Office中國(guó)論壇/Access中國(guó)論壇

 找回密碼
 注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

返回列表 發(fā)新帖
查看: 8203|回復(fù): 1
打印 上一主題 下一主題

[模塊/函數(shù)] 自定義函數(shù) - 字段字符串聚合連接函數(shù) DCONCAT

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2009-2-28 10:02:17 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
DCONCAT 函數(shù)

可以使用 DCONCAT 函數(shù)連接指定的一組記錄(域)中的一組值的字符串。
可以在 Visual Basic、宏、查詢(xún)表達(dá)式或者計(jì)算控件中使用 DCONCAT 函數(shù)。
(ADO/DAO不支持)。

例如,可以在查詢(xún)中使用 DCONCAT 函數(shù)。
select customerID, DCONCAT('orderID','orders','customerID=' & customerID
from customers

DCONCAT(expr, domain, [criteria],[delimiter])
DCONCAT 函數(shù)具有下列參數(shù)。

參數(shù)     說(shuō)明
expr      表達(dá)式,用于標(biāo)識(shí)要計(jì)算其記錄數(shù)的字段。它可以是標(biāo)識(shí)表或查詢(xún)中
          字段的字符串表達(dá)式,也可以是對(duì)該字段中的數(shù)據(jù)執(zhí)行計(jì)算的表達(dá)式。
          在 expr 中,可以包含表中字段的名稱(chēng)、窗體上的控件、常量或函數(shù)。
          如果 expr 包含一個(gè)函數(shù),那么它可能是內(nèi)置或用戶(hù)定義的函數(shù),但
          不是另一個(gè)域聚合函數(shù)或 SQL 聚合函數(shù)。
domain    字符串表達(dá)式,用于標(biāo)識(shí)組成域的一組記錄。它可以是表名稱(chēng)或不需
          要參數(shù)的查詢(xún)的查詢(xún)名稱(chēng)。
criteria  可選字符串表達(dá)式,用于限制對(duì)其執(zhí)行 DCONCAT 函數(shù)的目標(biāo)數(shù)據(jù)的范
          圍。例如,criteria 通常等價(jià)于 SQL 表達(dá)式中的 WHERE 子句,但它
          沒(méi)有單詞 WHERE。如果 criteria 被省略,那么 DCONCAT 函數(shù)將針對(duì)
          整個(gè)域計(jì)算 expr。任何包含在 criteria 中的字段必須也是 domain
          中的字段;否則 DCONCAT 函數(shù)將返回 錯(cuò)誤信息。
delimiter 可選字符串表達(dá)式,用于分隔各記錄的字符串值。默認(rèn)值為逗號(hào)"," 。



函數(shù)實(shí)現(xiàn)



'*******************************************************************************
' 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 中類(lèi)似EXCEL 中的MAX() 函數(shù)實(shí)現(xiàn)


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

.
ACMAIN - Access論壇回貼準(zhǔn)則(個(gè)人).
.
復(fù)
.
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://ctxi.cn .
.
http://ctxi.cn/home/space.php?uid=141646 .

本帖被以下淘專(zhuān)輯推薦:

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享分享 分享淘帖1 訂閱訂閱
2#
發(fā)表于 2009-4-9 10:12:33 | 只看該作者
謝謝!
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則

QQ|站長(zhǎng)郵箱|小黑屋|手機(jī)版|Office中國(guó)/Access中國(guó) ( 粵ICP備10043721號(hào)-1 )  

GMT+8, 2024-10-23 08:34 , Processed in 0.249536 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表