SQL query with loop

Hatye

Board Regular
Joined
Jan 11, 2010
Messages
143
Hi all :)

I am using this code to connect to SQL server to fetch some data:

Code:
Sub DataLoop()

Dim Conn As New ADODB.Connection
Dim recset As New ADODB.Recordset
Dim sqlQry As String, sConnect As String


FrDt = Range("B1").Value
ToDt = Range("B2").Value




sqlQry = "SELECT COUNT(VoNo) from dbo.SupTr where ValDt between " & FrDt & " and " & ToDt & " and VoTp=21"
sConnect = "Driver={SQL Server};Server=[server name]; Database=xxxx;Trusted_Connection=yes;"


Conn.Open sConnect


Set recset = New ADODB.Recordset


    recset.Open sqlQry, Conn
    Sheets("Ark1").Cells(7, 3).CopyFromRecordset recset
    recset.Close


Conn.Close


Set recset = Nothing


End Sub

This works great, but I would like to add it into a loop from row 7 and until the end of the list.
I would like to enter the database ('xxxx' above) from cell value A7 (and down the list) and insert the return value (recset) into C-column in the same row.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hey Hatye

Try the below code

Code:
Sub DataLoop()
Dim Conn As New ADODB.Connection
Dim recset As New ADODB.Recordset
Dim sqlQry As String, sConnect As String
FrDt = Range("B1").Value
ToDt = Range("B2").Value
For x = 7 To Range("A" & Rows.Count).End(xlUp).Row
    sqlQry = "SELECT COUNT(VoNo) from dbo.SupTr where ValDt between " & FrDt & " and " & ToDt & " and VoTp=21"
    sConnect = "Driver={SQL Server};Server=[server name]; Database=" & Cells(x, 1) & ";Trusted_Connection=yes;"
        Conn.Open sConnect
        Set recset = New ADODB.Recordset
            recset.Open sqlQry, Conn
            Sheets("Ark1").Cells(x, 3).CopyFromRecordset recset
            recset.Close
        Conn.Close
        Set recset = Nothing
Next x
End Sub
 
Upvote 0
Excellent, thank you so much :)

Hey Hatye

Try the below code

Code:
Sub DataLoop()
Dim Conn As New ADODB.Connection
Dim recset As New ADODB.Recordset
Dim sqlQry As String, sConnect As String
FrDt = Range("B1").Value
ToDt = Range("B2").Value
For x = 7 To Range("A" & Rows.Count).End(xlUp).Row
    sqlQry = "SELECT COUNT(VoNo) from dbo.SupTr where ValDt between " & FrDt & " and " & ToDt & " and VoTp=21"
    sConnect = "Driver={SQL Server};Server=[server name]; Database=" & Cells(x, 1) & ";Trusted_Connection=yes;"
        Conn.Open sConnect
        Set recset = New ADODB.Recordset
            recset.Open sqlQry, Conn
            Sheets("Ark1").Cells(x, 3).CopyFromRecordset recset
            recset.Close
        Conn.Close
        Set recset = Nothing
Next x
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top