访问 VBA 以将 SUM() 添加到查询字符串
Access VBA To Add SUM() To Query String
我有一个多 select 的列表框,基本上构建了一个查询。语法正常工作,但不是每个 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
您可以尝试用遍历数组的循环替换 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
我有一个多 select 的列表框,基本上构建了一个查询。语法正常工作,但不是每个 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
您可以尝试用遍历数组的循环替换 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