【问题标题】:access vba to create array访问 vba 以创建数组
【发布时间】:2014-07-07 02:20:50
【问题描述】:

我有一个带有数组的循环,并希望将 [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

【问题讨论】:

标签: ms-access vba ms-access-2010


【解决方案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
....

这应该将您的数组重新调整为记录集的确切大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2015-06-24
    相关资源
    最近更新 更多