背景
我需要打开一个Access文件并获取其中的信息,以便与Excel电子表格中的数据进行比较。
我使用的是Microsoft Office 15.0 Access数据库引擎对象库,而不是Microsoft DAO对象库。
的问题:
虽然我可以使用以下代码粘贴所有数据,但出于某种原因,它从"第2行和第34行开始。无视头衔。
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
访问中的示例数据
Excel中的示例数据
的的问题:
如何获得标题值?
答案 0 :(得分:1)
您可以获取字段名称并填充第1行
For i = 0 To SampleTable.Fields.Count - 1
Sheets(TitleSampleTable).Cells(1, i + 1) = SampleTable.Fields(i).Name
Next i
然后你可以像你原来那样从第2行开始填充日期