|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 z315559822 于 2013-4-12 09:23 编辑
[code=sql]
'需要引用ado2.5以上
Sub 往sql里添加新编码() '定义过程名称
Dim i As Integer, j As Integer, sht As Worksheet 'i,j为整数变量;sht 为excel工作表对象变量,指向某一工作表
Dim cn As New ADODB.Connection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
Dim rs As New ADODB.Recordset '定义记录集对象,保存数据表
Dim strCn As String, strSQL As String '字符串变量
'If Environ("userNAME") & " / " & Environ("COMPUTERNAME") = "zxc / A1981" Then
If Application.UserName = "Administrator/MPC01" Then
strCn = "Provider=sqloledb;Server=XYINGSC01;Database=ld;Uid=1981;Pwd=852;" '定义数据库链接字符串
Else
msgbox "只有管理员才可以操作"
Exit Sub
End If
strSQL = "select * from cpbm" '定义SQL查询命令字符串
cn.ConnectionTimeout = 30
cn.CursorLocation = adUseClient
cn.Open strCn '与数据库建立连接,如果成功,返回连接对象cn
rs.Open strSQL, cn, adOpenStatic, adLockBatchOptimistic, adCmdText '执行strSQL所含的SQL命令,结果保存在rs记录集对象中
Set sht = ActiveWorkbook.Worksheets("添加编码")
With rs
For i = 2 To sht.[a65536].End(xlUp).Row '循环开始,i从2到4
.MoveFirst
.Find "产品编码='" & sht.Cells(i, 4) & "'"
If Not rs.EOF Then
For j = 1 To 37
.Fields(j - 1).Value = sht.Cells(i, j).Value
Next j
Else
.AddNew
For j = 1 To 37
.Fields(j - 1).Value = sht.Cells(i, j).Value
Next j
End If
Application.StatusBar = "请稍等.........正在处理第 " & i - 1 & "条编码"
If Range("V" & i & "").Value = "T" Or Range("V" & i & "").Value = "T2" Then
RunYesNo = msgbox("这个台子是否是拉台?", vbYesNo, "提示")
If RunYesNo = vbYes Then Exit Sub
End If
Next i
.UpdateBatch
End With
rs.Close: cn.Close
Set rs = Nothing: Set cn = Nothing: Set sht = Nothing
msgbox "处理完毕!" + Chr(13) + "记得更新cpgzde!", vbInformation, "系统提示"
Application.StatusBar = False
End Sub
[/code]
|
|