简体   繁体   中英

SQL Recordset close inside loop

I have a question regarding opening and closing an recordset inside my loop. The code that I now have works fine, except for the fact that I know it can be coded in an different, much faster or nicer way. I am not that advanced with SQL, therefore my question.

due to the fact I have to close my recordset everytime the code is a bit slow.

 Do Until Cells(c, 2).Value = Empty
        TeamMember = Cells(c, 2).Value

        r = 4
        For i = 4 To 34
            Days = Cells(r, i).Value

             strSQL = "SELECT [" & TeamMember & "] From [Planning$] Where [Days]=" & "#" & Format(Days, "mm/dd/yyyy") & "#" & ""
             rs.Open strSQL, DBConnection, adOpenDynamic, adLockOptimistic
             Cells(c, i).Offset(0, 0).CopyFromRecordset rs
             rs.Close
            Next i
        c = c + 1
    Loop

Look at using something like this

"Where [Days]>=#" & _
                CDate(Application.WorksheetFunction.Min(Range("a4:a34"))) & _
                "# AND [Days]<=#" & _
                CDate(Application.WorksheetFunction.Max(Range("a4:a34"))) & "#"

Then use the recordset .find method to look for your date, then just use .moveFirst each loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM