drom
Well-known Member
- Joined
- Mar 20, 2005
- Messages
- 528
- Office Version
- 2021
- 2019
- 2016
- 2013
- 2011
- 2010
- 2007
Hi and Thanks in advance!
I am connecting to Through VBA-SQL to a external database (Works OK on my Macro1_SqlConnection Macro)
I am Pasting the Data to a Range (works fine)
I am using the following macro, and Works PERFECT, (without checking the number os rows)
But How Can I check the Number of rows before pasting anything ??
Sometimes the Data source contains many Rows, so prior pasting the Data, I would like to know how many rows I am going to paste
So I am trying to Use:
How Can I get the Number of Rows ??
I am connecting to Through VBA-SQL to a external database (Works OK on my Macro1_SqlConnection Macro)
I am Pasting the Data to a Range (works fine)
I am using the following macro, and Works PERFECT, (without checking the number os rows)
VBA Code:
Sub Macro1_SqlConnection(wSql As String, wProvider As String, wDataSource As String, wInitialCatalog As String, wUserID As String, wPwd As String, rToPasteTheData As Range)
Dim objConn As New ADODB.connection
Dim objRs As New ADODB.Recordset
Dim objRsTemp As New ADODB.Recordset
Dim wConnection As String: wConnection = _
"Provider=" & wProvider & ";" & _
"Data Source=" & wDataSource & ";" & _
"Initial Catalog=" & wInitialCatalog & ";" & _
"User ID=" & wUserID & ";" & _
"Pwd=" & wPwd
objConn.Open wConnection
objRs.Open wSql, objConn
rToPasteTheData.CopyFromRecordset objRs
End Sub
But How Can I check the Number of rows before pasting anything ??
Sometimes the Data source contains many Rows, so prior pasting the Data, I would like to know how many rows I am going to paste
So I am trying to Use:
VBA Code:
Sub Macro2_SqlConnection(wSql As String, wProvider As String, wDataSource As String, wInitialCatalog As String, wUserID As String, wPwd As String, rToPasteTheData As Range)
Dim objConn As New ADODB.connection
Dim objRs As New ADODB.Recordset
Dim objRsTemp As New ADODB.Recordset
Dim wConnection As String: wConnection = _
"Provider=" & wProvider & ";" & _
"Data Source=" & wDataSource & ";" & _
"Initial Catalog=" & wInitialCatalog & ";" & _
"User ID=" & wUserID & ";" & _
"Pwd=" & wPwd
objConn.Open wConnection
objRs.Open wSql, objConn
Dim varArrayReader(): varArrayReader() = objRs.GetRows
Debug.Print UBound(varArrayReader, 2), UBound(varArrayReader, 1) ' I get 1590 5
'So I can create an array with the Data ....
'But when doing this:
If UBound(varArrayReader, 2) < 5000 Then rPastedPlace.CopyFromRecordset objRs
'The Error keeps 0 But I am not able to paste anything
'and I do not want to create an array with the data if not necessary
If objRs.State = adStateOpen Then objRs.Close
If objConn.State = adStateOpen Then objConn.Close
End Sub
How Can I get the Number of Rows ??