Expr1000 error when performing a SUM on a SQL quer

2019-07-24 07:18发布

The following query is providing the result in excel: "Expr1000 RESULT" I need to get the result without a header and nothing I've found will accomplish this. I can't find any redundancies in the query, but when I remove the SUM function, the header reverts back to the column's correct header. I very much appreciate any ideas.

Sub CalculateComplete()
Sheet2.Cells.ClearContents

Dim oCn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String

Dim QueryDPiN As String
QueryDPiN = "SELECT IIF(SUM([Amount Due]) IS NULL, 0, SUM([Amount Due])) FROM [OP$] WHERE [Originator Dept]='BLC New Business' AND [Overpayment Category]='Dollars Paid in Error';"

Call SQLQueries(QueryDPiN, "C2")

End Sub

Sub SQLQueries(QueryVariable As String, TableLocation As String)

Dim oCn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String
Dim qt As QueryTable

ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\cnop0g\Desktop\OP.xlsx;Extended Properties=Excel 8.0;Persist Security Info=False"
Set oCn = New ADODB.Connection
oCn.ConnectionString = ConnString
oCn.ConnectionTimeout = 30
oCn.Open

SQL = QueryVariable

Set oRS = New ADODB.Recordset
oRS.Source = SQL
oRS.ActiveConnection = oCn
oRS.Open

Set qt = Worksheets(6).QueryTables.Add(Connection:=oRS, _
Destination:=Worksheets(6).Range(TableLocation))

    qt.Refresh

If oRS.State <> adStateClosed Then
oRS.Close
End If

If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCn Is Nothing Then Set oCn = Nothing

End Sub

Thank you for your help! - Zokah

1条回答
不美不萌又怎样
2楼-- · 2019-07-24 08:05

You need to specify column name AS [Amount Due] in your query so that SQL engine would not generate one automatically:

QueryDPiN = "SELECT IIF(SUM([Amount Due]) IS NULL, 0, SUM([Amount Due])) AS [Amount Due] FROM [OP$] WHERE [Originator Dept]='BLC New Business' AND [Overpayment Category]='Dollars Paid in Error';"
查看更多
登录 后发表回答