当我运行此代码时,它不会显示我需要查看的正确记录。我在某个地方遗失了一些东西,但我没有看到它。这个想法是为了计算所有已完成的,正在进行的和未从上个月开始的计数,并给我总数。
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 :(得分:0)
首先,您需要更改数据库表字段名称。如上所述,“状态”是保留的,“计数”也是如此。因此,您的三个字段应该类似于[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