Yes - I added the [JobNbr] already and not sure why I am getting the
In(-1,-1,-1)
Here is the code I actually already have in that works for one or many items
select from the lstcategory list box - I just need to only add to handle
when none is selected to pull up Only the job #'s that are in this List box
(they are based on an sql where ciriteria is set)
WOULD LOVE to just a a FEW lines to handle if NOTHING select to preview ALL
of the job#s that are IN the LIST box.
thanks see below:
Private Sub cmdPreview_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
'strDelim = """" 'Delimiter appropriate to field type. See
note 1.
strDoc = "LaborReport2 BP"
'Loop through the ItemsSelected in the list box.
With Me.lstcategory
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varItem) &
strDelim & ","
'Build up the description from the text in the visible
column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[JobNbr] IN (" & Left$(strWhere, lngLen) & ")" & " And
[checkDate] >= #" & Me.txtCheckStart & "# And [checkDate] <= #" &
Me.txtCheckEnd & "# AND [Policy Year] = " & Me.cmbPolicy
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Name: " & Left$(strDescrip, lngLen)
End If
End If
'Report will not filter if open, so close it. For Access 97, see note 3.
If CurrentProject.AllReports(strDoc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If
'Omit the last argument for Access 2000 and earlier. See note 4.
DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere,
OpenArgs:=strDescrip
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"cmdPreview_Click"
End If
Resume Exit_Handler
End Sub
Damon Heron said:
Did you add the code from my previous post, that reads:
"I left out a crucial line, after the function call, you need to
combine the fieldname with the results of the function call:"
strWhere = "[Your FieldName] " & strWhere
So the string would look something like this:
"[YourFieldName] IN ('apples','oranges','pears')"
So you call the function, and when it returns with the value, you combine
the field name from the listbox that has your Numeric ID #s, and the
combined string is used to restrict your report to just those in the string.
I am troubled by the results you show, though (-1,-1,-1) those should be
unique IDs from the listbox. What is the source of your list box? I am
thinking it should be from a table or query with ID#, Name, etc. and the
function pulls the unique ID for each item in creating the string.
Is that correct?
Damon
babs said:
I am getting a syntax error missing operator IN(-1,-1,-1)
below is the code I put into the function as you suggested - not sure why
it
is not pulling up the [jobnbr]
any ideas,
thanks, barb
Private Function BuildWhereCondition(strControl As String) As String
'Set up the WhereCondition Argument for the reports
Dim varItem As Variant
Dim strWhere As String
Dim strWhereLst As String
Dim ctl As Control
Dim strDelim As String 'Delimiter for this field type.
Set ctl = Me.Controls(strControl)
Select Case ctl.ItemsSelected.Count
Case 0 'Include All
strWhereLst = ""
Case 1 'Only One Selected
strWhereLst = "= " & _
ctl.ItemData(ctl.ItemsSelected(0)) & " "
Case Else 'Multiple Selection
strWhereLst = " IN ("
With ctl
For Each varItem In .ItemsSelected
strWhereLst = strWhereLst & " " & .ItemData(varItem) & ", "
Next varItem
End With
strWhereLst = Left(strWhereLst, Len(strWhereLst) - 2) & ")"
End Select
BuildWhereCondition = strWhereLst
Set ctl = Nothing
End Function
Damon Heron said:
In the function, the ' single quotes surround the text, so eliminate
those
in the Select Case statement:
Select Case ctl.ItemsSelected.Count
Case 0 'Include All
strWhereLst = ""
Case 1 'Only One Selected
strWhereLst = "= " & _
ctl.ItemData(ctl.ItemsSelected(0)) & " "
Case Else 'Multiple Selection
strWhereLst = " IN ("
With ctl
For Each varItem In .ItemsSelected
strWhereLst = strWhereLst & " " & .ItemData(varItem)
&
", "
Next varItem
End With
strWhereLst = Left(strWhereLst, Len(strWhereLst) - 2) & ")"
End Select
Damon
Thanks for your help not sure where to modify code since I am using
numbers
and not text?
Thanks again,
Barb
:
Create a function in a module:
Public Function BuildWhereCondition(strControl As String) As String
'Set up the WhereCondition Argument for the report
Dim varItem As Variant
Dim strWhereLst As String
Dim ctl As Control
Set ctl = Forms!frmYourFormName!(strControl) 'replace with your
form
name that has the listbox
Select Case ctl.ItemsSelected.Count
Case 0 'Include All
strWhereLst = ""
Case 1 'Only One Selected
strWhereLst = "= '" & _
ctl.ItemData(ctl.ItemsSelected(0)) & "'"
Case Else 'Multiple Selection
strWhereLst = " IN ("
With ctl
For Each varItem In .ItemsSelected
strWhereLst = strWhereLst & "'" &
.ItemData(varItem)
&
"', "
Next varItem
End With
strWhereLst = Left(strWhereLst, Len(strWhereLst) - 2) &
")"
End Select
BuildWhereCondition = strWhereLst
Set ctl = Nothing
End Function
Call the function from your form:
strWhere = BuildWhereCondition("lstcategory")
Then open the report with the conditions of strWhere:
DoCmd.OpenReport stDocName, acPreview, , strWhere
Damon
I have my report set up to preview when multiple items are selected
from
the
simple multiselect box. I am populating the list box named
lstcategory
(cat
#) with field from a linked table. I would like the report -if
nothing
is
selected in the listbox to print all categories that populate the
LIst
box.
not sure how to do this???
thanks for your help!
Barb