MS Access:根据记录数将不同的分隔符连接到记录集

时间:2016-02-10 16:30:54

标签: ms-access access-vba

如何连接记录集并根据记录数设置分隔符?一条记录没有分隔符,两条记录没有“和”,两条记录之间的逗号和最后一条记录前的“,和”。

1 个答案:

答案 0 :(得分: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))