使用 Excel VBA 从 Excel 导出到 Access 2007
Export from Excel to Access 2007 using Excel VBA
我需要导出工作表的内容以附加到访问数据库 table(均为 2007 年)并且我正在尝试从 Excel 中的模块中 运行电子表格。 table 有一个主键,它是一个自动编号,我已经尝试 运行 在下面的模块中使用和不使用电子表格中的第一列来匹配电子表格列与 table 列.此外,table 中的最后一个字段是一个复选框 Y\N,我已将电子表格中的最后一列设置为 TRUE 和 FALSE 值。但是当我 运行 模块时,我得到了 "Finished" 消息框,但是 table 没有更新。电子表格和数据库都位于 C: 的位置。我做错了什么?
Private Sub AddData()
Dim strMyPath As String, strDBName As String, strDB As String, strSQL
As String
Dim i As Long, n As Long, lastRow As Long, lFieldCount As Long
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
strDBName = "CMDB.mdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
"Microsoft.ACE.OLEDB.12.0". The ACE Provider can be used for both the
Access .mdb & .accdb files.
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0;
data source=" & strDB
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(18)
Set adoRecSet = New ADODB.Recordset
strTable = "Asset_Table"
adoRecSet.Open Source:=strTable, ActiveConnection:=connDB,
CursorType:=adOpenStatic, LockType:=adLockOptimistic
lFieldCount = adoRecSet.Fields.Count
'determine last data row in the worksheet:
lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
adoRecSet.AddNew
For n = 0 To lFieldCount - 1
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1)
Next n
adoRecSet.Update
Next i
adoRecSet.Close
connDB.Close
Set adoRecSet = Nothing
Set connDB = Nothing
MsgBox "Finished"
End Sub
编辑:下面 Evans 和 ChipsLetten 的建议很有帮助,我已经解决了这个问题。我更改了行数计算并添加了一个 If 来处理自动编号,因为 Chips 建议如下。
Dim b As Long
b = ws.UsedRange.Rows.Count
For i = 2 To b - 1
adoRecSet.AddNew
For n = 0 To lFieldCount - 1
If Not adoRecSet.Fields(n).Properties("ISAUTOINCREMENT") Then
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1).Value
End If
Next n
adoRecSet.Update
Next i
您可以测试该字段以查看它是否为自动递增字段,然后再尝试向其写入值。试试下面对我有用的代码(Excel 2007 但 Access 2010)
For i = 2 To lastRow
adoRecSet.AddNew
For n = 0 To lFieldCount - 1
If Not adoRecSet.Fields(n).Properties("ISAUTOINCREMENT") Then
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1).Value
End If
Next n
adoRecSet.Update
Next i
在 Excel sheet 的最后一列使用 TRUE/FALSE 对我来说效果很好。
我需要导出工作表的内容以附加到访问数据库 table(均为 2007 年)并且我正在尝试从 Excel 中的模块中 运行电子表格。 table 有一个主键,它是一个自动编号,我已经尝试 运行 在下面的模块中使用和不使用电子表格中的第一列来匹配电子表格列与 table 列.此外,table 中的最后一个字段是一个复选框 Y\N,我已将电子表格中的最后一列设置为 TRUE 和 FALSE 值。但是当我 运行 模块时,我得到了 "Finished" 消息框,但是 table 没有更新。电子表格和数据库都位于 C: 的位置。我做错了什么?
Private Sub AddData()
Dim strMyPath As String, strDBName As String, strDB As String, strSQL
As String
Dim i As Long, n As Long, lastRow As Long, lFieldCount As Long
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
strDBName = "CMDB.mdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
"Microsoft.ACE.OLEDB.12.0". The ACE Provider can be used for both the
Access .mdb & .accdb files.
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0;
data source=" & strDB
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(18)
Set adoRecSet = New ADODB.Recordset
strTable = "Asset_Table"
adoRecSet.Open Source:=strTable, ActiveConnection:=connDB,
CursorType:=adOpenStatic, LockType:=adLockOptimistic
lFieldCount = adoRecSet.Fields.Count
'determine last data row in the worksheet:
lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
adoRecSet.AddNew
For n = 0 To lFieldCount - 1
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1)
Next n
adoRecSet.Update
Next i
adoRecSet.Close
connDB.Close
Set adoRecSet = Nothing
Set connDB = Nothing
MsgBox "Finished"
End Sub
编辑:下面 Evans 和 ChipsLetten 的建议很有帮助,我已经解决了这个问题。我更改了行数计算并添加了一个 If 来处理自动编号,因为 Chips 建议如下。
Dim b As Long
b = ws.UsedRange.Rows.Count
For i = 2 To b - 1
adoRecSet.AddNew
For n = 0 To lFieldCount - 1
If Not adoRecSet.Fields(n).Properties("ISAUTOINCREMENT") Then
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1).Value
End If
Next n
adoRecSet.Update
Next i
您可以测试该字段以查看它是否为自动递增字段,然后再尝试向其写入值。试试下面对我有用的代码(Excel 2007 但 Access 2010)
For i = 2 To lastRow
adoRecSet.AddNew
For n = 0 To lFieldCount - 1
If Not adoRecSet.Fields(n).Properties("ISAUTOINCREMENT") Then
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1).Value
End If
Next n
adoRecSet.Update
Next i
在 Excel sheet 的最后一列使用 TRUE/FALSE 对我来说效果很好。