Hi All,
I'm a newbie in terms of Access and some of its functionality . I've been stuck on this problem for a couple days, even after searching the Web, etc.
Currently I have five combo boxes (actually list boxes) that are multiselects in my main form. I need to use these combo boxes to filter a subform within my main form.
My combo boxes are as follows:
1. A - 4 select options
2. B - 10 select options
3. C - 4 select options
4. D - 3 select options
5. E - 4 select options
My subform is a listing of all the possible combinations of these select options (therefore there are 4 x 10 x 4 x 3 x 4 records).
I am having trouble understanding how to link these unbound list boxes to the subform to make the filter work once a user has selected their options from the 5 combo boxes.
Case example:
If I select options 1, 2, and 3 from list box A, and option 2 from listbox B, C, D, and E; I would want the subform to filter based on this query:
SELECT * FROM TABLE WHERE
(A_id = 1 OR A_id = 2 OR A_id = 3) AND
B_id = 2 AND
C_id = 2 AND
D_id = 2 AND
E_id = 2;
I hope this makes sense. Can anyone please help me out on this one. I'd be indebted to you! I really appreciate it
Any help on which functions or what code to put and where to put it would be great!
Thanks,
Nate
9 15836 MMcCarthy 14,534
Recognized Expert Moderator MVP
Currently I have five combo boxes (actually list boxes) that are multiselects in my main form. I need to use these combo boxes to filter a subform within my main form.
Always refer to them as listboxes as the functionality is different and you will confuse the experts.
listboxes are as follows:
1. A - 4 select options
2. B - 10 select options
3. C - 4 select options
4. D - 3 select options
5. E - 4 select options
My subform is a listing of all the possible combinations of these select options (therefore there are 4 x 10 x 4 x 3 x 4 records).
I am having trouble understanding how to link these unbound list boxes to the subform to make the filter work once a user has selected their options from the 5 combo boxes.
Case example:
If I select options 1, 2, and 3 from list box A, and option 2 from listbox B, C, D, and E; I would want the subform to filter based on this query:
SELECT * FROM TABLE WHERE
(A_id = 1 OR A_id = 2 OR A_id = 3) AND
B_id = 2 AND
C_id = 2 AND
D_id = 2 AND
E_id = 2;
OK
I would create a command button (cmdFindRecords ) and put this code behind it. -
-
Private Sub cmdFindRecords_Click()
-
Dim strWhere As String
-
Dim iSelected As Variant
-
-
strWhere = "(("
-
For Each iSelected In Me.ListBoxA_Name.ItemsSelected
-
strWhere = strWhere & "[A_id]=" & Me.ListBoxA_Name.ItemData(iSelected) & " OR "
-
Next iSelected
-
-
' remove last OR, close the bracket and add AND with open bracket
-
strWhere = Left(strWhere, Len(strWhere) - 4) & ") AND ("
-
-
For Each iSelected In Me.ListBoxB_Name.ItemsSelected
-
strWhere = strWhere & "[B_id]=" & Me.ListBoxB_Name.ItemData(iSelected) & " OR "
-
Next iSelected
-
-
' remove last OR, close the bracket and add AND with open bracket
-
strWhere = Left(strWhere, Len(strWhere) - 4) & ") AND ("
-
-
For Each iSelected In Me.ListBoxC_Name.ItemsSelected
-
strWhere = strWhere & "[C_id]=" & Me.ListBoxC_Name.ItemData(iSelected) & " OR "
-
Next iSelected
-
-
' remove last OR, close the bracket and add AND with open bracket
-
strWhere = Left(strWhere, Len(strWhere) - 4) & ") AND ("
-
-
For Each iSelected In Me.ListBoxD_Name.ItemsSelected
-
strWhere = strWhere & "[D_id]=" & Me.ListBoxD_Name.ItemData(iSelected) & " OR "
-
Next iSelected
-
-
' remove last OR, close the bracket and add AND with open bracket
-
strWhere = Left(strWhere, Len(strWhere) - 4) & ") AND ("
-
-
For Each iSelected In Me.ListBoxE_Name.ItemsSelected
-
strWhere = strWhere & "[E_id]=" & Me.ListBoxE_Name.ItemData(iSelected) & " OR "
-
Next iSelected
-
-
' remove last OR and close the both brackets
-
strWhere = Left(strWhere, Len(strWhere) - 4) & "))"
-
-
Forms![MainFormName]![SubFormName].Form.RecordSource = "SELECT * FROM TableName WHERE " & strWhere & ";"
-
Me.SubFormName.Requery
-
-
End Sub
-
-
NeoPa 32,568
Recognized Expert Moderator MVP
I would do something very like that, except I feel that the IN() SQL function would work perfectly for ListBoxes, rather than the more complicated [X]=A OR [X]=B etc.
Although not usually used for a single itemed list, IN() will work correctly in that scenario too.
ComboBoxes would be different, but IN() is better for MULTISELECT ListBoxes.
BTW Congratulations on a clearly stated question.
MMcCarthy 14,534
Recognized Expert Moderator MVP
I would do something very like that, except I feel that the IN() SQL function would work perfectly for ListBoxes, rather than the more complicated [X]=A OR [X]=B etc.
Although not usually used for a single itemed list, IN() will work correctly in that scenario too.
ComboBoxes would be different, but IN() is better for MULTISELECT ListBoxes.
BTW Congratulations on a clearly stated question.
Good suggestion Adrian
Never thought of using IN
Mary
NeoPa 32,568
Recognized Expert Moderator MVP
That's funny Mary.
I posted that a while back, and just in the last minute, I came across some code you posted for something similar which used a loop to create the comparison string - and IN() aswell.
The code may prove useful here too ( Passing Multi-Select field parameters to a query).
MMcCarthy 14,534
Recognized Expert Moderator MVP
That's funny Mary.
I posted that a while back, and just in the last minute, I came across some code you posted for something similar which used a loop to create the comparison string - and IN() aswell.
The code may prove useful here too (Passing Multi-Select field parameters to a query).
I can't remember what I did 5 minutes ago and and you want me to remember how I answered another question. AHH!!!
Mary
Thanks so much everyone! It helped a lot. I really appreciate all your help and advice. I tweaked it a little, but the basic gist really helped! Thanks a bunch.
NeoPa 32,568
Recognized Expert Moderator MVP
It's always nice to have someone come back and let us know they've been helped.
Everybody likes to feel appreciated and, more importantly perhaps, we can consider a thread finished.
So thank you.
I've used the above code and tweeked it a little to allow the user to pick some list boxes to filter, but not others. (code posted below)
I get - 'run time error '5': Invalid procedure call or reference
I've had my head stuck in it for so long now, I'm not sure I could find the problem if it were staring me in the face. Any help would be much appreciated. - Private Sub Command113_Click()
-
Dim strWhere As String
-
Dim iSelected As Variant
-
-
strWhere = "(("
-
-
-
For Each iSelected In Me.aclist.ItemsSelected
-
If Not IsNull(aclist) Then
-
strWhere = strWhere & "[Activity]=" & Me.aclist & ")"
-
End If
-
Next iSelected
-
-
' remove last OR, close the bracket and add AND with open bracket
-
strWhere = Left(strWhere, Len(strWhere) - 1)
-
If Left(strWhere, 1) = Not Null And Me.Combo58 = Not Null Then
-
strWhere = strWhere & ")AND("
-
End If
-
For Each iSelected In Me.Combo58.ItemsSelected
-
If Not IsNull(Combo58) Then
-
strValues = strValues & "[Activity_theme]=" & Me.Combo58 & ")"
-
-
End If
-
Next iSelected
-
-
' remove last OR, close the bracket and add AND with open bracket
-
strWhere = Left(strWhere, Len(strWhere) - 1)
-
-
-
If Left(strWhere, 1) = Not Null And Me.Combo60 = Not Null Then
-
strWhere = strWhere & ")AND("
-
End If
-
For Each iSelected In Me.Combo60.ItemsSelected
-
If Not IsNull(Combo60) Then
-
strWhere = strWhere & "[Activity_group]=" & Me.Combo60 & ")"
-
End If
-
Next iSelected
-
' remove last OR, close the bracket and add AND with open bracket
-
strWhere = Left(strWhere, Len(strWhere) - 1)
-
-
If Right(strWhere, 1) = ")" And Me.Scheme_gp = Not Null Then
-
strWhere = strWhere & ")AND("
-
End If
-
For Each iSelected In Me.Scheme_gp.ItemsSelected
-
If Not IsNull(Scheme_gp) Then
-
strWhere = strWhere & "[Scheme_group]=" & "[Forms]![rep]![Scheme_gp]" & ")"
-
End If
-
Next iSelected
-
-
' remove last OR, close the bracket and add AND with open bracket
-
strWhere = Left(strWhere, Len(strWhere) - 1)
-
If Right(strWhere, 1) = ")" And Me.Combo64 = Not Null Then
-
strWhere = strWhere & ")AND("
-
End If
-
For Each iSelected In Me.Combo64.ItemsSelected
-
If Not IsNull(Combo64) Then
-
strWhere = strWhere & "[CH_theme]=" & "[Forms]![rep]![Combo64]"
-
End If
-
Next iSelected
-
-
-
' remove last OR and close the both brackets
-
strWhere = Left(strWhere, Len(strWhere) - 1) & ")"
-
-
Forms![rep]![Master2].Form.RecordSource = "SELECT * FROM Master WHERE " & strWhere & ";"
-
Me.Master2.Requery
i m greatful to you submitting this code i was wondering before watching this code i got idea from it and convert it to my own needs
thanks again
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 have a primary form named
TestResults, which is connected to data in a table named TestResults.
There are basically two other tables that are related to the
TestResults table (and the primary form) named Names-Normalized and
SiteAddresses. The...
|
by: CSDunn |
last post by:
Hello,
In Access ADP's that connect to SQL Server databases, any time I have
a situation where I have a combo box in a main form that looks up a
record in a subform, the subform record source has to be based on
either a View or a Table. I can almost always use a View, and it helps
to do this since I can have better control over the size of the
RecordSet of the subform.
There are times when the use of a Stored Procedure would give me...
|
by: Nicolae Fieraru |
last post by:
Hi All,
I have a problem and I can't figure out how to solve it.
My database has three tables:
tblCustomers, with CustomerId and CustomerName
tblProducts, with ProductId and ProductCode
tblPurchases, with PurchaseId, CustomerId and ProductId
I created a form with a subform which shows all the customers, one at a time
|
by: MLH |
last post by:
I have a form with a subform control on it listing
records returned by a saved query named UnbilledVehicles.
I would like to put command buttons on the main form
to apply dynamic filters to the records displayed in the
subform control. Say, for instance, to list only 2004 model
cars. I'm seeking the simplest approach.
The main form is frmCreateInvoice, the subform control
is named frmCreateInvoiceSubFormCtl and the actual sub-
form is...
|
by: Ecohouse |
last post by:
I have a main form with two subforms. The first subform has the child
link to the main form identity key.
subform1 - Master Field: SK
Child Field: TrainingMasterSK
The second subform has a master-child link to the first subform.
subform2 - Master Field: subTrainingModule.Form!TrainingModuleTopicSK
Child Field: TrainingModuleTopicSK
| |
by: virtualgreek |
last post by:
Dear All,
First of all I would like to take the time to thank you all for your efforts and time spent at this wonderful forum. I have found it very helpful with numerous examples available for every level of user.
I am facing a rather weird behaviour with a combo box on a subform (Continuous form).
I have two tables. Order and Order_Details. The master form is based on the Orders tables and the details form (subform) is based on the...
|
by: kinglioness |
last post by:
Hello All, I am new to this forum so I apologize if this quesiton was asked already. Although i did search but i couldn't find.
I have 2 combo boxes one unbound and the other bound they are on the main form. One (combo box) filters the other. I would like for both boxes to filter the subforms which by the way are on tab controls I have 8 pages. I am very new to vba, but I am trying to get a code that will filter whatever subform is...
|
by: WyvsEyeView |
last post by:
I am doing the very standard thing of filtering the contents of one combo box based on another combo box. I've done it many times, but always on a main form. Now I'm trying to do it on a datasheet subform and it is not working. After making the selection in the first combo box, when I move to the second combo box, I get the Access input parameters box. If I input the value that is in the first combo box, I get the expected results in the second...
|
by: woodey2002 |
last post by:
Hi Everyone and many thanks for your time.. I am trying to begin access and a bit of VBA i am enjoying it but I have a annoying problem I just can’t get any where on.
My databse mostly includes bits of code for different examples. I have one last thing to finish.
I am trying to create a search form that will allow users to select criteria from multiple sources eg ,multi select list boxes , combo boxes.
I have a subform showing all the...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |