当前位置: 代码迷 >> VBA >> excel VBA有关循环执行sql语句的有关问题
  详细解决方案

excel VBA有关循环执行sql语句的有关问题

热度:2155   发布时间:2013-02-26 00:00:00.0
excel VBA有关循环执行sql语句的问题
j = 6
Do Until j = 1000000
 str2 = "delete from dispose1121"
 str = "insert dispose1121 Exec pt_AppTempGetFiliationDispose @apptid='" & Sheets("Sheet1").Cells(j, 16) & " ',@version='" & Sheets("Sheet1").Cells(j, 17) & " ',@currentappdid='" & Sheets("Sheet1").Cells(j, 18) & " '"
 str1 = "select count(*) as counts from dispose1121"

 rs.Open str2, strcon
 rs.Open str, strcon
 rs.Open str1, strcon
 Sheets("Sheet1").Cells(j, 7) = rs("counts")
j = j + 1

Loop

如上一段VBA代码,j的初始值为6,在J=1000000之前循环执行str2,str,str1三条sql语句,当j=6时执行完3条语句,将counts值显示在第6行的第7列,然后开始循环,让J+1等于7,继续循环执行sql三条语句,就出错了。
求救各位大侠,问题出在哪呢?

------解决方案--------------------------------------------------------
每次循环完成后,关闭RS试一下吧
j = 6
Do Until j = 1000000
 str2 = "delete from dispose1121"
 str = "insert dispose1121 Exec pt_AppTempGetFiliationDispose @apptid='" & Sheets("Sheet1").Cells(j, 16) & " ',@version='" & Sheets("Sheet1").Cells(j, 17) & " ',@currentappdid='" & Sheets("Sheet1").Cells(j, 18) & " '"
 str1 = "select count(*) as counts from dispose1121"

 rs.Open str2, strcon
 rs.Open str, strcon
 rs.Open str1, strcon
 Sheets("Sheet1").Cells(j, 7) = rs("counts")
j = j + 1
rs.close
Loop
  相关解决方案