访问vba来创建数组

时间:2014-07-07 02:20:51

标签: access-vba ms-access-2010

我有一个数组循环,想创建[mesAmis]为“Alpha,Beta,Charlie,Delta Echo,Foxtrot,Golf”。但是,它会返回朋友的名字以及19个逗号。

由于我的循环从我的记录集[tblFriendsList]读取,我们不知道我们有多少朋友。无论如何我可以在下面重写我的代码,所以我不必将其设置为19但使用记录计数?我已经尝试了很多次,但无法解决“monAmie”的束缚。提前致谢!

Dim monAmie(0 To 19) As String, lngPosition As Long
Dim mesAmis As String
If Not (rs2.EOF And rs2.BOF) Then
 rs2.MoveFirst 'Unnecessary in this case, but still a good habit
 ' LOOPING STARTS
 Do Until rs2.EOF = True        
 intCounter = intCounter + 1        
 monAmie(intCounter) = rs2!TaskDetailedInfo
 rs2.MoveNext
 Loop
 ' LOOPING ENDS
 mesAmis = Join(monAmie, ", ")
 MsgBox ("intCounter: " & intCounter)
 MsgBox ("mesAmis: " & vbCrLf & mesAmis)
' mesAmis is created as :
'Alpha, Beta, Charlie, Delta, Echo, Foxtrot, Golf
 Me.ShowMeAllYourFriends = mesAmis 'display the friends in this textbox
Else
 MsgBox "There are no records in the recordset."
End If

1 个答案:

答案 0 :(得分:1)

你可以试试这个。像这样调暗你的数组(而不是0到19):

Dim monAmie() As String

并将代码的相关部分更改为:

If Not (rs2.EOF And rs2.BOF) Then
    'Must move to the end of the recordset to get accurate RecordCount.
    rs2.MoveLast
    'Redimension array to number of records in recordset.
    Redim monAmie(rs2.RecordCount)

    rs2.MoveFirst 'Unnecessary in this case, but still a good habit
....

这应该将数组重新定义为记录集的确切大小。