在不知道记录条数的情况下如何设置解析查询结果范围?
How to set the parsing query result range without knowing the number of records?
我 运行 SQL 通过 excel-vba& ADO 在 excel 中查询。
我使用循环解析结果
然后,我发现我必须知道在解析结果之前会生成多少条记录sql。
实际上,在生成查询之前,我并不知道查询结果记录的数量。
任何函数或方法可以让我知道,以便我可以放入循环语句?
(我了解 .Fields() .Properties() ,但不起作用)
Sub sbADO()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
'You can provide the full path of your external file as shown below
'DBPath ="C:\InputData.xlsx"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
sSQLSting = select ....' it is too long so i choose to skip
Set rs = Conn.Execute(sSQLSting)
Do While Not rs.EOF
'Officer = rs.Fields(i).Value
For j = 5 To 29 ' THIS IS THE MAIN CODE I NEED TO IMPROVE
'worksheet1.Cells(7, 11) = rs.Properties.Count
worksheet1.Cells(j, 1) = rs.Fields(0).Value
worksheet1.Cells(j, 3) = rs.Fields(2).Value
worksheet1.Cells(j, 4) = rs.Fields(3).Value
worksheet1.Cells(j, 7) = rs.Fields(6).Value
' Insert data to your worksheet here
rs.MoveNext
Next j
Loop
rs.Close
End Sub
For j = 5 To 29 '
next j
这是我应该设置的范围,影响工作表解析的范围。本例查询记录结果一共有24条。如果j设置太大(比如30),会提示3021 error -BOF & EOF 应该是真的。因此,范围应该符合记录的数量。
j 不需要循环。只需在所有单元分配之后和 do 循环结束之前递增 j 即可。
Sub sbADO()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
'You can provide the full path of your external file as shown below
'DBPath ="C:\InputData.xlsx"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
sSQLSting = select .... ' it is too long so i choose to skip
Set rs = Conn.Execute(sSQLSting)
j = 5
Do While Not rs.EOF
'worksheet1.Cells(7, 11) = rs.Properties.Count
worksheet1.Cells(j, 1) = rs.Fields(0).Value
worksheet1.Cells(j, 3) = rs.Fields(2).Value
worksheet1.Cells(j, 4) = rs.Fields(3).Value
worksheet1.Cells(j, 7) = rs.Fields(6).Value
j = j + 1
rs.MoveNext
Loop
rs.Close
End Sub
我 运行 SQL 通过 excel-vba& ADO 在 excel 中查询。 我使用循环解析结果 然后,我发现我必须知道在解析结果之前会生成多少条记录sql。
实际上,在生成查询之前,我并不知道查询结果记录的数量。
任何函数或方法可以让我知道,以便我可以放入循环语句?
(我了解 .Fields() .Properties() ,但不起作用)
Sub sbADO()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
'You can provide the full path of your external file as shown below
'DBPath ="C:\InputData.xlsx"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
sSQLSting = select ....' it is too long so i choose to skip
Set rs = Conn.Execute(sSQLSting)
Do While Not rs.EOF
'Officer = rs.Fields(i).Value
For j = 5 To 29 ' THIS IS THE MAIN CODE I NEED TO IMPROVE
'worksheet1.Cells(7, 11) = rs.Properties.Count
worksheet1.Cells(j, 1) = rs.Fields(0).Value
worksheet1.Cells(j, 3) = rs.Fields(2).Value
worksheet1.Cells(j, 4) = rs.Fields(3).Value
worksheet1.Cells(j, 7) = rs.Fields(6).Value
' Insert data to your worksheet here
rs.MoveNext
Next j
Loop
rs.Close
End Sub
For j = 5 To 29 '
next j
这是我应该设置的范围,影响工作表解析的范围。本例查询记录结果一共有24条。如果j设置太大(比如30),会提示3021 error -BOF & EOF 应该是真的。因此,范围应该符合记录的数量。
j 不需要循环。只需在所有单元分配之后和 do 循环结束之前递增 j 即可。
Sub sbADO()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
'You can provide the full path of your external file as shown below
'DBPath ="C:\InputData.xlsx"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
sSQLSting = select .... ' it is too long so i choose to skip
Set rs = Conn.Execute(sSQLSting)
j = 5
Do While Not rs.EOF
'worksheet1.Cells(7, 11) = rs.Properties.Count
worksheet1.Cells(j, 1) = rs.Fields(0).Value
worksheet1.Cells(j, 3) = rs.Fields(2).Value
worksheet1.Cells(j, 4) = rs.Fields(3).Value
worksheet1.Cells(j, 7) = rs.Fields(6).Value
j = j + 1
rs.MoveNext
Loop
rs.Close
End Sub