简体   繁体   English

将 MS Access SQL 选择查询转换为 VBA。 使用聚合求和函数进行选择时中断

[英]Translating MS Access SQL select query to VBA. Breaks when select with aggregation sum function

Background背景

I'm trying to use Excel VBA to load data from Microsoft Office Access database.我正在尝试使用 Excel VBA 从 Microsoft Office Access 数据库加载数据。 The code was worked fine and I am now trying to add an extra column Position drawn from the datebasetable named EqBucket into the final result table代码运行良好,我现在尝试将从名为EqBucket的日期EqBucket表中提取的额外列Position添加到最终结果表中

The SQL works find in Access but it doesn't parse through to VBA. SQL 可以在 Access 中找到,但不会解析为 VBA。 The code break when I add in添加时代码中断

SUM(Eq_Buckets.Position) AS PositionOfSum

I'm guess it has to do with the aggregation sum wrapped around the column because this issue has never come up with other direct referenced columns.我猜这与围绕列的聚合总和有关,因为这个问题从未出现在其他直接引用的列中。

Appreciate for any pointers.感谢任何指针。 Thanks谢谢

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''

Info: 1. SQL string is stored in Sheets("SQL").Range("A1").value 2. Database tables Eq_SingleName_LBU , Eq_Buckets << this is where the position data are stored 3. Eq_Portfolio_Ref is just a reference table which could be ignored信息: 1. SQL 字符串存储在 Sheets("SQL").Range("A1").value 2. 数据库表Eq_SingleName_LBU , Eq_Buckets << 这是位置数据的存储位置 3. Eq_Portfolio_Ref只是一个参考表可以忽略

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' **IF I remove "Sum(Eq_Buckets.Position) AS PositionOfSum" the code works in VBA '''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''' **如果我删除“Sum(Eq_Buckets.Position) AS PositionOfSum”代码在VBA中工作

Here is the FULLY working SQL code in MS Access:这是 MS Access 中完全可用的 SQL 代码:

SELECT Eq_SingleName_LBU.Identifier AS Identifier, Eq_SingleName_LBU.Issuer AS Issuer, Eq_SingleName_LBU.MV_USD AS MV, Sum(Eq_Buckets.Position) AS PositionOfSum, Eq_SingleName_LBU.Issuer_Weight AS [Issuer Weight], Eq_SingleName_LBU.Test_Limit AS Limit, Eq_SingleName_LBU.Room_Limit AS [Remaining Limit], Eq_SingleName_LBU.Data_Date
FROM Eq_SingleName_LBU INNER JOIN (Eq_Buckets INNER JOIN Eq_Portfolio_Ref ON Eq_Buckets.Composite_Portfolio = Eq_Portfolio_Ref.BBG_Account_Codes) ON Eq_SingleName_LBU.Identifier = Eq_Buckets.BB_UniqueID
Where Eq_Buckets.Data_Date = (#03/12/2020#) and Eq_SingleName_LBU.UnderTest="Y"
GROUP BY Eq_SingleName_LBU.Identifier, Eq_SingleName_LBU.Issuer, Eq_SingleName_LBU.MV_USD, Eq_SingleName_LBU.Issuer_Weight, Eq_SingleName_LBU.Test_Limit, Eq_SingleName_LBU.Room_Limit, Eq_SingleName_LBU.Data_Date
HAVING (((Eq_SingleName_LBU.Data_Date) In (#03/12/2020#)))
ORDER BY Eq_SingleName_LBU.Data_Date;

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''

Here is the VBA code that the SQL string needs to fit through这是 SQL 字符串需要适应的 VBA 代码

Sub ADOImportFromAccessTable()

'On Error GoTo ErrorHandler
Application.ScreenUpdating = False
Application.Calculation = xlManual
Sheets("EQ1_SQL").Visible = True

    Dim con As Object
    Dim rst As Object
    Dim dbPath As String

    dbPath = "\\Db\Asset_db.accdb"
    Set con = CreateObject("ADODB.Connection")
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
    con.Open

    Set rst = CreateObject("ADODB.Recordset")

'This is where the SQL code will be referenced.
strSql = ThisWorkbook.Sheets("SQL").Range("A1").Value
Debug.Print strSql
strSql = Replace(strSql, "{date1}", Date_1)
Debug.Print strSql
strSql = Replace(strSql, "{date2}", Date_2)

rst.Open strSql, con, adOpenDynamic, adLockOptimistic

    rst.Close
    Set rst = Nothing
    con.Close
    Set con = Nothing

End sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''

Here is the error message I get from Excel VB editor这是我从 Excel VB 编辑器中得到的错误消息

Here is the error I get from VB editor.
Run-tme error '-2147467259 (80004005);: 
Method 'Open' of object' _ Recordset' failed

Try adding brackets around Position ie Sum(B.[Position]) ,尝试在Position周围添加括号,即Sum(B.[Position])

You can shorten the SQL by using table name aliases, for example您可以使用表名别名来缩短 SQL,例如

strSQL = " SELECT A.Identifier AS Identifier, A.Issuer AS Issuer, A.MV_USD AS MV," & _
             "   Sum(B.[Position]) AS PositionOfSum, " & _
             "   A.Issuer_Weight AS [Issuer Weight]," & _
             "   A.Test_Limit AS Limit, " & _
             "   A.Room_Limit AS [Remaining Limit]," & _
             "   A.Data_Date" & _
             " FROM Eq_SingleName_LBU AS A " & _
             " INNER JOIN Eq_Buckets AS B" & _
             " ON A.Identifier = B.BB_UniqueID" & _
             "   WHERE B.Data_Date = #2020/12/03# " & _
             "   AND A.UnderTest = 'Y' " & _
             " GROUP BY A.Identifier, A.Issuer," & _
             "   A.MV_USD, A.Issuer_Weight, A.Test_Limit," & _
             "   A.Room_Limit, A.Data_Date" & _
             " HAVING A.Data_Date IN (#2020/12/03#) " & _
             " ORDER BY A.Data_Date"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM