我搜寻了有关使用VBA更改列表框记录源的想法的主题,并且创建了自己的作品..但它只能以1/2方式工作。
我有一个表单,用于跟踪员工的出勤问题。.我们有2个地点,在Az中1个,在Tx中1个。。由于最近在Az中更改的法律,我们的政策已更新。保留1年(或365天),在Tx中发生3个月(90天)。
基本思想是这样的: 如果“主管状态”的文本框= Az,则HISTORYBOX应显示/计算365天的记录。否则,如果“主管状态” = Tx,则HISTORYBOX应显示/计算90天的记录。
我的问题是选择AZ主管时..它仍在提取90天版本的代码..而不是365天版本。
这是到目前为止我已经能够创建的代码:
Public Sub ChangeHistory()
Dim strSQL As String
If Me.txtsupervisorstate.Value = "AZ" Then
strSQL = "SELECT OccuTable.ValueOfOccurance, " & _
" OccuTable.Short_Code_Occurance, " & _
" OccuTable.OccuranceDate, " & _
" OccuTable.Roll_Off_Date, " & _
" OccuTable.Notes, " & _
" OccuTable.AssociatedIDNumber " & _
" FROM OccuTable GROUP BY OccuTable.ValueOfOccurance, " & _
" OccuTable.Short_Code_Occurance, " & _
" OccuTable.OccuranceDate, " & _
" OccuTable.Roll_Off_Date, " & _
" OccuTable.Notes, " & _
" OccuTable.AssociatedIDNumber " & _
" WHERE (((OccuTable.OccuranceDate) Between
Date() And Date()-365) And " & _
"
((OccuTable.AssociatedIDNumber)=Forms!OccuranceTracker!txtAssociateID))
ORDER BY OccuTable.OccuranceDate DESC;"
ElseIf Me.txtsupervisorstate.Value = "TX" Then
strSQL = " SELECT OccuTable.ValueOfOccurance, " & _
" OccuTable.Short_Code_Occurance, " & _
" OccuTable.OccuranceDate, " & _
" OccuTable.Roll_Off_Date, " & _
" OccuTable.Notes, " & _
" OccuTable.AssociatedIDNumber " & _
" FROM OccuTable GROUP BY OccuTable.ValueOfOccurance, " & _
" OccuTable.Short_Code_Occurance, " & _
" OccuTable.OccuranceDate, " & _
" OccuTable.Roll_Off_Date, " & _
" OccuTable.Notes, " & _
" OccuTable.AssociatedIDNumber " & _
" WHERE (((OccuTable.OccuranceDate) Between
Date() And Date()-90) And
((OccuTable.AssociatedIDNumber)=Forms!OccuranceTracker!txtAssociateID))
ORDER BY OccuTable.OccuranceDate DESC;"
Me.listBoxPastOccurances.RowSource = strSQL
End If
Call SumOfOccu
End Sub
这是表格的样子:
答案 0 :(得分:0)
我的天哪,我很想这个问题。我刚接触VBA的过程是试图找到最复杂和最令人困惑的方法来完成这项工作。.在对StackOverflow进行了大量研究之后,我发现我可以编写一个查询并保存该查询以备将来使用。.因此,我编写了2个单独的查询(Occurrences365和Occurrences90)。这些重复了对列表框的原始查询(当我只需要90天版本时),该列表框可以正常工作。这使创建365天版本变得非常简单。
然后,我能够将所有其他复杂的代码简化为:
Public Sub ClearHistory()
If Me![txtsupervisorstate] = "AZ" Then
Me![listBoxPastOccurances].RowSource = "Occurrences365"
ElseIf Me![txtsupervisorstate] = "TX" Then
Me![listBoxPastOccurances].RowSource = "Occurrences90"
End If
Call SumOfOccu
End Sub
Public Function SumOfOccu()
If Me![txtsupervisorstate] = "AZ" Then
Me.txtSumOfOccu = DSum("valueofoccurance", "occurrences365")
ElseIf Me![txtsupervisorstate] = "TX" Then
Me.txtSumOfOccu = DSum("valueofoccurance", "occurrences90")
End If
End Function
现在,我可以回过头来,开始消除不必要的子功能/功能,清理命名约定并使之总体上更“干净”。
对于任何有兴趣的人..我将它们写为已保存的查询后,查询如下所示。
SELECT OccuTable.ValueOfOccurance, OccuTable.ShortCodeOccurance,
OccuTable.OccuranceDate, OccuTable.RollOffDate, OccuTable.Notes,
OccuTable.AssociatedIDNumber
FROM OccuTable
GROUP BY OccuTable.ValueOfOccurance, OccuTable.ShortCodeOccurance,
OccuTable.OccuranceDate, OccuTable.RollOffDate, OccuTable.Notes,
OccuTable.AssociatedIDNumber
HAVING (((OccuTable.OccuranceDate) Between Date() And Date()-365) AND
((OccuTable.AssociatedIDNumber)=[Forms]![OccuranceTracker]![txtAssociateID]))
ORDER BY OccuTable.OccuranceDate DESC;
再次。这2个之间的唯一区别是这一个。
And Date()-365)
And Date()-90)
但是一切正常!
非常感谢您的建议,并促使我继续寻找!