遍历评估当前月份和前两个月的记录集
Moving Through a Recordset evaluating current month plus two previous months
我正在编写 VBA 宏来管理手机数据库。我正在测试的是,如果前几个月(当前月加上前两个月)中的任何一个有 activity。如果所有三个月都显示 "no usage" 那么我们将选中一个布尔框 true 然后停用该行。
我写了部分逻辑。
我从具有计算字段的交叉表查询开始。这样我的查询始终保持结构。我评估了字段的价值。如果字段值大于零,则继续。如果等于 0,则将计数器设置为 1。一旦计数器达到 3,将该行的布尔框标记为 true 表示未使用。
代码如下。我的 table 上的月份列之前有两列,因此我的 rsIndexes 从 2 开始到 12,整个 table 有 14 列。使用我创建的 For Next 循环,我可以处理非常自然发生的情况,例如 7、6、5(七月、六月、五月)。
这是我的问题:当您必须评估记录集索引为 1、12、11(一月、十二月、十一月)的数据时,如何使用此结构并在记录集中移动。从1回到12再回到11是我的问题
Sub detectNonUsage2()
Dim rsNonUsageList As New ADODB.Recordset
lastMonthIndex = [Forms]![frmNonUsageLogic]![cboCurMonth].ListIndex
lastMonthIndex = (lastMonthIndex + 2)
MsgBox lastMonthIndex
rsSQL = "Select * from qryDeviceIDbyMonth"
rsNonUsageList.Open rsSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
Do Until rsNonUsageList.EOF
curVal = rsNonUsageList(lastMonthIndex)
curMonth = rsNonUsageList(lastMonthIndex).Name
curCount = 0
For curField = lastMonthIndex To lastMonthIndex - 2 Step -1
If rsNonUsageList(lastMonthIndex) = 0 Then
curCount = curCount + 1
Else
End If
curMonth = rsNonUsageList(curField).Name
Debug.Print curMonth
Next curField
If curCount >= 3 Then
'set flag for nonUsage
Else
End If
rsNonUsageList.MoveNext
Loop
rsNonUsageList.Close
End Sub
我不确定您所说的 "use this structure" 是什么意思,因为您的解释和代码有点难以理解。如果您只需要迭代一年中的数字月份,一个有效的解决方案将使用 模块化算法 。考虑以下表达式:
result = ((month + delta + 11) mod 12) + 1
如果从起始 month
移动 delta
步,result
将是结果月份,例如+1 或 -1。这假设 month
和 result
在 1 到 12 的集合中。
另一种方法是简单地使用基本的 if 语句 来检测和控制处于边缘时发生的情况。如果继续前进则:
if month == 12 then result = 1 else result = month + 1
否则向后移动
if month == 1 then result = 12 else result = month - 1
您可以使用这样的简单查询:
Update
CellLines
Set
Deactivated = True
Where
PhoneId Not In
(Select Distinct PhoneId From CellCalls
Where CallDate Between
DateSerial(Year(Date()), Month(Date()) - 2, 1) And Date()
我正在编写 VBA 宏来管理手机数据库。我正在测试的是,如果前几个月(当前月加上前两个月)中的任何一个有 activity。如果所有三个月都显示 "no usage" 那么我们将选中一个布尔框 true 然后停用该行。
我写了部分逻辑。
我从具有计算字段的交叉表查询开始。这样我的查询始终保持结构。我评估了字段的价值。如果字段值大于零,则继续。如果等于 0,则将计数器设置为 1。一旦计数器达到 3,将该行的布尔框标记为 true 表示未使用。
代码如下。我的 table 上的月份列之前有两列,因此我的 rsIndexes 从 2 开始到 12,整个 table 有 14 列。使用我创建的 For Next 循环,我可以处理非常自然发生的情况,例如 7、6、5(七月、六月、五月)。
这是我的问题:当您必须评估记录集索引为 1、12、11(一月、十二月、十一月)的数据时,如何使用此结构并在记录集中移动。从1回到12再回到11是我的问题
Sub detectNonUsage2()
Dim rsNonUsageList As New ADODB.Recordset
lastMonthIndex = [Forms]![frmNonUsageLogic]![cboCurMonth].ListIndex
lastMonthIndex = (lastMonthIndex + 2)
MsgBox lastMonthIndex
rsSQL = "Select * from qryDeviceIDbyMonth"
rsNonUsageList.Open rsSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
Do Until rsNonUsageList.EOF
curVal = rsNonUsageList(lastMonthIndex)
curMonth = rsNonUsageList(lastMonthIndex).Name
curCount = 0
For curField = lastMonthIndex To lastMonthIndex - 2 Step -1
If rsNonUsageList(lastMonthIndex) = 0 Then
curCount = curCount + 1
Else
End If
curMonth = rsNonUsageList(curField).Name
Debug.Print curMonth
Next curField
If curCount >= 3 Then
'set flag for nonUsage
Else
End If
rsNonUsageList.MoveNext
Loop
rsNonUsageList.Close
End Sub
我不确定您所说的 "use this structure" 是什么意思,因为您的解释和代码有点难以理解。如果您只需要迭代一年中的数字月份,一个有效的解决方案将使用 模块化算法 。考虑以下表达式:
result = ((month + delta + 11) mod 12) + 1
如果从起始 month
移动 delta
步,result
将是结果月份,例如+1 或 -1。这假设 month
和 result
在 1 到 12 的集合中。
另一种方法是简单地使用基本的 if 语句 来检测和控制处于边缘时发生的情况。如果继续前进则:
if month == 12 then result = 1 else result = month + 1
否则向后移动
if month == 1 then result = 12 else result = month - 1
您可以使用这样的简单查询:
Update
CellLines
Set
Deactivated = True
Where
PhoneId Not In
(Select Distinct PhoneId From CellCalls
Where CallDate Between
DateSerial(Year(Date()), Month(Date()) - 2, 1) And Date()