|
zhaogang1960 发表于 2015-1-12 17:52
老师您好!
Sub 写入access数据()
Dim cnn As New ADODB.Connection
Dim myPath As String
Dim s As String
Dim t As String
Dim SQL As String
Dim arr As Variant
Dim i As Integer
Dim sh As Worksheet
myPath = ThisWorkbook.Path & "\jiageguanli.mdb"
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myPath
For Each sh In Worksheets
arr = sh.Range("A1").CurrentRegion
If UBound(arr) > 1 Then
s = ""
t = ""
For i = 2 To UBound(arr, 2)
s = s & ",A." & arr(1, i) & "=B." & arr(1, i)
t = t & ",A." & arr(1, i)
Next
SQL = "UPDATE " & sh.Name & " A,[Excel 8.0;Database=" & ThisWorkbook.FullName & ";].[" & sh.Name & "$" _
& sh.Range("A1").CurrentRegion.Address(0, 0) & "] B SET " & Mid(s, 2) & " WHERE A.zdbh=B.zdbh"
cnn.Execute SQL '更新已存在的数据
SQL = "INSERT INTO " & sh.Name & " SELECT " & Mid(t, 2) & " FROM [Excel 8.0;Database=" & ThisWorkbook.FullName & ";].[" & sh.Name & "$" & sh.Range("A1").CurrentRegion.Address(0, 0) _
& "] A LEFT JOIN " & sh.Name & " B ON A.zdbh=B.zdbh WHERE B.zdbh IS NULL"
cnn.Execute SQL '插入不存在的数据
End If
Next
MsgBox "数据修改、写入成功。", vbInformation
cnn.Close
Set cnn = Nothing
End Sub
这个也老师写的代码
请问在写入数据到access数据库时要注意什么
为什么我的不可以写入 |
|