|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
最近在研究赵老师的这段代码,由于实际的需要对代码进行了修改,只更新和插入,不删除!代码如下:
- Sub 保存()
- Application.ScreenUpdating = False
- 'ActiveSheet.Unprotect Password:="vadsys" '撤消工作表保护
- Dim Target As String
- Dim i As Integer
- Call Init
-
-
-
- '生成更新字符串,如:a.姓名=b.姓名,a.性别=b.性别,……
- With Sheets(Split(act, "$")(0))
- arrFields = Range("A1:X1") '工作表中的字段名写入数组
- For i = 2 To UBound(arrFields, 2) '生成更新字符串
- StrTemp = StrTemp & ",a." & arrFields(1, i) & "=b." & arrFields(1, i)
- Next
- End With
-
- For i = 1 To Sheets.Count
- If Sheets(i).Name = Split(act, "$")(0) Then
- s = "[Excel 12.0;imex=0;Database=" & ThisWorkbook.FullName & "].[" & act & [a1].CurrentRegion.Address(0, 0) & "]"
- '生成更新SQL语句(请注意Office2007后需要加imex=0参数)
- sql = "update " & DataTable & " a," & s & " b set " & Mid(StrTemp, 2) & " where a.生产订单号=b.生产订单号 and a.产品型号=b.产品型号"
- cnn.Execute sql '不判断,更新可能存在的“SN”
-
- '将工作表信息追加到数据库表
- sql = "insert into " & DataTable & " select * from " & s & " where not exists"
- sql = sql & "(select * from " & DataTable & " a," & s & " b where a.生产订单号=b.生产订单号 and a.产品型号=b.产品型号)"
- cnn.Execute (sql)
-
- '关闭连接释放内存
- cnn.Close
- Set cnn = Nothing
- MsgBox "保存出货记录成功并上传数据库!", , "保存成功"
-
- End If
- Next
-
- 'ActiveSheet.Protect Password:="vadsys"
- ActiveWorkbook.RemovePersonalInformation = False
- Application.ScreenUpdating = True
- End Sub
-
复制代码
|
|