I have an sql query which I want to store in a vba variable and then get the number of rows of the output.
I have the following:
Dim PortInfo As Variant
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim lStr As String
Dim LastRow As Long
strServer = "DB-01"
ConnectionString = "Provider=SQLOLEDB;Data Source=" & strServer & ";" & _
"Integrated Security=SSPI;"
cnn.Open ConnectionString
lStr = "SELECT [PORTINFOID],[PORTNAME] FROM [a].[dbo].[portinfo]"
rst.Open lStr, cnn
If Not rst.EOF And Not rst.BOF Then
For i = 0 To rst.Fields.Count - 1
PortInfo = PortInfo & rst.Fields(i).name & vbTab
Next
Do Until rst.EOF
For i = 0 To rst.Fields.Count - 1
PortInfo = PortInfo & rst.Fields(i) & vbTab
Next
rst.MoveNext
Loop
End If
rst.Close
MsgBox PortInfo
LastRow = ???
With the above I get the following:
Firstly, I am not sure if the PortInfo variable should be Variant. And if it should be, how can I get the output to be in a column/row way so that I can get the number of rows of the resulting table.
