如何使用VBA显示总计行?

问题描述 投票:0回答:1

有没有一种方法可以使用VBA添加列的总和?我知道我可以使用'Application.CommandBars.ExecuteMso "RecordsTotals"添加“总计”行,但似乎找不到找到想要添加的列总和的方法。我尝试使用CurrentDb.QueryDefs("queryName").Fields("Quantity").Properties("AggregateType").Value = 0,但没有任何反应。我会很感激任何帮助的人。顺便说一下,这是我的代码。

Private Sub Command1_Click()
    Dim db As DAO.Database: Set db = CurrentDb
    Dim CustName As String
    Dim qDef As DAO.QueryDef

    If IsNull(Combo2.Value) Then
        MsgBox "Please Choose a customer"
    Else
        CustName = DLookup("[FullName]", "TblCustomer", _
                           "[CustID] = " & Combo2.Value)
        For Each qDef In db.QueryDefs
            If qDef.Name = "TblCustomer Query" Or qDef.Name = "TblLocationId Query" Then
            Else
                DoCmd.DeleteObject acQuery, qDef.Name
            End If
        Next

        Set qDef = db.CreateQueryDef(CustName)
        qDef.sQl = "SELECT OrderID, SaleDate, ProductID, UnitPrice, Quantity, Quantity * UnitPrice As TotalPrice FROM TblOrderDetails WHERE CustID = " & Combo2.Value & ""
        Application.RefreshDatabaseWindow

        DoCmd.OpenQuery CustName, acViewNormal
        DoCmd.SelectObject acQuery, CustName
        Application.CommandBars.ExecuteMso "RecordsTotals"
        CurrentDb.QueryDefs(CustName).Fields("Quantity").Properties("AggregateType").Value = 2
    End If
End Sub

enter image description here

ms-access access-vba
1个回答
0
投票
我通过在SQL中使用UNION解决了这个问题。感谢您的帮助!
© www.soinside.com 2019 - 2024. All rights reserved.