使用VBA将.MDB文件中的表中的数据读入Excel 2013

时间:2016-02-08 06:18:26

标签: vba excel-vba ms-access ms-access-2003 excel

我正在尝试将Access .MDB文件中的一些数据导入Excel 2013电子表格。

到目前为止,我已经尝试将数据库转换为2007格式但是没有用(到目前为止我尝试了2种方法)

是否有更简单的方法将数据直接从.MDB文件中的表导入Excel 2013电子表格中的工作表? (最终结果)

Sub convertaccessdb()
Application.ConvertAccessProject _
 SourceFilename:="C:\new\pabxcalls.mdb", _
 DestinationFilename:="C:\My Documents\pabxcalls2007.accdb", _
 DestinationFileFormat:=acFileFormatAccess2007
End Sub
' **** THIS GIVES AN RUNTIME ERROR 438 - OBJECT DOESN'T SUPPORT THIS PROPERTY OR METHOD ***

Sub Command7_Click()
Dim app As New Access.Application
app.Visible = True
app.AutomationSecurity = msoAutomationSecurityLow
app.SysCmd 603, "C:\New\pabxcalls.mdb", "C:\new\pabxcalls_new.mdb"
Set app = Nothing
End Sub
' *** THIS RUNS, BUT DOES NOT OUTPUT THE FILE REQUIRED ***

谢谢你们!

MDB Description

1 个答案:

答案 0 :(得分:1)

发现我实际上并没有转换数据库。只需使用查询和ADODB连接提取数据,如下所示。

非常感谢!

Sub GetCallData()

Dim objAdoCon As Object
Dim objRcdSet As Object
Dim DailyOutgoing, DailyIncoming, MonthlyOutgoing, MonthlyIncoming As String

DailyOutgoing = "SELECT calls.Extension,Sum(calls.Duration) FROM calls WHERE (((calls.Calldate)=Date())) AND (((calls.calltype)=""O"")) GROUP BY calls.Extension, calls.Calldate ;"
DailyIncoming = "SELECT calls.Extension,Sum(calls.Duration) FROM calls WHERE (((calls.Calldate)=Date())) AND (((calls.calltype)=""I"")) GROUP BY calls.Extension, calls.Calldate ;"

Set objAdoCon = CreateObject("ADODB.Connection")
Set objRcdSet = CreateObject("ADODB.Recordset")

 objAdoCon.Open "Provider = Microsoft.Jet.oledb.4.0;Data Source = \\remotehost\PABXSoft\Call Collector\Data\pabxcalls.mdb"

 ' *** GET DAILY OUTGOING ***

 ThisWorkbook.Worksheets("CALL_DATA").Range("A3:B24").Value = ""
 objRcdSet.Open DailyOutgoing, objAdoCon
 ThisWorkbook.Worksheets("CALL_DATA").Range("A3").CopyFromRecordset objRcdSet
Set objRcdSet = Nothing

 ' *** GET DAILY INCOMING ***

Set objRcdSet = CreateObject("ADODB.Recordset")
 ThisWorkbook.Worksheets("CALL_DATA").Range("A27:B46").Value = ""
 objRcdSet.Open DailyIncoming, objAdoCon
 ThisWorkbook.Worksheets("CALL_DATA").Range("A27").CopyFromRecordset objRcdSet

Set objAdoCon = Nothing
Set objRcdSet = Nothing

End Sub