Access裡自訂類似MySQL group_concat 的函數

來源:互聯網
上載者:User

MySQL裡的group_concat很方便,MS-Access裡沒有。網上找到一個:http://access.mvps.org/access/modules/mdl0008.htm

Concatenate fields in same table Author(s) Dev Ashish   (Q)    I need to concatenate a field in the format "Value1; Value2; Value3" etc. for each unique value of another field in the same table.  How can I do this?(A)    Using the fConcatFld function,  in the Northwind database, the following query should return a concatenated list of all CustomerIDs if you group by ContactTitle. SELECT ContactTitle, fConcatFld("Customers","ContactTitle","CustomerID","string",[ContactTitle]) AS Customers FROM Customers GROUP BY ContactTitle;'************ Code Start **********'This code was originally written by Dev Ashish'It is not to be altered or distributed,'except as part of an application.'You are free to use it in any application,'provided the copyright notice is left unchanged.''Code Courtesy of'Dev Ashish'Function fConcatFld(stTable As String, _                    stForFld As String, _                    stFldToConcat As String, _                    stForFldType As String, _                    vForFldVal As Variant) _                    As String'Returns mutiple field values for each unique value'of another field in a single table'in a semi-colon separated format.''Usage Examples:'   ?fConcatFld(("Customers","ContactTitle","CustomerID", _'                "string","Owner")'Where  Customers     = The parent Table'       ContactTitle  = The field whose values to use for lookups'       CustomerID    = Field name to concatenate'       string        = DataType of ContactTitle field'       Owner         = Value on which to return concatenated CustomerID'Dim lodb As Database, lors As RecordsetDim lovConcat As Variant, loCriteria As StringDim loSQL As StringConst cQ = """"    On Error GoTo Err_fConcatFld        lovConcat = Null    Set lodb = CurrentDb        loSQL = "SELECT [" & stFldToConcat & "] FROM ["    loSQL = loSQL & stTable & "] WHERE "        Select Case stForFldType        Case "String":            loSQL = loSQL & "[" & stForFld & "] =" & cQ & vForFldVal & cQ        Case "Long", "Integer", "Double":    'AutoNumber is Type Long            loSQL = loSQL & "[" & stForFld & "] = " & vForFldVal        Case Else            GoTo Err_fConcatFld    End Select        Set lors = lodb.OpenRecordset(loSQL, dbOpenSnapshot)        'Are we sure that duplicates exist in stFldToConcat    With lors        If .RecordCount <> 0 Then            'start concatenating records            Do While Not .EOF                lovConcat = lovConcat & lors(stFldToConcat) & "; "                .MoveNext            Loop        Else            GoTo Exit_fConcatFld        End If    End With            'That's it... you should have a concatenated string now    'Just Trim the trailing ;    fConcatFld = Left(lovConcat, Len(lovConcat) - 2)Exit_fConcatFld:    Set lors = Nothing: Set lodb = Nothing    Exit FunctionErr_fConcatFld:    MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description    Resume Exit_fConcatFldEnd Function'************ Code End ********** 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.