Hi to all,
I have a form with two list boxes(miltiselect).
First box I want to allow user to select one or many offices, second box I want user to be able to select one or many job titles. My code below works ok when one selection of office and job title, but not working correctly when selection one office and many job titles, I get all offices for the job titles.
The report source is a query.
Query points to form, report points to query.
In my form I have the following: Under office I ahve in the criteria as:Like [forms]![frm_selection]![office]&"*"
and under job title criteriaI have: Like [forms]![FRM_selection]![job_title]
Here's my code:
Private Sub OK_Click()
On Error GoTo err_preview_report_click
Dim title, wclause, RO_name, ro
Dim frm As Form, ctl As Control, ctl1 As Control
Dim varItm As Variant
Dim strlist, strWhere, strTitle, strGroup As String
Dim strlsit2 As String
strWhere = "[reg office] = "
strTitle = "[Type Contract]= "
Set ctl = Me.[Ro_list]
Set ctl1 = Me![report_list]
'Build my list of offices to pass to query
For Each varItm In ctl.ItemsSelected
If strWhere = "[reg office] = " Then
strWhere = strWhere & "'" & ctl.ItemData(varItm) & "'"
Else
strWhere = strWhere & " or [reg office] = '" & ctl.ItemData(varItm) & "'"
End If
Next varItm
'Build my list of type contracts to pass to query
For Each varItm In ctl1.ItemsSelected
If strTitle = "[Type Contract]= " Then
strTitle = strTitle & "'" & ctl1.ItemData(varItm) & "'"
Else
strTitle = strTitle & " or [Type contract] = '" & ctl1.ItemData(varItm) & "'"
End If
Next varItm
'strGroup = strWhere &" and " strTitle
'strWhere = strWhere & " or [reg office] = '" & ctl.ItemData(varItm) & "'"
strGroup = strWhere & " and " & strTitle
wclause = "rpt_current_diskette_paper_report"
' DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
DoCmd.OpenReport wclause, acViewNormal, , strGroup
'DoCmd.OpenReport wclause, acViewPreview, WhereCondition:=strGroup
If wclause = "" Then
Resume EXIT_OK_CLICK
'GoTo err_preview_report_click
'MsgBox "You must Select a report!"
'Resume EXIT_OK_CLICK
End If
Me.[report_list] = ""
Me.[Ro_list] = ""
Call Clear_MultiSelect
EXIT_OK_CLICK:
Exit Sub
err_preview_report_click:
MsgBox "Select a report!"
Resume EXIT_OK_CLICK
End Sub
I have a form with two list boxes(miltiselect).
First box I want to allow user to select one or many offices, second box I want user to be able to select one or many job titles. My code below works ok when one selection of office and job title, but not working correctly when selection one office and many job titles, I get all offices for the job titles.
The report source is a query.
Query points to form, report points to query.
In my form I have the following: Under office I ahve in the criteria as:Like [forms]![frm_selection]![office]&"*"
and under job title criteriaI have: Like [forms]![FRM_selection]![job_title]
Here's my code:
Private Sub OK_Click()
On Error GoTo err_preview_report_click
Dim title, wclause, RO_name, ro
Dim frm As Form, ctl As Control, ctl1 As Control
Dim varItm As Variant
Dim strlist, strWhere, strTitle, strGroup As String
Dim strlsit2 As String
strWhere = "[reg office] = "
strTitle = "[Type Contract]= "
Set ctl = Me.[Ro_list]
Set ctl1 = Me![report_list]
'Build my list of offices to pass to query
For Each varItm In ctl.ItemsSelected
If strWhere = "[reg office] = " Then
strWhere = strWhere & "'" & ctl.ItemData(varItm) & "'"
Else
strWhere = strWhere & " or [reg office] = '" & ctl.ItemData(varItm) & "'"
End If
Next varItm
'Build my list of type contracts to pass to query
For Each varItm In ctl1.ItemsSelected
If strTitle = "[Type Contract]= " Then
strTitle = strTitle & "'" & ctl1.ItemData(varItm) & "'"
Else
strTitle = strTitle & " or [Type contract] = '" & ctl1.ItemData(varItm) & "'"
End If
Next varItm
'strGroup = strWhere &" and " strTitle
'strWhere = strWhere & " or [reg office] = '" & ctl.ItemData(varItm) & "'"
strGroup = strWhere & " and " & strTitle
wclause = "rpt_current_diskette_paper_report"
' DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
DoCmd.OpenReport wclause, acViewNormal, , strGroup
'DoCmd.OpenReport wclause, acViewPreview, WhereCondition:=strGroup
If wclause = "" Then
Resume EXIT_OK_CLICK
'GoTo err_preview_report_click
'MsgBox "You must Select a report!"
'Resume EXIT_OK_CLICK
End If
Me.[report_list] = ""
Me.[Ro_list] = ""
Call Clear_MultiSelect
EXIT_OK_CLICK:
Exit Sub
err_preview_report_click:
MsgBox "Select a report!"
Resume EXIT_OK_CLICK
End Sub