Private Sub btnGetReport_Click()
' Declare variables
Dim dbs As DAO.Database
Dim qdf1 As DAO.QueryDef
Dim qdf2 As DAO.QueryDef
Dim varItem1 As Variant
Dim strNamesCriteria As String
Dim strModelCriteria As String
Dim strFrMonth As Variant
Dim strToMonth As String
Dim strFrYear As String
Dim strToYear As String
' Get the database and stored query
Set dbs = CurrentDb()
Set qdf1 = db.QueryDefs("Q1")
Set qdf2 = db.QueryDefs("Q2")
strToMonth = Me.cbToMonth.Value
strFrMonth = Me.cbFromMonth.Value
strToYear = Me.cbToYear.Value
strFrYear = Me.cbFromYear.Value
' Loop through the selected items in the list box and build a text string
For Each varItem1 In Me!lstNames.ItemsSelected
strNamesCriteria = strNamesCriteria & ",'" & Me!lstNames.ItemData(varItem1) & "'"
Next varItem1
' Loop through the selected items in the list box and build a text string
For Each varItem2 In Me!lstNames.ItemsSelected
strModelCriteria = strModelCriteria & ",'" & Me!lstNames.ItemData(varItem2) & "'"
Next varItem2
' Remove the leading comma from the string
strNamesCriteria = Right(strNamesCriteria, Len(strNamesCriteria) - 1)
strModelCriteria = Right(strModelCriteria, Len(strModelCriteria) - 1)
' Get's the SQL string
If (strFrMonth = strToMonth) And (strFrYear = strToYear) Then
qdf1 = getSQLstr(strModelCriteria, strNamesCriteria, strFrMonth, strFrYear)
' Apply the new SQL statement to the query
qdf1.SQL = strSQL1
Else
qdf1 = getSQLstr(strModelCriteria, strNamesCriteria, strFrMonth, strFrYear)
qdf2 = getSQLstr(strModelCriteria, strNamesCriteria, strToMonth, strToYear)
' Apply the new SQL statement to the query
qdf1.SQL = strSQL1
qdf2.SQL = strSQL2
End If
' Empty the memory
Set dbs = Nothing
Set qdf1 = Nothing
Set qdf2 = Nothing
End Sub
'*******
' Build the new SQL statement incorporating the string
'*******
Private Function getSQLstr(a As String, b As String, c As String, d As String) As String
Dim strSQL As String
Dim strLgnA As String
Dim strLgnB As String
Dim strLgnC As String