|
- Sub 从Excel工作表中向数据表添加纪录()
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim myTable As String
- Dim d As String
- Dim iCount As Long
- iCount = Cells(Rows.Count, 1).End(xlUp).Row
- d = Format([f1].Value, "yyyy-mm-dd")
- myTable = "工资表"
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & "\db1.mdb"
-
- Sql = "select 日期 from " & myTable & " where 日期='" & d & "'"
- rs.Open Sql, cnn, adOpenKeyset, adLockOptimistic
-
- If rs.RecordCount = 0 Then
- MsgBox "没有发现存在当前年月的工资记录,确定插入工资数据", vbInformation, "提示"
- Else
- Sql = "delete * from " & myTable & " where 日期='" & d & "'"
- cnn.Execute Sql
- MsgBox rs.RecordCount & "条记录被删除。", vbInformation, "提示"
- End If
- Sql = "INSERT INTO " & myTable & " SELECT '" & d & "' as 日期,* FROM " & _
- "[Excel 8.0;Database=" & ThisWorkbook.FullName & ";].[" & ActiveSheet.Name & "$a2:k" & iCount & "]"
- cnn.Execute Sql
- MsgBox "一共插入" & iCount - 2 & "条记录。", vbInformation, "提示"
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|