2017-04-13 59 views
0

当我运行这段代码时,它没有显示我需要看到的正确记录。我错过了一些地方,但我没有看到它。这个想法是为了计算所有已完成,正在进行的计数,而不是从前一个月开始,并给我总计。显示前几个月的总计,我错过了什么?

Sub Update() 
StatusCount "Completed" 
StatusCount "In Progress" 
StatusCount "Not Started" 
'StatusCount "Moved to Cleanup" 
'StatusCount "N/A" 
'StatusCount "This is a new category" ', Now - 2, Now + 3 

End Sub 

Sub StatusCount(ByVal status As String, Optional start_date As Date, Optional end_date As Date) 
Dim i As Variant 
Dim db As DAO.Database 
Dim rst As DAO.Recordset 
Dim qd As DAO.QueryDef 

Set db = CurrentDb 

Dim SQL As String 
If start_date = 0 Or end_date = 0 Then 
    SQL = "insert into statussummary (Count,mmyy,status) Select count(*), [created], [research status] " & _ 
      "from [gwc master list]" & _ 
      "where [research status] = '" & status & "'" & _ 
      "group by [research status], [created]" 
Else 
    SQL = "insert into statussummary (Count,mmyy,status) Select count(*), [created],[research status] " & _ 
      "from [gwc master list]" & _ 
      "where [research status] = '" & status & "'" & _ 
      " and [created] > #" & start_date & "# and created < #" & end_date & "#" & _ 
      "group by [research status], [created]" 
End If 

db.Execute (SQL) 
rc = db.RecordsAffected 
If rc = 0 Then 
    Debug.Print status & ": " & rc 
    SQL = "insert into statussummary (Count,status) values (" & rc & ", '" & status & "')" 
    db.Execute (SQL) 
End If 
End Sub 

任何帮助表示赞赏 -D

+0

你缺少一个空格GROUP BY的 –

+0

之前,能否请您解释一下? – Deke

+0

计数和状态都是保留字,很可能会让你失望。我会改变他们到别的地方,或者至少把方括号放在他们周围。 – Minty

回答

0

首先,你需要改变你的数据库表字段名。如前所述,“状态”是保留的,正如“count”一样。因此,您的三个字段应该是[R_COUNT], [CREATED], [R_STATUS]。然后,你调整你的VBA代码如下所示:

Sub StatusCount(ByVal status_var As String, Optional start_date As Date, Optional end_date As Date) 

这应该解决“身份”的第一次冲突。然后,修改第一个SQL语句,以指定要插入到statussummary中的正确字段(正如我将名称更改为上面的那样)。不要忘记,在继续标记之前,你在代码中错过了一两个空格。

If start_date = 0 Or end_date = 0 Then 
    SQL = "insert into statussummary ([R_COUNT], [CREATED], [R_STATUS]) Select COUNT(*), [created], [research status] " & _ 
      "from [gwc master list] " & _ 
      "where [research status] = '" & status_var & "'" & _ 
      " group by [research status], [created];" 
Else 
    SQL = "insert into statussummary ([R_COUNT], [CREATED], [R_STATUS]) Select COUNT(*), [created], [research status] " & _ 
      "from [gwc master list] " & _ 
      "where [research status] = '" & status_var & "'" & _ 
      " and [created] > #" & start_date & "# and created < #" & end_date & "#" & _ 
      " group by [research status], [created];" 
End If 

最后,解决您的最后一条语句:

rc = db.RecordsAffected 
If rc = 0 Then 
    Debug.Print status & ": " & rc 
    SQL = "insert into statussummary ([R_COUNT], [R_STATUS]) values (" & rc & ", '" & status_var & "');" 
    db.Execute (SQL) 
End If 
End Sub 
+0

这几乎是完美的,但它仍然没有给我我想查询的记录范围。我需要它向我展示上个月的所有“已完成”,“进行中”和“未开始”。这些数据正从[研究状况]字段计算。我最终还是需要按月排序,但这是另一次。有什么建议么? – Deke

+0

您可能需要从[gwc master list]中选择COUNT(*),[created],[research status],其中[“Completed”“,”“In progress”“,”“未开始“”)和[已创建]>#“&start_date&”#并创建了<#>&end_date&“#”&group by [research status],[created];“' – SandPiper

+0

如果您不仅想要从你的输入框指定的状态,你需要让你的查询看起来更像这样^ – SandPiper