情况:
我有这个代码,它将 Shipment ID 字段中的所有记录存储在一个名为 arrayShipmentID 的数组中。
Dim conn As New ADODB.Connection
Dim connStr As String
Dim rs As ADODB.Recordset
Dim ShipmentIDSQL As String
Dim arrayShipmentID() As Variant
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "P:\PathToTheAccessDatabase\" & "NewVersion.accdb" & ";"
conn.ConnectionString = connStr
conn.Open
' Store Shipment IDs
Set rs = New ADODB.Recordset
ShipmentIDSQL = "SELECT [Shipment ID] FROM 12Dec"
rs.Open ShipmentIDSQL, conn, adOpenStatic, adLockReadOnly, adCmdText
If Not rs.EOF Then
arrayShipmentID = rs.GetRows
End If
Dim i As Integer
For i = 0 To UBound(arrayShipmentID, 2)
Debug.Print arrayShipmentID(0, i)
Next i
Set rs = Nothing
为了确保它有效,我Debug.Print
数组的每个元素。
问题:
此代码在大多数情况下都适用,但出于某种原因,有时我会得到一个超出范围的值'下标' ({而不是181)在MsgBox UBound(arrayShipmentID, 2)
当然,在这种情况下,Debug.Print不会在立即窗口中显示任何内容。
任何想法可以来自哪里?
答案 0 :(得分:0)
使用DAO记录集,您经常需要找到记录来检索所有记录。 ADO也许就是这种情况:
for t in time:
for i in range(kk, len(TBM)-1):
if (TBM[i,1] > t[j] and TBM[i,1] < t[j+1]):
sums = sums + TBM[i,2]
kk = kk + 1
means[t] = sums / kk
另外,我通常将变量声明为一个简单的Variant:
RecordCount = ' Obtain true count of records.
arrayShipmentID = rs.GetRows(RecordCount)