|
楼主 |
发表于 2021-12-28 14:33
|
显示全部楼层
本帖最后由 zzg_123 于 2021-12-28 15:52 编辑
Function 执行查询()
Dim strWhere As String
Dim strField As String
Dim strFieldxm As String
Dim strFieldlb As String
Dim strFormat As String
Dim strCaption As String
Dim strCaptionlb As String
Dim strCaptionxm As String
Const xlCategory = 1
If Nz(Me.开始日期) <> "" Then strWhere = strWhere & " AND 生产日期 >= #" & Format$(Me.开始日期, "yyyy-mm-dd") & "#"
If Nz(Me.截止日期) <> "" Then strWhere = strWhere & " AND 生产日期 <= #" & Format$(Me.截止日期, "yyyy-mm-dd") & "#"
If Nz(Me.项目名称) <> "" Then strWhere = strWhere & " AND ([项目名称] Like '*" & Me.项目名称 & "*')"
If strWhere <> "" Then strWhere = " WHERE " & Mid(strWhere, 6)
Select Case Me.区间单位
Case 1
strCaption = "日"
strField = "生产日期"
strFormat = "yyyy-mm-dd"
Case 2
strCaption = "周"
strField = "Year([生产日期]) & '-' & Format(DatePart('ww',[生产日期]),'00')"
strFormat = "yyyy-mm"
Case 3
strCaption = "月"
strField = "Format([生产日期],'yyyy-mm')"
strFormat = "yyyy-mm"
Case 4
strCaption = "季"
strField = "Year([生产日期]) & '-' & DatePart('q',[生产日期])"
strFormat = "yyyy-mm"
Case 5
strCaption = "年"
strField = "Year([生产日期])"
strFormat = "0"
End Select
strFieldxm = "统计项目"
Select Case Me.统计项目
Case 1
strCaptionxm = "平均值"
strFieldxm = "项目名称"
Case 2
strCaptionxm = "最大值"
strFieldxm = "项目名称"
Case 3
strCaptionxm = "最小值"
strFieldxm = "项目名称"
End Select
strFieldlb = "项目名称"
Select Case Me.项目名称
Case 1
strCaptionlb = "项目名称"
strFieldlb = "项目名称"
End Select
Me.子窗体!周期.Controls(0).Caption = strCaption
Me.子窗体!项目名称.Controls(0).Caption = strCaptionlb
Me.子窗体!统计项目.Controls(0).Caption = strFieldxm
Me.子窗体.Form.RecordSource = " SELECT " & strField & " AS 周期," _
& strFieldlb & " AS 项目名称,Avg(" & strFieldxm & ") AS 平均值 , Min(" & strFieldxm & ") AS 最小值 , Max(" & strFieldxm & ") AS 最大值 " _
& " FROM mainlist" & strWhere _
& " GROUP BY " & strField & "," & strFieldlb & ";"
Me.Graph.RowSource = " TRANSFORM Avg(" & strFieldxm & ") AS 平均值 , Min(" & strFieldxm & ") AS 最小值 , Max(" & strFieldxm & ") AS 最大值" _
& " SELECT " & strField & " AS 周期" _
& " FROM mainlist" & strWhere _
& " GROUP BY " & strField & "" _
& " PIVOT " & strField
On Error Resume Next
Me.Graph.Axes(xlCategory).TickLabels.NumberFormatLocal = strFormat
End Function
|
|