|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 bmin1117 于 2013-10-8 23:18 编辑
- Public Sub UpdateToSql()
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim SQL As String, mydata As String, mytable As String
- mydata = "123" '指定要修改的数据库
- mytable = "321" '指定数据表
-
- '建立与指定SQL Server数据库的连接
- cnn.ConnectionString = "Provider=SQLOLEDB;" _
- & "User ID=111;" _
- & "Password =222;" _
- & "Data Source=333;" _
- & "Initial Catalog =" & mydata
- cnn.Open
- '按顺序更新全表某些字段的记录
- For i = 9 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
- SQL = " update " & mytable _
- & " set 销货清单日期='" & Sheet1.Cells(i, 2).Value & "',开发公司='" & Sheet1.Cells(i, 3).Value & "',结算性质='" & Sheet1.Cells(i, 4).Value & "',项目部='" & Sheet1.Cells(i, 5).Value & "',施工单位='" & Sheet1.Cells(i, 6).Value & "',工程名称='" & Sheet1.Cells(i, 7).Value & "',栋号='" & Sheet1.Cells(i, 8).Value & "'," _
- & "送货地点='" & Sheet1.Cells(i, 9).Value & "',商品名称='" & Sheet1.Cells(i, 10).Value & "',材质='" & Sheet1.Cells(i, 11).Value & "',规格型号='" & Sheet1.Cells(i, 12).Value & "',长度='" & Sheet1.Cells(i, 13).Value & "',钢厂='" & Sheet1.Cells(i, 14).Value & "',理论重量='" & Sheet1.Cells(i, 15).Value & "',刨根支数='" & Sheet1.Cells(i, 16).Value & "'," _
- & "检尺重量='" & Sheet1.Cells(i, 17).Value & "',采购渠道='" & Sheet1.Cells(i, 18).Value & "',销售运费单价='" & Sheet1.Cells(i, 19).Value & "',钢筋销售单价='" & Sheet1.Cells(i, 20).Value & "',出库时间='" & Sheet1.Cells(i, 21).Value & "'," _
- & "出库重量='" & Sheet1.Cells(i, 22).Value & "',钢筋采购单价_市场价='" & Sheet1.Cells(i, 23).Value & "',一次运费成本='" & Sheet1.Cells(i, 25).Value & "',二次运费成本='" & Sheet1.Cells(i, 26).Value & "',加价起始日='" & Sheet1.Cells(i, 28).Value & "',加价终止日='" & Sheet1.Cells(i, 29).Value & "',已收货款='" & Sheet1.Cells(i, 30).Value & "',未收加价='" & Sheet1.Cells(i, 31).Value & "',已收加价='" & Sheet1.Cells(i, 32).Value & "' where 销货单号='" & Sheet1.Cells(i, 1).Value & "'"
- Set rs = cnn.Execute(SQL)
-
- SQL = " update " & mytable _
- & "销售运费金额=检尺重量 * 销售运费单价,钢筋销售金额=检尺重量 * 钢筋销售单价,差价=钢筋销售单价 - 钢筋采购单价_市场价," _
- & "减掉吨数=理论重量 - 检尺重量,胀库数=检尺重量 - 出库重量,加权含税=财务加权_不含税 * 1.17 where 销货单号='" & Sheet1.Cells(i, 1).Value & "'"
- Set rs = cnn.Execute(SQL)
-
- SQL = " update " & mytable _
- & "运费成本总额_挂牌=二次运费成本 * 检尺重量,运费成本总额_加权=一次运费成本 * 出库重量 + 二次运费成本 * 检尺重量,钢筋成本金额_市场价=出库重量 * 钢筋采购单价_市场价,钢筋成本金额_加权=出库重量 * 加权含税 where 销货单号='" & Sheet1.Cells(i, 1).Value & "'"
- Set rs = cnn.Execute(SQL)
-
- SQL = " update " & mytable _
- & "销售合价=销售运费金额 + 钢筋销售金额,胀库率=胀库数 / 检尺重量 where 销货单号='" & Sheet1.Cells(i, 1).Value & "'"
- Set rs = cnn.Execute(SQL)
-
- SQL = " update " & mytable _
- & "总成本_加权=运费成本总额_加权 + 钢筋成本金额_加权,总成本_挂牌价=运费成本总额_挂牌 + 钢筋成本金额_市场价 where 销货单号='" & Sheet1.Cells(i, 1).Value & "'"
- Set rs = cnn.Execute(SQL)
-
- SQL = " update " & mytable _
- & "毛利_挂牌价=销售合价 - 总成本_挂牌价,毛利_加权=销售合价 - 总成本_加权 where 销货单号='" & Sheet1.Cells(i, 1).Value & "'"
- Set rs = cnn.Execute(SQL)
- Next i
- MsgBox "数据保存完毕!", vbInformation, "保存数据"
- '关机记录集以及与数据库的连接
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码
报错在第二个"Set rs = cnn.Execute(SQL)"这里..
个人理解应该是关闭记录集的问题吧..
请老师们帮忙改一改..
ps..
之所以要分6次update..是因为这些数据有前后的逻辑运算关系..
写在一起的话要update 4次才能把数据更新完整..
所以我就只能想到分次更新..不知是否正确..
|
|