对于这个问题,可以写一个自定义函数如下:
Function DDSUM(tbname as string,fldname as string,fldIDname as string,StrID As String) as string
'示例:select 姓名 & " " & DDSUM("课程表","学科","姓名",[姓名]) from 课程表 group by 姓名
Dim rs As New ADODB.Recordset
Dim ssql As String
Dim i As Long
Dim s as string
ssql = "select " & fldname & " from " & tbname & " where Cstr(" & fldIDname & ")='" & strID & "'"
rs.Open ssql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
for i=1 to rs.recordcount
DDSUM=DDSUM & rs.Fields(fldname).value & " "
rs.movenext
next
DDSUM=left(DDSUM,len(DDSUM)-1)
rs.close
set rs=nothing
end function
-------------------------------
其后,版友liqianwu同志又问了一个从工序合格率,计算产品合格率的问题,也就是分类连乘的问题。对于这个问题,可以将上面的函数稍作修改,写成如下:
Function Multiply(tbname As String, fldname As String, fldIDname As String, StrID As String) As String
'示例:select 产品,Multiply("生产表","合格率","产品",[产品]) as 产品合格率 from 生产表 group by 产品
Dim rs As New ADODB.Recordset
Dim ssql As String
Dim i As Long
Dim s As String
ssql = "select " & fldname & " from " & tbname & " where Cstr(" & fldIDname & ")='" & StrID & "'"
rs.Open ssql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Multiply = 1
For i = 1 To rs.RecordCount
Multiply = Multiply * rs.Fields(fldname).Value
rs.MoveNext
Next
rs.Close
Set rs = Nothing
End Function