使用宏更改Excel SQL命令

时间:2012-02-10 14:18:11

标签: sql excel excel-vba vba

我需要创建一个Excel 2010宏,它会更改Excel连接的SQL命令文本。我正在使用SQL Server,如果重要的话。

我怎么能这样做?我在网上找到了几个链接,但没有一个链接。

我使用的最新代码:

Dim comText As String

comText = "select  top 10000 [activitypointer0.subject] as subject , [activitypointer0.activityid] as activityid , [activitypointer0.instancetypecodename] as instancetypecodename , [activitypointer0.scheduledend] as scheduledend , [activitypointer0.createdbyname] as createdbyname , [activitypointer0.activitytypecodename] as activitytypecodename , [a_9cdbdceab5ee4a8db875050d455757bd.name] as [a_9cdbdceab5ee4a8db875050d455757bd.name]"
comText = comText & " from  FilteredActivityPointer as [activitypointer0] left outer join FilteredAccount as [a_9cdbdceab5ee4a8db875050d455757bd] on ([activitypointer0.regardingobjectid]  =  [a_9cdbdceab5ee4a8db875050d455757bd.accountid])"
comText = comText & " where  ([activitypointer0.activitytypecode] = 4201 and ([activitypointer0.ownerid] in (N'{69CB3CDE-0998-E011-916A-00155D000607}' , N'{D8D40800-B798-E011-916A-00155D000607}' , N'{7ECB3CDE-0998-E011-916A-00155D000607}' , N'{70CB3CDE-0998-E011-916A-00155D000607}')) and [activitypointer0.statecode] = 1 and  [activitypointer0.scheduledendutc] >= '20111231 22:00:00'  and  [activitypointer0.scheduledendutc] < '20120131 22:00:00' )"
comText = comText & " order by  [activitypointer0.subject] asc , [activitypointer0.activityid] asc"

With ActiveWorkbook.Connections("conCRM").ODBCConnection
    .commandText = comText
End With

ActiveWorkbook.Connections("conCRM").Refresh

这给我一个关于列名的错误。

Column name error

1 个答案:

答案 0 :(得分:2)

如果你得到的错误仅仅是“抱怨”有关列名的话,你可能应该使用[activitypointer0].[subject]而不是[activitypointer0.subject]等栏目名称(以及其他类似名称)。