David Ball
Registered User.
- Local time
- Tomorrow, 09:08
- Joined
- Aug 9, 2010
- Messages
- 230
Hi,
I have a listbox (List10) on a form that has a command button (cmdOpenQuery6) that runs a report (rptJC_Manhours_JC_DC_SI_2) via a query (qryJC_Manhours_JC_DC_SI_3).
The row source of List10 is SELECT DISTINCT [tblSMSdata].[Project] FROM tblSMSdata UNION SELECT "ALL" FROM tblSMSdata
The listbox contains Project names and when I select one (or several) it opens the report showing records for the project/s selected (the field in the query and table is called Project).
The idea is that I can select a Project or Projects from the listbox and see only those records. This works well but I would also like to be able to select the week ending date so that I can see records by Project for a particular reporting week.
To this end I have set up another listbox (List11) on the same form that pulls dates from the Reporting Week Ending field.
The row source is SELECT DISTINCT [Reporting Week Ending] FROM tblSMSdata;
This is where I start to get well out of my depth and after a few attempts to modify my code to get this working I realise that I need help.
How would I modify the code below to allow me to select Projects and a Reporting Week Ending date and then run the report?
Private Sub cmdOpenQuery6_Click()
On Error GoTo Err_cmdOpenQuery6_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM qryJC_Manhours_JC_DC_SI"
'Build the IN string by looping through the listbox
For i = 0 To List10.ListCount - 1
If List10.Selected(i) Then
If List10.Column(0, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & List10.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [Project] in " & _
"(" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qryJC_Manhours_JC_DC_SI_3"
Set qdef = MyDB.CreateQueryDef("qryJC_Manhours_JC_DC_SI_3", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenReport "rptJC_Manhours_JC_DC_SI_2", acViewReport
'Clear listbox selection after running query
For Each varItem In Me.List10.ItemsSelected
Me.List10.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery6_Click:
Exit Sub
Err_cmdOpenQuery6_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list" _
, , "Selection Required !"
Resume Exit_cmdOpenQuery6_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery6_Click
End If
End Sub
Thanks very much
Dave
I have a listbox (List10) on a form that has a command button (cmdOpenQuery6) that runs a report (rptJC_Manhours_JC_DC_SI_2) via a query (qryJC_Manhours_JC_DC_SI_3).
The row source of List10 is SELECT DISTINCT [tblSMSdata].[Project] FROM tblSMSdata UNION SELECT "ALL" FROM tblSMSdata
The listbox contains Project names and when I select one (or several) it opens the report showing records for the project/s selected (the field in the query and table is called Project).
The idea is that I can select a Project or Projects from the listbox and see only those records. This works well but I would also like to be able to select the week ending date so that I can see records by Project for a particular reporting week.
To this end I have set up another listbox (List11) on the same form that pulls dates from the Reporting Week Ending field.
The row source is SELECT DISTINCT [Reporting Week Ending] FROM tblSMSdata;
This is where I start to get well out of my depth and after a few attempts to modify my code to get this working I realise that I need help.
How would I modify the code below to allow me to select Projects and a Reporting Week Ending date and then run the report?
Private Sub cmdOpenQuery6_Click()
On Error GoTo Err_cmdOpenQuery6_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM qryJC_Manhours_JC_DC_SI"
'Build the IN string by looping through the listbox
For i = 0 To List10.ListCount - 1
If List10.Selected(i) Then
If List10.Column(0, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & List10.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [Project] in " & _
"(" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qryJC_Manhours_JC_DC_SI_3"
Set qdef = MyDB.CreateQueryDef("qryJC_Manhours_JC_DC_SI_3", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenReport "rptJC_Manhours_JC_DC_SI_2", acViewReport
'Clear listbox selection after running query
For Each varItem In Me.List10.ItemsSelected
Me.List10.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery6_Click:
Exit Sub
Err_cmdOpenQuery6_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list" _
, , "Selection Required !"
Resume Exit_cmdOpenQuery6_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery6_Click
End If
End Sub
Thanks very much
Dave