|
照下面改一下试试:
Private Sub CommandButton1_Click()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim rst2 As New ADODB.Recordset
Dim sq1 As String
Dim sqlM As String
Dim x As Long
x = [a65536].End(xlUp).Row
cnn.Open "provider=microsoft.jet.oledb.4.0;data source=" & ThisWorkbook.Path & "\db.mdb"
sq1 = "select * from 用户表"
sqlM = "select Max(用户编号) From 用户表"
rst.Open sq1, cnn, adOpenKeyset, adLockOptimistic
For i = 2 To x
rst2.Open sqlM, cnn, adOpenKeyset, adLockOptimistic
If Range("a" & i) > rst2.Fields(0) Then
rst.AddNew
rst.Fields("用户编号") = Range("a" & i)
rst.Fields("姓名") = Range("b" & i)
rst.Fields("年龄") = Range("c" & i)
rst.Fields("基本工资") = Range("d" & i)
rst.Fields("津贴") = Range("e" & i)
rst.Fields("工资合计") = Range("f" & i)
rst.Update
rst2.Close
Else
MsgBox "重复"
rst2.Close
End If
Next i
cnn.Close
Set cnn = Nothing
MsgBox "保存成功"
End Sub |
|