VBA中执行SQL存储过程的问题。
我在Excel中用如下VBA代码将当前EXCEL行记录插入到SQL 表。我想在VBA中加一段代码当所有Excel记录插入完成后再执行一个SQL的存储过程。请教如何实现?谢谢!
VBA代码下:
cn.Open strCn
'清空表记录
cn.execute "truncate table table1"
'循环增加记录
For row = Me.TextBox1.Value To Me.TextBox2.Value
strSQL = "insert into table1(供应商代码,物料代码,单位,不含税单价,供应商名称,物料名称) values ( '" & Sheet3.Cells(row, 1) & "','" & Sheet3.Cells(row, 2) & "','" & Sheet3.Cells(row, 3) & "','" & Sheet3.Cells(row, 4) & "','" & Sheet3.Cells(row, 5) & "','" & Sheet3.Cells(row, 6) & "')"
cn.execute strSQL
Next row
cn.Close
[解决办法]
cn.Open strCn'清空表记录cn.execute "truncate table table1"'循环增加记录For row = Me.TextBox1.Value To Me.TextBox2.Value strSQL = "insert into table1(供应商代码,物料代码,单位,不含税单价,供应商名称,物料名称) values ( '" & Sheet3.Cells(row, 1) & "','" & Sheet3.Cells(row, 2) & "','" & Sheet3.Cells(row, 3) & "','" & Sheet3.Cells(row, 4) & "','" & Sheet3.Cells(row, 5) & "','" & Sheet3.Cells(row, 6) & "')"cn.execute strSQLNext rowcn.execute " exec 存储过程名 "cn.Close