【问题标题】:Access VBA To Add SUM() To Query String访问 VBA 以将 SUM() 添加到查询字符串
【发布时间】:2017-03-21 16:36:41
【问题描述】:

我有一个多选列表框,本质上是构建一个查询。语法按其应有的方式工作,但不是为每个sale 逐行输入,我如何检查数组是否包含字段sale 以及它是否将文本更改为SUM(Sales) As SaleAmt

这是我当前的语法:

    Dim I As Long 
Dim X As Long 
Dim arrValues() 
If lstQueryBuild.ListIndex <> -1 Then 
    For I = 0 To lstQueryBuild.ListCount - 1 
        If lstQueryBuild.Selected(I) Then 
            Redim Preserve arrValues(X) 
            arrValues(X) = lstQueryBuild.List(I) 
            X = X + 1 
        End If 
    Next I 
End If 

CurrentDb.Exeucte "Select " & Join(arrValues, ",") & " FROM holdingtable"

这会产生

Debug.Print produces Select name, address, phone, company, sales from holdingtable

【问题讨论】:

    标签: arrays vba ms-access listbox ms-access-2013


    【解决方案1】:

    您可以尝试用遍历数组的循环替换您的 Join() 函数。

    dim sSQL$
    
    sSQL="Select "
    
    For x=lbound(arrValues) to ubound(arrvalues)
    if arrValues(x)="sales" then 
         sSQL=sSQL & "sum(Sales),"
    else
         sSQL=sSQL & arrValues(x) & ","
    end if
    next x
    
    ssQL=left(sSQL,len(ssql)-1) & " from holdingtable"
    CurrentDb.Execute sSQL
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多