|
[img]file:///C:\Users\lenovo\AppData\Roaming\Tencent\Users\389046961\QQ\WinTemp\RichOle\EH6[})[QVZ4_Y~2J6IWFU7K.png[/img] 文中的示例是把excel中的数据全部更新到数据库中,
我自己建立了一个数据库,是希望只存部分 列到数据库中,
如图只把A,C列存到数据库对应的,类别和产品名称列中
不太会实现,不知道应该怎么修改呢
下边的代码是在示例基础上修改的。运行起来说类型不匹配
注释部分是书中的原代码
Sub addValues()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim myPath As String
Dim myTable As String
Dim arrALL()
Dim arrFields()
Dim arrValues()
Dim i As Long
Dim r As Long
myPath = ThisWorkbook.Path & "\产品.mdb"
myTable = "产品"
On Error GoTo errmsg
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & myPath
rst.Open "select * from " & myTable & " where 1=2", cnn, adOpenDynamic, adLockOptimistic
' With ThisWorkbook.Path & "\products.csv".products
With Worksheets("products")
r = .Range("a1").End(xlDown).Row
' arrALL = .Range("a1:c" & r)
' arrFields = WorksheetFunction.Index(arrALL, 1, 0)
' For i = 3 To r
' arrValues = WorksheetFunction.Index(arrALL, i, 0)
' rst.AddNew arrFields, arrValues
' Next
arrALL = .Range("a1:c" & r)
arrFields = WorksheetFunction.Index(arrALL, 2, 3)
For i = 3 To r
arrValues = WorksheetFunction.Index(arrALL, i, 3)
rst.AddNew arrFields, arrValues
Next
End With
MsgBox "数据添加成功", , "添加数据"
'MsgBox WorksheetFunction.Index(arrALL, i, 3)
Exit Sub
errmsg:
MsgBox Err.Description, , "错误报告"
End Sub
|
|