vba excel中的MS Access查询

时间:2017-05-03 08:19:48

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

Set objAccess = CreateObject("Access.Application")


objAccess.OpenCurrentDatabase "C:\Users\" & sUser & "\Downloads\ICO\" & DB & ".mdb"

objAccess.DoCmd.RunSQL "SELECT [Total-7yr_27Mar2017].TYPE AS Type, [Total-7yr_27Mar2017].[HOLDING CO] AS [HOLDING CO], [Total-7yr_27Mar2017].CODE AS CODE, [Total-7yr_27Mar2017].ST AS ST, [Total-7yr_27Mar2017].STLVL AS STLVL, [Total-7yr_27Mar2017].STCMP AS STCMP, [Total-7yr_27Mar2017].JUR AS JUR, Sum([Total-7yr_27Mar2017].[OR LS MOU]) AS OR_LS_MOU, Sum([Total-7yr_27Mar2017].[UP LS MOU]) AS UP_LS_MOU, Sum([Total-7yr_27Mar2017].[TOT USG EXP]) AS UP_USG_EXP INTO 7yr_Total_Varients
 FROM [Total-7yr_27Mar2017]
 GROUP BY [Total-7yr_27Mar2017].TYPE, [Total-7yr_27Mar2017].[HOLDING CO], [Total-7yr_27Mar2017].CODE, [Total-7yr_27Mar2017].ST, [Total-7yr_27Mar2017].STLVL, [Total-7yr_27Mar2017].STCMP, [Total-7yr_27Mar2017].JUR"

Docmd.RunSQL有时正在运行并且因为Docmd.RunSQL对于选择查询不可靠而给出错误。所以我尝试使用Database.execute和adodb.connection adobd.recordset.it抛出7yr_Total_Varients已经存在的错误。

1 个答案:

答案 0 :(得分:1)

修改SQL以将查询从创建表查询更改为追加查询