D
Dan @BCBS
The code below works perfect - it produces a report and also creates an email
based on choices from 3 list boxes.
My question is: I need to copy this code to another command button and have
it produce results of the Query (DoCmd.OpenQuery "q_AuditDump")
But when I replace this DoCmd the results are not limited to the 3 list boxes.
And I do not get any errors.. As long as I have values picked in the list
boxes...
Note: I did try to add a criteria to the Query
[Forms]![f_AuditDump]![ListProduct] which did not work:
Her is the working code that I need to change to run the query based on the
3 choices.
Dim stDocName As String
Dim stAreaList As String
Dim stProductList As String
Dim stLinkCriteria As String
'first time thru loop?
Dim FirstTime As Boolean
Dim stArea As Variant
Dim stProduct As Variant
stDocName = "r_AuditDump"
stAreaList = ""
stProductList = ""
'dates
If IsNull(txtStart) Or IsNull(txtEnd) Then
MsgBox "Please enter start and end dates"
Exit Sub
End If
'get areas selected in ListArea
FirstTime = True
For Each stArea In ListArea.ItemsSelected
If FirstTime Then
stAreaList = "In('" & ListArea.ItemData(stArea) & "'"
FirstTime = False
Else
stAreaList = stAreaList & ",'" & ListArea.ItemData(stArea) & "'"
End If
Next stArea
If Not FirstTime Then
stAreaList = stAreaList & ")"
End If
'get products in ListProduct
FirstTime = True
For Each stProduct In ListProduct.ItemsSelected
If FirstTime Then
stProductList = "In('" & ListProduct.ItemData(stProduct) & "'"
FirstTime = False
Else
stProductList = stProductList & ",'" & ListProduct.ItemData(stProduct) & "'"
End If
Next stProduct
If Not FirstTime Then
stProductList = stProductList & ")"
End If
'create criteria string
'stAreaList
If Len(Trim(Nz(stAreaList, ""))) > 0 Then
stLinkCriteria = "[TR_GBU] " & stAreaList & " And "
End If
'stProductList
If Len(Trim(Nz(stProductList, ""))) > 0 Then
stLinkCriteria = stLinkCriteria & "[TR_PRODUCT] " & stProductList & " And "
End If
'removes the last 'And' and spaces
stLinkCriteria = Left(stLinkCriteria, Len(stLinkCriteria) - 5)
'-------------------------
' for debugging
'MsgBox stLinkCriteria
'-------------------------
'open report in preview mode AND send it email
DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
'DoCmd.SendObject acReport, stDocName, acFormatRTF, , , , , , True
Exit_cmdViewDump:
Exit Sub
Err_cmdViewDump_Click:
///////////////////// END oF CODE//////////////////
based on choices from 3 list boxes.
My question is: I need to copy this code to another command button and have
it produce results of the Query (DoCmd.OpenQuery "q_AuditDump")
But when I replace this DoCmd the results are not limited to the 3 list boxes.
And I do not get any errors.. As long as I have values picked in the list
boxes...
Note: I did try to add a criteria to the Query
[Forms]![f_AuditDump]![ListProduct] which did not work:
Her is the working code that I need to change to run the query based on the
3 choices.
Dim stDocName As String
Dim stAreaList As String
Dim stProductList As String
Dim stLinkCriteria As String
'first time thru loop?
Dim FirstTime As Boolean
Dim stArea As Variant
Dim stProduct As Variant
stDocName = "r_AuditDump"
stAreaList = ""
stProductList = ""
'dates
If IsNull(txtStart) Or IsNull(txtEnd) Then
MsgBox "Please enter start and end dates"
Exit Sub
End If
'get areas selected in ListArea
FirstTime = True
For Each stArea In ListArea.ItemsSelected
If FirstTime Then
stAreaList = "In('" & ListArea.ItemData(stArea) & "'"
FirstTime = False
Else
stAreaList = stAreaList & ",'" & ListArea.ItemData(stArea) & "'"
End If
Next stArea
If Not FirstTime Then
stAreaList = stAreaList & ")"
End If
'get products in ListProduct
FirstTime = True
For Each stProduct In ListProduct.ItemsSelected
If FirstTime Then
stProductList = "In('" & ListProduct.ItemData(stProduct) & "'"
FirstTime = False
Else
stProductList = stProductList & ",'" & ListProduct.ItemData(stProduct) & "'"
End If
Next stProduct
If Not FirstTime Then
stProductList = stProductList & ")"
End If
'create criteria string
'stAreaList
If Len(Trim(Nz(stAreaList, ""))) > 0 Then
stLinkCriteria = "[TR_GBU] " & stAreaList & " And "
End If
'stProductList
If Len(Trim(Nz(stProductList, ""))) > 0 Then
stLinkCriteria = stLinkCriteria & "[TR_PRODUCT] " & stProductList & " And "
End If
'removes the last 'And' and spaces
stLinkCriteria = Left(stLinkCriteria, Len(stLinkCriteria) - 5)
'-------------------------
' for debugging
'MsgBox stLinkCriteria
'-------------------------
'open report in preview mode AND send it email
DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
'DoCmd.SendObject acReport, stDocName, acFormatRTF, , , , , , True
Exit_cmdViewDump:
Exit Sub
Err_cmdViewDump_Click:
///////////////////// END oF CODE//////////////////