473,508 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subform filtering

33 New Member
I'm getting no error messages here, but I'm not getting any results visible when the filter is applied. There are 2 unbound combo boxes in the main form, SetStudy and SetCDB. The only possible values for the column [ROLE] are the 9 reflected in the code below. (The form and table for [ROLE] have the Row Source: "CEC Chair";"CEC Co-Chair";"CEC Member";"DSMB Chair";"DSMB Co-Chair";"DSMB Member";"CEC/DSMB Chair";"CEC/DSMB Co-Chair";"CEC/DSMB Member")

Expand|Select|Wrap|Line Numbers
  1. Private Sub SetCDB_AfterUpdate()
  2. If Me.SetCDB.Selected(0) Then
  3.     Me.SubR.Form.Filter = "[ROLE] = " & Chr(34) & "CEC Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "CEC Co-Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "CEC Member" & Chr(34) & " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  4. ElseIf Me.SetCDB.Selected(1) Then
  5.     Me.SubR.Form.Filter = "[ROLE] = " & Chr(34) & "DSMB Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "DSMB Co-Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "DSMB Member" & Chr(34) & " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  6. ElseIf Me.SetCDB.Selected(2) Then
  7.     Me.SubR.Form.Filter = "[ROLE] = " & Chr(34) & "CEC/DSMB Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "CEC/DSMB Co-Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "CEC/DSMB Member" & Chr(34) & " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  8. Else
  9.     Me.SubR.Form.Filter = "[ROLE ID] = 0"
  10. End If
  11. Me.SubR.Form.FilterOn = True
  12. SubR.Requery
  13. End Sub
Any ideas what's going wrong with the filtering?
Dec 29 '15 #1
12 1138
zmbd
5,501 Recognized Expert Moderator Expert
line 12: Me!SubR.Form.Refresh

you shouldn't need a new query if all you are doing is applying a filter to the form's current record-set; however,

line 12: Me!SubR.Form.Requery
Dec 29 '15 #2
abcrf
33 New Member
Thanks, Z, but that wasn't it. It's performing the same with both syntaxes.
Dec 29 '15 #3
zmbd
5,501 Recognized Expert Moderator Expert
The most common issue is a malformed string which is why I always build my strings first and then set to the form or the called function.

Give me a moment to double check things....
Dec 29 '15 #4
zmbd
5,501 Recognized Expert Moderator Expert
Ok, a little re-arrangement on the code

Expand|Select|Wrap|Line Numbers
  1. Private Sub SetCDB_AfterUpdate()
  2.     Dim zSQL As String
  3.     Dim zSelected as Integer
  4. '
  5.     zSelected = Me.setcdb.value
  6. '
  7.     Select Case zSelected
  8.         Case 1
  9.             zSQL = "[ROLE] = " & Chr(34) & "CEC Chair" & Chr(34) & _
  10.                 " Or [ROLE] = " & Chr(34) & "CEC Co-Chair" & Chr(34) & _
  11.                 " Or [ROLE] = " & Chr(34) & "CEC Member" & Chr(34) & _
  12.                 " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  13.         Case 2
  14.             zSQL = "[ROLE] = " & Chr(34) & "DSMB Chair" & Chr(34) & _
  15.                 " Or [ROLE] = " & Chr(34) & "DSMB Co-Chair" & Chr(34) & _
  16.                 " Or [ROLE] = " & Chr(34) & "DSMB Member" & Chr(34) & _
  17.                 " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  18.         Case 3
  19.             zSQL = "[ROLE] = " & Chr(34) & "CEC/DSMB Chair" & Chr(34) & _
  20.                 " Or [ROLE] = " & Chr(34) & "CEC/DSMB Co-Chair" & Chr(34) & _
  21.                 " Or [ROLE] = " & Chr(34) & "CEC/DSMB Member" & _
  22.                 Chr(34) & " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  23.          Case Else
  24.             zSQL = "[ROLE ID] = 0"
  25.     End Select
  26. '
  27. Debug.Print zSQL
  28. '
  29.     Me!SubR.Form.Filter = zSQL
  30. '
  31.     Me!SubR.Form.FilterOn = True
  32. '
  33.     Me!SubR.Form.Refresh
  34. '    Me!SubR.Requery
  35. End Sub
Open the VBE, <CTRL><G> to open the immediate panel

Run your form

Check the immediate panel for properly formatted string for the filter

((another option along the line of the OP code))

Expand|Select|Wrap|Line Numbers
  1. Private Sub SetCDB_AfterUpdate()
  2.     Dim zSQL As String
  3. '
  4.     Select Case  Me.setcdb.ListIndex
  5.         Case 0
  6.             zSQL = "[ROLE] = " & Chr(34) & "CEC Chair" & Chr(34) & _
  7.                 " Or [ROLE] = " & Chr(34) & "CEC Co-Chair" & Chr(34) & _
  8.                 " Or [ROLE] = " & Chr(34) & "CEC Member" & Chr(34) & _
  9.                 " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  10.         Case 1
  11.             zSQL = "[ROLE] = " & Chr(34) & "DSMB Chair" & Chr(34) & _
  12.                 " Or [ROLE] = " & Chr(34) & "DSMB Co-Chair" & Chr(34) & _
  13.                 " Or [ROLE] = " & Chr(34) & "DSMB Member" & Chr(34) & _
  14.                 " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  15.         Case 2
  16.             zSQL = "[ROLE] = " & Chr(34) & "CEC/DSMB Chair" & Chr(34) & _
  17.                 " Or [ROLE] = " & Chr(34) & "CEC/DSMB Co-Chair" & Chr(34) & _
  18.                 " Or [ROLE] = " & Chr(34) & "CEC/DSMB Member" & _
  19.                 Chr(34) & " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  20.          Case Else
  21.             zSQL = "[ROLE ID] = 0"
  22.     End Select
  23. '
  24. Debug.Print zSQL
  25. '
  26.     Me!SubR.Form.Filter = zSQL
  27. '
  28.     Me!SubR.Form.FilterOn = True
  29. '
  30.     Me!SubR.Form.Refresh
  31. '    Me!SubR.Requery
  32. End Sub
Dec 29 '15 #5
abcrf
33 New Member
I'm getting an "argument not optional" error for .Selected using the "Case" structure above, but I'll try using that string technique with my original code.
Dec 29 '15 #6
abcrf
33 New Member
Interesting result, though it fits with the initial problem.

Expand|Select|Wrap|Line Numbers
  1. Private Sub SetCDB_AfterUpdate()
  2. Dim zSQL As String
  3. Forms!FRM_ROLES!SubR!SubStudy.DefaultValue = Chr(34) & Me.SetStudy.Value & Chr(34)
  4. If Me.SetCDB.Selected(0) Then
  5.     zSQL = "[ROLE] = " & Chr(34) & "CEC Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "CEC Co-Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "CEC Member" & Chr(34) & " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  6. ElseIf Me.SetCDB.Selected(1) Then
  7.     zSQL = "[ROLE] = " & Chr(34) & "DSMB Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "DSMB Co-Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "DSMB Member" & Chr(34) & " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  8. ElseIf Me.SetCDB.Selected(2) Then
  9.     zSQL = "[ROLE] = " & Chr(34) & "CEC/DSMB Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "CEC/DSMB Co-Chair" & Chr(34) & " Or [ROLE] = " & Chr(34) & "CEC/DSMB Member" & Chr(34) & " And [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
  10. Else
  11.     zSQL = "[ROLE ID] = 0"
  12. End If
  13. Debug.Print zSQL
  14. Me!SubR.Form.Filter = zSQL
  15. Me!SubR.Form.FilterOn = True
  16. Me!SubR.Form.Refresh
  17. End Sub
Gives the response:
Expand|Select|Wrap|Line Numbers
  1. [ROLE ID] = 0
Dec 29 '15 #7
zmbd
5,501 Recognized Expert Moderator Expert
I was afraid of that... I've not used the selected property like that before... I'll revise the post

The selected property is usually used to set a selection in a list box.

I've re-written the code to use the value of the cbox instead; however, this is assuming that the cbox has the bound column set so that the first selection returns 1, the second 2, etc...
Dec 29 '15 #8
abcrf
33 New Member
Turns out I was using the wrong syntax for .Selected; it needs to be in the format Me.SetCDB.Selected(0) = True.

But with it corrected in the code above, I get the same string printed, so maybe that's not the location of the error?
Dec 29 '15 #9
zmbd
5,501 Recognized Expert Moderator Expert
The selected property isn't used to retrieve the row selected, it is used to set the selected row.
(This will not however, set the value of the cbox)

Please see the revised code in Post#5

Two options
First Option
The revision now will return the value of the bound column for the cbox in Line 5 and the compare against the select...case starting on line 7.

Of course, you may have to modify the Case comparisons based upon what the actual return is for the cbox's bound column.

Second Option:
Just thought of this: We could potentially us the listindex property... and this more closely matches your original logic. Not really how I would approach the use of a combobox...

you got me "out of the box" on this one ;-)
Dec 29 '15 #10
abcrf
33 New Member
The listindex approach works, thanks! Of course, I also needed to change the syntax of the filter to include parentheses as per below.

Expand|Select|Wrap|Line Numbers
  1. zSQL = "([ROLE] = " & Chr(34) & "CEC Chair" & Chr(34) & _
  2.             " OR [ROLE] = " & Chr(34) & "CEC Co-Chair" & Chr(34) & _
  3.             " OR [ROLE] = " & Chr(34) & "CEC Member" & Chr(34) & _
  4.             ") AND [STUDY NAME] = " & Chr(34) & Me.SetStudy.Value & Chr(34)
Dec 29 '15 #11
zmbd
5,501 Recognized Expert Moderator Expert
good deal, looks like we solved two potential snags here :)

Like I said earlier, using the cbo.ListIndex isn't the most common method employed with this control, I'd actually forgotten that it was available. The most common usage I've ran across is to help the end-user select the appropriate value from a related table for the record at hand by showing the human-friendly text in the list/box while actually setting the related foreign/primary-key based on the row-source "behind the scenes."
Dec 29 '15 #12
NeoPa
32,557 Recognized Expert Moderator MVP
Hi there. This isn't directly related to the question but simply some advice about putting together strings which contain quotes in them. The existing solution is all good.

However, using quotes needn't be so complicated. Chr() should never be required, even when using quotes that aren't the most appropriate (See Quotes (') and Double-Quotes (") - Where and When to use them). As we're looking at strings for SQL here (Whether a full SQL string or just a filter in SQL format is immaterial.) we would probably prefer to use the quote character ('). This can be written out simply within a VBA string that uses double-quotes (") without any need for Chr().

Whenever we need to use quote characters of the same style as the string they're enclosed within though, we can simply double them up to be interpreted properly. This is a coding convention that goes back many decades and is supported consistently across all languages I've ever used that I recall (Some I last used many decades ago myself so may not recall all such details).

Thus, using option A can be always be replaced by option B :
Expand|Select|Wrap|Line Numbers
  1. strName = "Fairholme"
  2.  
  3. A.  strExample = "Their house was named " & Chr(34) & strName & Chr(34) & "."
  4. B.  strExample = "Their house was named """ & strName & """."
My personal preference, for easiest readability and interpretation, would be to use Replace() as in :
Expand|Select|Wrap|Line Numbers
  1. C.  strExample = Replace("Their house was named ""%N"".", "%N", strName)
With very little imagination or understanding you can see what it is the code is trying to achieve. It does have a function call in the code, but not within the text you're trying to formulate. There it simply has a replacement token which is easy to process visually for easy understanding.
Dec 30 '15 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
11086
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
19
3509
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
0
1919
by: johnson_cy | last post by:
I am using Access 2000. My main form has a record source of a table (employeeTbl with key of . It is filtering the record results. My subform and mainform have the link child/link master set...
1
4922
by: aintnorock | last post by:
I have a relatively simple relational database. One of my forms is based on a query with a 3 parameter sort: Status / Priority / Customer. When I open the form, the records are sorted properly. ...
1
1598
by: crazytegger | last post by:
I am trying to filter a subforms combolist options through a combolist on the main form. Heres what i have: Form: frmOrderDetail Datasource: tblOrders cmbOrderSupplierID w/ control source...
3
1568
by: BSTAFFORD | last post by:
I was researching some vba code on TheScripts.com and found this post. I’m looking to accomplish the same idea but I need to filter a sub form query with 3 combo boxes from the main form. This is...
0
1669
by: diogenes | last post by:
"Rick Brandt" <rickbrandt2@hotmail.comwrote in news:bPnKj.456$%41.325 @nlpi064.nbdc.sbc.com: I used this approach, and it works a treat! ID In(SELECT Order_ID FROM orderitems WHERE NAME =...
5
7079
by: jbrumbau | last post by:
Hello, I have some specification sheets with subforms I have created. I'm having a problem where if you right click and filter/sort any field in the subform (which is tied to the main form in a...
6
1464
by: lovelydan | last post by:
I have designed a form(main form) which has a record source that runs subforms. this works perfectly. I also have a macro that is attached to this subform which filters records on a different form....
4
4584
zmbd
by: zmbd | last post by:
Ok, So I've created forms with both filtering as in http://bytes.com/topic/access/insights/590551-example-filtering-form, same concept with cascading comboboxs, and I have used Main/Subform formats...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.