我是使用Recordsets
的新手,并且需要一些有关数据为何从数据库转换为Excel的帮助。我相信下面的代码,但不确定。
For iRow = 0 To intNumReturned - 1
For iCol = 0 To intNumColumns - 1
Debug.Print rsRecords(iCol, iRow)
Next iCol
Next iRow
预期的SQL字符串结果:
PROCESSOR | ACCOUNT NUMBER | LOAN AMOUNT | ORIGNATION DATE
ZJE xxxxxxx XXXXXX.XX 2018-01-01
ZJE xxxxxxx XXXXXX.XX 2018-02-06
如何将数据放入Excel工作表:
PROCESSOR | ACCOUNT NUMBER | LOAN AMOUNT | ORIGNATION DATE
ZJE ZJE ZJE ZJE
acct no acct no acct no acct no
loan amt loan amt loan amt loan amt
Org Date Org Date Org Date org date
下面是我当前正在使用的代码减去Connection和SQL字符串:
Sub RunSearch()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim connStr As String, strSQL As String
Dim begQ1 As String, endQ1 As String, begQ2 As String, endQ2 As String, begQ3 As String, endQ3 As String, begQ4 As String, endQ4 As String
Dim ctrl As Control
Dim rsRecords As Variant
Dim intNumReturned As Long, intNumColumns As Long, iCol As Long, iRow As Long, fldCount As Long, i As Long, rsCount As Long
Set wb = ThisWorkbook
Set wsVol = wb.Sheets("Volume By Processor")
Set wsDE = wb.Sheets("DE")
Set quarterYear = wsDE.Range("Quarter_Year")
Set Q = wsDE.Range("Quarter")
Set fDate = wsDE.Range("From_Date")
Set tDate = wsDE.Range("To_Date")
Do While Not rs.EOF
rs.MoveNext
rsRecords = rs.GetRows
intNumReturned = UBound(rsRecords, 2) + 1
intNumColumns = UBound(rsRecords, 1) + 1
Loop
For iRow = 0 To intNumReturned - 1
For iCol = 0 To intNumColumns - 1
Debug.Print rsRecords(iCol, iRow)
Next iCol
Next iRow
'\\\NEED TO FIGURE OUT NUMBER OF RECORDS TO
'\\\DEFINE LAST ROW/COLUMN OF RANGE???? PROBABLY A
'\\\SEPARATE QUESTION ON SO
wsVol.Range("B3:E1800") = rsRecords
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
我希望Excel工作表中的结果与SQL String的输出相匹配,我们将不胜感激。
答案 0 :(得分:2)
如果要转到记录集的另一个“条目”,则可以使用“ .MoveNext”方法。
会是这样的:
rsRecords.MoveFirst 'In this line you will back for the first entry
For iRow = 0 To intNumReturned - 1
For iCol = 0 To intNumColumns - 1
Debug.Print rsRecords(iCol)
Next iCol
rsRecords.MoveNext 'In this line you move to the next "row" as you want
Next iRow