|
Public Sub 实例()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Long
Dim mydata As String, myTable As String, SQL As String
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("sheet1")
mydata = "工资管理"
myTable = "基本信息"
Set cnn = New ADODB.Connection
With cnn
.ConnectionString = "Provider=SQLOLEDB.1;" _
& "User ID=sa;" _
& "Data Source=THTFCOMPUTER;" _
& "Initial Catalog=" & mydata
.Open
End With
n = ws.Range("A65536").End(xlUp).Row
For i = 2 To n
SQL = "select * from " & myTable _
& " where 职工编号='" & Trim(ws.Cells(i, 1).Value) & "'" _
& " and 姓名='" & Trim(ws.Cells(i, 2).Value) & "'" _
& " and 性别='" & Trim(ws.Cells(i, 3).Value) & "'" _
& " and 所属部门='" & Trim(ws.Cells(i, 4).Value) & "'" _
& " and 工资总额=" & Trim(ws.Cells(i, 5).Value) _
& " and 备注='" & Trim(ws.Cells(i, 6).Value) & "'"
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
If rs.EOF And rs.BOF Then
rs.AddNew
For j = 1 To rs.Fields.Count
rs.Fields(j - 1) = Trim(ws.Cells(i, j).Value)
Next j
rs.Update
Else
For j = 1 To rs.Fields.Count
rs.Fields(j - 1) = Trim(ws.Cells(i, j).Value)
Next j
rs.Update
End If
Next i
MsgBox "数据保存完毕!", vbInformation, "保存数据"
rs.Close
cnn.Close
Set wb = Nothing
Set ws = Nothing
Set rs = Nothing
Set cnn = Nothing
End Sub
|
|