2016-02-10 127 views
1

如何连接记录集并根据记录数分隔记录?对于一条记录没有分隔符,“和”对于两条记录以及两条以上的记录与“”和“最后一条记录之前的逗号之间的逗号”。MS Access:根据记录数使用不同分隔符连接记录集

+1

请提供样本数据和预期的结果。使用http://www.sensefulsolutions.com/2010/10/format-text-as-table.html – Andre

+0

我有一个连接功能,导致一个编号列表: 1)彼得 2)保罗 3)玛丽 4)约瑟夫 我想要的结果是在句子的格式,如“彼得,保罗,玛丽和约瑟夫。”我需要的功能来正确打断的记录数量的句子。 如果记录集只有一个名称,结果将是“彼得” 如果记录集有两个名称,结果将是“彼得和保罗” 如果记录集有两个以上的名字,结果将是“彼得,保罗,玛丽,和约瑟夫。“ – Wendell

回答

0

我会做这样的事情:

Dim test() As String 
Dim arraySize As Integer 
Dim arrayCounter As Integer 
Dim rec as RecordSet 

Set rec = CurrentDB.OpenRecordset("SELECT MyField FROM MyTable") 

'I was always told that MoveFirst/MoveLast gave the most 
' accurate count of records 
rec.MoveFirst 
rec.MoveLast 
arraysize = rec.RecordCount 

'reDim the array, now that you know the size 
reDim test(arraySize) 

'Fill the array 
For i = 0 to arraysize - 1 
    test(i) = rec(0) 
next I 

'Set up the text string 
MyText = "" 

For j = 0 to arraysize - 1 
    If (j - 1) = arraysize Then 
    MyText = MyText & " and " & test(j) 
    else 
    MyText = MyText & ", " & test(j) 
    End If 
Next j 

'Trim off the first comma 
MyFinalText = Trim(Right(MyText, Len(MyText) - 2))