【发布时间】:2016-07-29 16:09:46
【问题描述】:
背景:
我需要打开一个 Access 文件并获取其中的信息,以便与我的 Excel 电子表格中的数据进行比较。
我正在使用 Microsoft Office 15.0 Access db 引擎对象库而不是 Microsoft DAO 对象库。
问题:
虽然我可以使用以下代码粘贴所有数据,但出于某种原因,它从“第 2 行”开始,忽略了标题。
Sub Sample()
Const PathToDB = "C:\...\AccessFile.accdb"
Const TitleSampleTable = "Sample Table"
Dim BDSample As Database
Dim SampleTable As Recordset
Dim SampleTableDef As TableDef
Dim CounterTitles As Long
Dim CounterRows As Long
Dim ColToPasteIn As Long
Dim RowToPasteIn As Long
Set BDSample = DBEngine.Workspaces(0).OpenDatabase(PathToDB)
Set SampleTable = BDSample.OpenRecordset(TitleSampleTable, dbOpenDynaset)
Set SampleTableDef = BDSample.TableDefs(TitleSampleTable)
For CounterTitles = 0 To SampleTableDef.RecordCount
RowToPasteIn = RowToPasteIn + 1
ColToPasteIn = 1
For CounterRows = 0 To SampleTable.Fields.Count
With Sheets(TitleSampleTable)
.Cells(RowToPasteIn, ColToPasteIn).Value = SampleTable.Fields(CounterRows) 'this is starts in the "body" of access, I can't figure a way to retrieve titles!
ColToPasteIn = ColToPasteIn + 1
End With
Next CounterRows
SampleTable.MoveNext
Next CounterTitles
Set BDSample = Nothing
Set SampleTable = Nothing
Set SampleTableDef = Nothing
End Sub
Accesss 中的示例数据
Excel 中的示例数据
问题:
如何获取标题值?
【问题讨论】: