1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| ' ---
' FUSION DE VALEURS D'ENREGISTREMENTS
' ---
' Entrée : strSource <- Table, requête ou instruction SQL
' varFields <- Liste des champs à fusionner.
' Ex. : array("Nom", "Prénom")
' strRowDelimiter <- Séparateur pour les lignes.
' strFieldDelimiter <- Séparateur pour les colonnes.
' blnIncludeNulls <- True pour conserver les valeurs Null.
' Sortie : Résultat concaténé
'
Function MergeRows( _
ByVal strSource As String, _
ByVal varFields As Variant, _
Optional ByVal strRowDelimiter As String = ", ", _
Optional ByVal strFieldDelimiter As String = " ", _
Optional ByVal blnIncludeNulls = False) As String
' Quelques variables
Dim rst As DAO.Recordset
Dim varField As Variant
Dim strResult As String
Dim strVal As String
' Ouvrir la source
Set rst = CurrentDb.OpenRecordset(strSource, dbOpenSnapshot)
' Concaténer la valeur du même champ, pour tous les enregistrements
' de la source
strResult = ""
While Not rst.EOF
' Concaténer tous les champs
strVal = ""
For Each varField In varFields
If strVal <> "" Then strVal = strVal & strFieldDelimiter
strVal = strVal & rst(varField)
Next
strVal = Trim(strVal)
' Ajout de l'enregistrement au résultat
If (strVal <> "") Or blnIncludeNulls Then
If strResult <> "" Then strResult = strResult & strRowDelimiter
strResult = strResult & strVal
End If
' Enregistrement suivant
rst.MoveNext
Wend
' Libérer les ressources
rst.Close
Set rst = Nothing
MergeRows = strResult
End Function |
Partager