P
Patrick
I have 1 ListBox and 5 MultiSelect ListBoxes on an unbound form. I have
written the code to generate the WHERE statement for a DoCmd.OpenForm
command. The problem is that the data being pulled seems to build on each
subsequent part of the where statement instead of filtering each field with
the values from its MSLB.
Private Sub Okay_Click()
Dim MySel As String, frm As Form, ctl As Control, varItem As Variant
Set frm = Forms!frmMain
*****Starting off setting the value for the ListBox*********
MySel = "[tblDetail.COMPANY] = '" & Me.COMPANY & "'"
*****Start in on the MultiSelect ListBoxes**************
Set ctl = frm!Site
b = ctl.ItemsSelected.Count
If b > 0 Then
MySel = MySel & " AND [tblDetail.Site] = '"
For Each varItem In ctl.ItemsSelected
MySel = MySel & ctl.ItemData(varItem) & "' OR [tblDetail.Site]= '"
Next varItem
MySel = Left(MySel, Len(MySel) - 23)
End If
b = 0
Set ctl = frm!Vendor
b = ctl.ItemsSelected.Count
If b > 0 Then
MySel = MySel & " AND [tblDetail.Vendor] = '"
For Each varItem In ctl.ItemsSelected
MySel = MySel & ctl.ItemData(varItem) & "' OR
[tblDetail.Vendor]= '"
Next varItem
MySel = Left(MySel, Len(MySel) - 25)
End If
b = 0
Set ctl = frm!Customer
b = ctl.ItemsSelected.Count
If b > 0 Then
MySel = MySel & " AND [tblDetail.Custkey] = '"
For Each varItem In ctl.ItemsSelected
MySel = MySel & ctl.ItemData(varItem) & "' OR
[tblDetail.Custkey]= '"
Next varItem
MySel = Left(MySel, Len(MySel) - 27)
End If
b = 0
Set ctl = frm!IREF
b = ctl.ItemsSelected.Count
If b > 0 Then
MySel = MySel & " AND [tblDetail.partnbr] = '"
For Each varItem In ctl.ItemsSelected
MySel = MySel & ctl.ItemData(varItem) & "' OR
[tblDetail.partnbr]= '"
Next varItem
MySel = Left(MySel, Len(MySel) - 27)
End If
b = 0
Debug.Print MySel
DoCmd.OpenForm "frmDetail", acFormDS, , MySel, acFormEdit, acWindowNormal
End Sub
When I select a couple Sites and a couple Vendors I generate the statement:
[tblDetail.COMPANY] = 'CB' AND [tblDetail.Site] = '01' OR [tblDetail.Site]=
'12' AND [tblDetail.Vendor] = '0105' OR [tblDetail.Vendor] = '0052' OR
[tblDetail.Vendor]= '0061'
What I end-up getting with my
DoCmd.OpenForm "frmDetail", acFormDS, , MySel, acFormEdit, acWindowNormal
statement is all records for Company CB - Site 01, and only records in the
selected Vendors for Company CB - Site 12
I know it all those ANDs and ORs in there... but I have no idea how to get
what I want.... which is all the records I would get if each field was subset
sequentially.
Can anyone help? Is it even possible to do this in the manner I am
attempting? Is there a better way?
Best regards, patrick
written the code to generate the WHERE statement for a DoCmd.OpenForm
command. The problem is that the data being pulled seems to build on each
subsequent part of the where statement instead of filtering each field with
the values from its MSLB.
Private Sub Okay_Click()
Dim MySel As String, frm As Form, ctl As Control, varItem As Variant
Set frm = Forms!frmMain
*****Starting off setting the value for the ListBox*********
MySel = "[tblDetail.COMPANY] = '" & Me.COMPANY & "'"
*****Start in on the MultiSelect ListBoxes**************
Set ctl = frm!Site
b = ctl.ItemsSelected.Count
If b > 0 Then
MySel = MySel & " AND [tblDetail.Site] = '"
For Each varItem In ctl.ItemsSelected
MySel = MySel & ctl.ItemData(varItem) & "' OR [tblDetail.Site]= '"
Next varItem
MySel = Left(MySel, Len(MySel) - 23)
End If
b = 0
Set ctl = frm!Vendor
b = ctl.ItemsSelected.Count
If b > 0 Then
MySel = MySel & " AND [tblDetail.Vendor] = '"
For Each varItem In ctl.ItemsSelected
MySel = MySel & ctl.ItemData(varItem) & "' OR
[tblDetail.Vendor]= '"
Next varItem
MySel = Left(MySel, Len(MySel) - 25)
End If
b = 0
Set ctl = frm!Customer
b = ctl.ItemsSelected.Count
If b > 0 Then
MySel = MySel & " AND [tblDetail.Custkey] = '"
For Each varItem In ctl.ItemsSelected
MySel = MySel & ctl.ItemData(varItem) & "' OR
[tblDetail.Custkey]= '"
Next varItem
MySel = Left(MySel, Len(MySel) - 27)
End If
b = 0
Set ctl = frm!IREF
b = ctl.ItemsSelected.Count
If b > 0 Then
MySel = MySel & " AND [tblDetail.partnbr] = '"
For Each varItem In ctl.ItemsSelected
MySel = MySel & ctl.ItemData(varItem) & "' OR
[tblDetail.partnbr]= '"
Next varItem
MySel = Left(MySel, Len(MySel) - 27)
End If
b = 0
Debug.Print MySel
DoCmd.OpenForm "frmDetail", acFormDS, , MySel, acFormEdit, acWindowNormal
End Sub
When I select a couple Sites and a couple Vendors I generate the statement:
[tblDetail.COMPANY] = 'CB' AND [tblDetail.Site] = '01' OR [tblDetail.Site]=
'12' AND [tblDetail.Vendor] = '0105' OR [tblDetail.Vendor] = '0052' OR
[tblDetail.Vendor]= '0061'
What I end-up getting with my
DoCmd.OpenForm "frmDetail", acFormDS, , MySel, acFormEdit, acWindowNormal
statement is all records for Company CB - Site 01, and only records in the
selected Vendors for Company CB - Site 12
I know it all those ANDs and ORs in there... but I have no idea how to get
what I want.... which is all the records I would get if each field was subset
sequentially.
Can anyone help? Is it even possible to do this in the manner I am
attempting? Is there a better way?
Best regards, patrick