我编写了代码,用于从Oracle DB中的表中获取数据,并使用VBA转储到Excel工作表。
在Excel中,它会重复显示第一行。例如,如果从DB返回了45个不同的行,则在Excel工作表中,所有45行与数据库中的第一行相同。
如何将数据库中的行转换为Excel?
Sub Results()
Dim SQL As String
Dim OraDynaSet As Object
Dim i As Integer
SQL = "Select * from Employee where EmpID=20"
Set OraDynaSet = objDataBase.DBCreateDynaset(SQL, 0&)
If OraDynaSet.RecordCount > 0 Then
'There were records retrieved
OraDynaSet.MoveFirst
For ICOLS = 0 To OraDynaSet.Fields.Count - 1
.Cells(1, ICOLS + 1).Value = OraDynaSet.Fields(ICOLS).Name
Next ICOLS
'Loop the recordset for returned rows
For i = 0 To OraDynaSet.RecordCount - 1
For j = 0 To ICOLS - 1
.Cells(2 + i, j + 1) = OraDynaSet.Fields(j).Value
Next j
Next i
Else
MsgBox "No Matching records found"
End If
End Sub
答案 0 :(得分:0)
Dim SQL As String
Dim OraDynaSet As Object
Dim i As Integer
SQL = "Select * from Employee where EmpID=20"
Set OraDynaSet = objDataBase.DBCreateDynaset(SQL, 0&)
If OraDynaSet.RecordCount > 0 Then
'There were records retrieved
OraDynaSet.MoveFirst
For ICOLS = 0 To OraDynaSet.Fields.Count - 1
.Cells(1, ICOLS + 1).Value = OraDynaSet.Fields(ICOLS).Name
Next ICOLS
'Loop the recordset for returned rows
For i = 0 To OraDynaSet.RecordCount - 1
For j = 0 To ICOLS - 1
.Cells(2 + i, j + 1) = OraDynaSet.Fields(j).Value
Next j
OraDynaSet.Movenext
Next i
Else
MsgBox "No Matching records found"
End If
End Sub