首次发布海报,MS ACCESS新手。我正在使用MS ACCESS 2002-2003数据库格式。 我有一个Mainform,我有一个标签控件。在其中一个选项卡中,我有一个子窗体,我放置了另一个选项卡式控件。在其中一个选项卡中,我有一个包含类别列表的组合框,我试图使用VBA将该值传递给MS Chart控件。只要没有WHERE子句,图表就会更新(它将显示所有类别)。如果我从WHERE子句中删除变量并键入VALUE,它仍然不起作用。当我运行Debug.Print strSQLPIE时。 strSQLPIE值正确显示SQL语句和WHERE子句。但是,图表一直显示为空白。如果我复制该SQL语句并将其放入新查询中,则查询运行正常。
我已将问题归结为图表控件的ROWSOURCE属性中的SQL语句的WHERE子句。我错过了什么?
以下是代码:
Dim DB As Database
Dim rst As Recordset
Dim strSQLSB As String ' subform datasheet sql
Dim strSQLPIE As String ' chart control sql
Dim SelCatStr As String ' combobox for categories
Dim SelYrStr As String ' not being used for now
Dim SelYrMtStr As String ' not being used for now
SelCatStr = Me.PIEChart.Value ' combobox selected value
'SelYrStr = Me.SelectYear.Value ' not being used for now
'SelYrMtStr = Me.SelectYearMonth.Value ' not being used for now
strSQLSB = "SELECT UNIONMASTER.ByMonth, UNIONMASTER.Category, UNIONMASTER.Category1, UNIONMASTER.AdjustedAmount " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & SelCatStr & "';" THIS UPDATES THE DATASHEET IN ANOTHER SUBFORM IN THE SAME TAB CORRECTLY ON CHANGE OF COMBOBOX.
strSQLPIE = "SELECT UNIONMASTER.Category1, Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & Me.PIEChart.Value & "';"
我在WHERE CLAUSE中使用SelCatStr和Me.PIEChart.Value进行了尝试,但仍然无效。我甚至尝试输入一个值,它仍然无法正常工作。调试窗口正确显示SQL和where子句,但只要WHERE子句存在,图表就会显示为空白。
Me.PIESubform.Form.RecordSource = strSQLSB
Me.[PIESubform].Form.Requery
Debug.Print strSQLPIE
Me.[CatChart].RowSource = strSQLPIE
Me.[CatChart].Requery
我认为问题与CHART的rowsource设置中的WHERE子句有关。正如我所说,当我删除where子句时,图表会正确显示所有类别,当我粘贴WHERE子句时,使用VALUE或变量,它显示为空白。
如果使用此图表,该图表有效:
strSQLPIE = "SELECT UNIONMASTER.Category1, Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"GROUP BY UNIONMASTER.Category1 ;"
Me.CatChart.RowSource = strSQLPIE
Me.CatChart.Requery
有什么想法吗?
答案 0 :(得分:1)
您的查询中可能缺少“GROUP BY”子句。您的查询应该是这样的。
strSQLPIE = "SELECT UNIONMASTER.Category1, " &_
"Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & Me.PIEChart.Value & "';" &_
"GROUP BY UNIONMASTER.Category1 ;"