|
将你的单击事件的代码用下面的代码替换试一下。
Dim sql As String
Dim qd As DAO.QueryDef
Dim strWhere As String '定义条件字符串
Set qd = CurrentDb.QueryDefs("qry_电")
strWhere = "" '设定初始值-空字符串
If IsNull(Me.cmb_Year) And IsNull(Me.cmb_month) Then
strWhere = ""
ElseIf Not IsNull(Me.cmb_Year) And IsNull(Me.cmb_month) Then
strWhere = "[年]=" & Me.cmb_Year
ElseIf Not IsNull(Me.cmb_Year) And Not IsNull(Me.cmb_month) And IsNull(Me.cmb_add) Then
strWhere = "[年]=" & Me.cmb_Year & " And [月]=" & Me.cmb_month
ElseIf Not IsNull(Me.cmb_Year) And Not IsNull(Me.cmb_month) And Not IsNull(Me.cmb_add) Then
strWhere = "[年]=" & Me.cmb_Year & " And ( [月]>=1" & " And [月]<=" & Me.cmb_month & ")"
End If
Me.txt_Product = DSum("[产量_吨]", "[产量表]", strWhere)
Me.txt_strwhere = strWhere
sql = "SELECT 电表.单元, Sum(电表.数量_度) AS [数量(度)], Sum(电表.费用_元) AS [费用(元)], [费用(元)]/" & [Forms]![frm_能耗报表]![txt_Product] & " AS [吨麦芽耗电(元)], [数量(度)]/" & [Forms]![frm_能耗报表]![txt_Product] & " AS [吨麦芽耗电(度)], 系统表.系统" & _
" FROM 电表 LEFT JOIN 系统表 ON 电表.单元 = 系统表.单元" & _
" WHERE " & ([Forms]![frm_能耗报表]![txt_strwhere]) & _
" GROUP BY 电表.单元, 系统表.系统 " & _
" UNION ALL SELECT """" as 单元, Sum(电表.数量_度) AS [数量(度)], Sum(电表.费用_元) AS [费用(元)], [费用(元)]/" & [Forms]![frm_能耗报表]![txt_Product] & " AS [吨麦芽耗电(元)], [数量(度)]/" & [Forms]![frm_能耗报表]![txt_Product] & " AS [吨麦芽耗电(度)], 系统表.系统 & ""合计"" as 系统" & _
" FROM 电表 LEFT JOIN 系统表 ON 电表.单元 = 系统表.单元" & _
" WHERE " & ([Forms]![frm_能耗报表]![txt_strwhere]) & _
" GROUP BY 系统表.系统" & _
" UNION ALL SELECT """" as 单元, Sum(电表.数量_度) AS [数量(度)], Sum(电表.费用_元) AS [费用(元)], [费用(元)]/" & [Forms]![frm_能耗报表]![txt_Product] & " AS [吨麦芽耗电(元)], [数量(度)]/" & [Forms]![frm_能耗报表]![txt_Product] & " AS [吨麦芽耗电(度)], "" 电总计"" as 系统" & _
" FROM 电表 LEFT JOIN 系统表 ON 电表.单元 = 系统表.单元" & _
" WHERE " & ([Forms]![frm_能耗报表]![txt_strwhere]) & ";"
qd.sql = sql
qd.Close
Set qd = Nothing
Me.Refresh
DoEvents |
|