G
Graham
Looking to use DCount to return a count of specific values - but, what I
appear to be getting is a count of Non Null values.
When I hard code the criteria such as "[CommLearnID] = 8" then the result is
correct - I have spent hours on this - so frustrating.
Code follows
Option Base 1
Option Compare Database
Option Explicit
Dim iMonth, iYear As Integer
Dim sDateCriteria, sSQL As String
Dim aiCount(50) As Integer
Dim asField, asTable As String
Private Sub btnGo_Click()
Dim iCount, iCount2, iField, iLastVal As Integer
Dim sCriteria, sField As String
asField = Array("CommLearnID", "EnqID", "RefFromID", "RefToID")
'asTable = Array("tContacts", "tcCommLearn", "tcEnqType", "tcRefList",
"tcRefList")
sDateCriteria = "[ContactDate] >= #" & DateSerial(iYear, iMonth, 1) & "#
And [ContactDate] < #" _
& DateSerial(iYear, iMonth + 1, 1) & "#"
sSQL = "DELETE tContactStats.* " _
& "FROM tContactStats " _
& "WHERE (tContactStats.CalMth = " & iMonth & " And
tContactStats.CalYr = " & iYear & ");"
'MsgBox iMonth & " | " & iYear
DoCmd.SetWarnings False
DoCmd.RunSQL sSQL
DoCmd.SetWarnings True
For iField = 1 To 4
iLastVal = 0
sField = "[" & asField(iField) & "]"
For iCount = 1 To 50
aiCount(iCount) = 0
Next iCount
For iCount = 1 To 50
'sCriteria = "'" & sDateCriteria & " And " & sField & " = " &
iCount & "'"
sCriteria = """" & sField & " = " & iCount & """"
aiCount(iCount) = DCount("[ContactID]", "tContacts", sCriteria)
'MsgBox sCriteria & " | " & iCount & " | " & aiCount(iCount)
If aiCount(iCount) > 0 _
Then
iLastVal = iCount
End If
Next iCount
If iLastVal > 0 _
Then
For iCount2 = 1 To iLastVal
If aiCount(iCount2) > 0 _
Then
sSQL = "INSERT INTO tContactStats (Identifier, ID,
CalYr, CalMth, Count) " _
& "VALUES ('" & asField(iField) & "', " &
iCount2 & ", " & iYear & ", " & iMonth & ", " & aiCount(iCount2) & ");"
'MsgBox sSQL
DoCmd.SetWarnings False
DoCmd.RunSQL sSQL
DoCmd.SetWarnings True
End If
Next iCount2
End If
Next iField
End Sub
Thanks in advance. This forum is invaluable, particularlt for a casual
Access developer.
(Access 2002)
appear to be getting is a count of Non Null values.
When I hard code the criteria such as "[CommLearnID] = 8" then the result is
correct - I have spent hours on this - so frustrating.
Code follows
Option Base 1
Option Compare Database
Option Explicit
Dim iMonth, iYear As Integer
Dim sDateCriteria, sSQL As String
Dim aiCount(50) As Integer
Dim asField, asTable As String
Private Sub btnGo_Click()
Dim iCount, iCount2, iField, iLastVal As Integer
Dim sCriteria, sField As String
asField = Array("CommLearnID", "EnqID", "RefFromID", "RefToID")
'asTable = Array("tContacts", "tcCommLearn", "tcEnqType", "tcRefList",
"tcRefList")
sDateCriteria = "[ContactDate] >= #" & DateSerial(iYear, iMonth, 1) & "#
And [ContactDate] < #" _
& DateSerial(iYear, iMonth + 1, 1) & "#"
sSQL = "DELETE tContactStats.* " _
& "FROM tContactStats " _
& "WHERE (tContactStats.CalMth = " & iMonth & " And
tContactStats.CalYr = " & iYear & ");"
'MsgBox iMonth & " | " & iYear
DoCmd.SetWarnings False
DoCmd.RunSQL sSQL
DoCmd.SetWarnings True
For iField = 1 To 4
iLastVal = 0
sField = "[" & asField(iField) & "]"
For iCount = 1 To 50
aiCount(iCount) = 0
Next iCount
For iCount = 1 To 50
'sCriteria = "'" & sDateCriteria & " And " & sField & " = " &
iCount & "'"
sCriteria = """" & sField & " = " & iCount & """"
aiCount(iCount) = DCount("[ContactID]", "tContacts", sCriteria)
'MsgBox sCriteria & " | " & iCount & " | " & aiCount(iCount)
If aiCount(iCount) > 0 _
Then
iLastVal = iCount
End If
Next iCount
If iLastVal > 0 _
Then
For iCount2 = 1 To iLastVal
If aiCount(iCount2) > 0 _
Then
sSQL = "INSERT INTO tContactStats (Identifier, ID,
CalYr, CalMth, Count) " _
& "VALUES ('" & asField(iField) & "', " &
iCount2 & ", " & iYear & ", " & iMonth & ", " & aiCount(iCount2) & ");"
'MsgBox sSQL
DoCmd.SetWarnings False
DoCmd.RunSQL sSQL
DoCmd.SetWarnings True
End If
Next iCount2
End If
Next iField
End Sub
Thanks in advance. This forum is invaluable, particularlt for a casual
Access developer.
(Access 2002)