我正在尝试从数据库中获取记录并将其填入excel。
这就是我获取数据并填充数据的方法(注意:查询是简化的,我不能只是ORDER BY DESC
):
Sub FillPersons(ByRef connection As ADODB.connection)
Dim recordSet As ADODB.recordSet
Set recordSet = New ADODB.recordSet
Dim sql As String
sql = "SELECT TOP 2 Id FROM Persons"
recordSet.activeconnection = connection
recordSet.Open sql
Dim a As Variant
If Not recordSet.EOF Then
a = recordSet.GetRows
a.Reverse (a)
Sheet1.Cells(10, 2).Resize(UBound(a, 1) + 1, UBound(a, 2) + 1).Value = a
End If
'Sheet1.Range("B10").CopyFromRecordset recordSet
recordSet.Close
connection .Close
Set connection = Nothing
End Sub
不幸的是,结果如下:
B10 B11
2 1
而不是
B10 B11
1 2
我试图反转a
,但不幸的是我无法让它发挥作用。
是否有获得正确结果的技巧?
提前致谢
答案 0 :(得分:1)
这个简短的'帮助'功能应该取代VB.Net的Array.Reverse method。
Function reverseArray(vals As Variant) As Variant
Dim tmp As Variant, i As Long
For i = LBound(vals, 1) To UBound(vals, 1) / 2
tmp = vals(UBound(vals, 1) - (i - LBound(vals, 1)), 1)
vals(UBound(vals, 1) - (i - 1), 1) = vals(i, 1)
vals(i, 1) = tmp
Next i
reverseArray = vals
End Function
用于确定翻转值的对立元素的数学应该适用于从零开始的数组和基于一的数组。
If Not recordSet.EOF Then
a = recordSet.GetRows
a = reverseArray(a)
Sheet1.Cells(10, 2).Resize(UBound(a, 1) + 1, UBound(a, 2) + 1).Value = a
End If
答案 1 :(得分:0)
您可以对其进行子查询,并将第一个和最后一个的结果连接为2行
像这样,我的数据不是你的,对不起SELECT LAST(OriginalQry1.TeammemberID) AS FirstR
FROM (SELECT TOP 2 TM.TeammemberID
FROM tbl_Teammembers AS TM) AS OriginalQry1
UNION ALL
SELECT First(OriginalQry2.TeammemberID) AS LastR
FROM (SELECT TOP 2 TM.TeammemberID
FROM tbl_Teammembers AS TM) AS OriginalQry2
然后你可以使用记录集中的副本吗?