473,756 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Apply a filter to a Union Query

105 New Member
Hi!

I have created a Union Query in ACCESS 2003, that combines 130 records from one query (Extra Foreman Radios) and 250 records from another query (Forman Main Radios). I have created a continuous form and used the Union query (qryRadioDataUn ion) as my record source. My question is...how would i apply a filter to that query record source to allow the user to limit the search results down to an individual contractor, radio type, SIM card capacity, etc.

The problem is....I'm pullin from multiple tables here and I don't know which one to reference. Here's my code for the criteria on the continuous form. This occurs after the user selects their search criteria and then clicks the search button.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2. Dim GCriteria As String
  3.  
  4.     'Check for blank criteria
  5.     If IsNull(chkExtraRadio) And IsNull(cmbRadioType) And IsNull(txtName) And IsNull(txtRadioNum) And IsNull(cmbContractor) And IsNull(cmbSIMCap) Then
  6.         MsgBox "You must enter search criteria..."
  7.         Exit Sub
  8.     End If
  9.  
  10.         'Generate Radio Type search criteria
  11.         If Not IsNull(Me.cmbRadioType) Then
  12.             If GCriteria = "" Then
  13.                 GCriteria = "tblExtraRadios.RadioTypeID = " & Me.cmbRadioType.Value
  14.             Else
  15.                 GCriteria = GCriteria & " AND tblExtraRadios.RadioTypeID = " & Me.txtRadioNum.Value & ""
  16.             End If
  17.         End If
  18.  
  19.         'Generate Name search criteria
  20.         If Not IsNull(txtName) Then
  21.             If GCriteria = "" Then
  22.                 GCriteria = "(((tblExtraRadios.Name) Like " & """" & "*" & Me.txtName.Value & "*" & """" & "))"
  23.             Else
  24.                 GCriteria = GCriteria & " AND tblExtraRadios.Name = '" & Me.txtName.Value & "'"
  25.             End If
  26.         End If
  27.  
  28.         'Generate Radio Number search criteria
  29.         If Not IsNull(txtRadioNum) Then
  30.             If GCriteria = "" Then
  31.                 GCriteria = "(((tblExtraRadios.ExtraRadioNum) Like " & """" & "*" & Me.txtRadioNum.Value & "*" & """" & "))"
  32.             Else
  33.                 GCriteria = GCriteria & " AND tblExtraRadios.ExtraRadioNum = " & Me.txtRadioNum.Value & ""
  34.             End If
  35.         End If
  36.  
  37.         'Generate SIM Capacity search criteria
  38.         If Not IsNull(Me.cmbSIMCap) Then
  39.             GCriteria = "tblExtraRadios.SIMCapacityID = " & Me.cmbSIMCap.Value
  40.         End If
  41.  
  42.         'Generate Contractor search criteria
  43.         If Not IsNull(Me.cmbContractor) Then
  44.             If GCriteria = "" Then
  45.                 GCriteria = "tblCrews.Company = " & Me.cmbContractor.Value
  46.             Else
  47.                 GCriteria = GCriteria & " AND tblCrews.Company = " & Me.cmbContractor.Value & ""
  48.             End If
  49.         End If
  50.  
  51.         'Generate Extra Radio for True search criteria
  52.         If (Me.chkExtraRadio) = True Then
  53.             If GCriteria = "" Then
  54.                 GCriteria = "tblExtraRadios.ExtraRadio = " & Me.chkExtraRadio.Value
  55.             Else
  56.                 GCriteria = GCriteria & " AND tblExtraRadios.ExtraRadio = " & Me.chkExtraRadio.Value & ""
  57.             End If
  58.         End If
  59.  
  60.         'Generate Extra Radio for False search criteria
  61.         If (Me.chkExtraRadio) = False Then
  62.             If GCriteria = "" Then
  63.                 GCriteria = "tblCrew.ExtraRadio = " & Me.chkExtraRadio.Value
  64.             Else
  65.                 GCriteria = GCriteria & " AND tblCrew.ExtraRadio = " & Me.chkExtraRadio.Value & ""
  66.             End If
  67.         End If
  68.  
  69.         'Filter frmRadioDataAll based on search criteria
  70.         Forms!frmRadioData!frmRadioDataAll.Form.RecordSource = "SELECT DISTINCT tblExtraRadios.Name, tblExtraRadios.ExtraRadioNum AS RadioNum, tblContractor.ContractorName, tblExtraRadios.ExtraPhoneNumber AS PhoneNumber, tblRadioType.RadioType, tblSIMCapacity.SIMCapacityType, tblExtraRadios.RadioActivatedOn, tblExtraRadios.RadioChangedOn, tblExtraRadios.RadioDeactivatedOn, tblExtraRadios.Active AS RadioActive, tblExtraRadios.Notes AS RadioNotes, tblExtraRadios.ExtraRadio" & _
  71.                                                                 "FROM tblSIMCapacity RIGHT JOIN (tblRadioType RIGHT JOIN (tblContractor RIGHT JOIN (tblCrews RIGHT JOIN tblExtraRadios ON tblCrews.CrewID=tblExtraRadios.CrewID) ON tblContractor.ContractorID=tblCrews.Company) ON tblRadioType.RadioTypeID=tblExtraRadios.RadioTypeID) ON tblSIMCapacity.SIMCapacityID=tblExtraRadios.SIMCapacityID ORDER BY tblExtraRadios.Name UNION SELECT  tblCrews.ForemanFirstName & ' ' & tblCrews.ForemanLastName AS Name, tblCrews.RadioNum, tblContractor.ContractorName, tblCrews.PhoneNumber, tblRadioType.RadioType, tblSIMCapacity.SIMCapacityType, tblCrews.RadioActivatedOn, tblCrews.RadioChangedOn, tblCrews.RadioDeactivatedOn, tblCrews.RadioActive, tblCrews.RadioNotes, tblCrews.ExtraRadio" & _
  72.                                                                 "FROM tblSIMCapacity RIGHT JOIN (tblRadioType RIGHT JOIN (tblContractor RIGHT JOIN tblCrews ON tblContractor.ContractorID = tblCrews.Company) ON tblRadioType.RadioTypeID = tblCrews.RadioTypeID) ON tblSIMCapacity.SIMCapacityID = tblCrews.SIMCapacityID;"
  73.         Forms!frmRadioData.Caption = "Search Results"
  74. End Sub
Thanks so much!
Jan 30 '08 #1
6 7034
MMcCarthy
14,534 Recognized Expert Moderator MVP
You can't apply a filter on the overall query unless you save it and build another query on it. You would have to set criteria on each query within the union.
Feb 5 '08 #2
jmarcrum
105 New Member
You can't apply a filter on the overall query unless you save it and build another query on it. You would have to set criteria on each query within the union.
OK, so I created another query and saved it as qryRadioDataUni onFINAL. Now can I just filter the fields in that query and it work? Like this?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2. Dim GCriteria As String
  3.  
  4.     'Check for blank criteria
  5.     If IsNull(cmbRadioType) And IsNull(txtName) And IsNull(txtRadioNum) And IsNull(cmbContractor) And IsNull(cmbSIMCap) Then
  6.         MsgBox "You must enter search criteria..."
  7.         Exit Sub
  8.     End If
  9.  
  10.         'Generate Radio Type search criteria
  11.         If Not IsNull(cmbRadioType) Then
  12.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "RadioType = " & Me.cmbRadioType.Value
  13.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  14.         End If
  15.  
  16.         'Generate Name search criteria
  17.         If Not IsNull(txtName) Then
  18.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "Name = " & Me.txtName.Value
  19.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  20.         End If
  21.  
  22.         'Generate Radio Number search criteria
  23.         If Not IsNull(txtRadioNum) Then
  24.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "RadioNum = " & Me.txtRadioNum.Value
  25.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  26.         End If
  27.  
  28.         'Generate SIM Capacity search criteria
  29.         If Not IsNull(cmbSIMCap) Then
  30.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "SIMCapacityType = " & Me.cmbSIMCap.Value
  31.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  32.         End If
  33.  
  34.         'Generate Contractor search criteria
  35.         If Not IsNull(cmbContractor) Then
  36.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "ContractorName = " & Me.cmbContractor.Value
  37.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  38.         End If
  39.  
  40.         'Filter frmRadioDataAll based on search criteria
  41.         Forms!frmRadioData!frmRadioDataAll.Form.RecordSource = qryRadioDataUnion
  42.  
  43.         'Rename the form
  44.         Forms!frmRadioData.Caption = "Search Results"
  45. End Sub
Feb 6 '08 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
OK, so I created another query and saved it as qryRadioDataUni onFINAL. Now can I just filter the fields in that query and it work? Like this?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2. Dim GCriteria As String
  3.  
  4.     'Check for blank criteria
  5.     If IsNull(cmbRadioType) And IsNull(txtName) And IsNull(txtRadioNum) And IsNull(cmbContractor) And IsNull(cmbSIMCap) Then
  6.         MsgBox "You must enter search criteria..."
  7.         Exit Sub
  8.     End If
  9.  
  10.         'Generate Radio Type search criteria
  11.         If Not IsNull(cmbRadioType) Then
  12.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "RadioType = " & Me.cmbRadioType.Value
  13.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  14.         End If
  15.  
  16.         'Generate Name search criteria
  17.         If Not IsNull(txtName) Then
  18.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "Name = " & Me.txtName.Value
  19.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  20.         End If
  21.  
  22.         'Generate Radio Number search criteria
  23.         If Not IsNull(txtRadioNum) Then
  24.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "RadioNum = " & Me.txtRadioNum.Value
  25.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  26.         End If
  27.  
  28.         'Generate SIM Capacity search criteria
  29.         If Not IsNull(cmbSIMCap) Then
  30.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "SIMCapacityType = " & Me.cmbSIMCap.Value
  31.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  32.         End If
  33.  
  34.         'Generate Contractor search criteria
  35.         If Not IsNull(cmbContractor) Then
  36.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = "ContractorName = " & Me.cmbContractor.Value
  37.             Forms!frmRadioData!frmRadioDataAll.Form.Filter = True
  38.         End If
  39.  
  40.         'Filter frmRadioDataAll based on search criteria
  41.         Forms!frmRadioData!frmRadioDataAll.Form.RecordSource = qryRadioDataUnion
  42.  
  43.         'Rename the form
  44.         Forms!frmRadioData.Caption = "Search Results"
  45. End Sub
Have you tried it? I don't know if it will work with your data, you have to try it out and see. I think it will but I can't see your database.
Feb 6 '08 #4
jmarcrum
105 New Member
Have you tried it? I don't know if it will work with your data, you have to try it out and see. I think it will but I can't see your database.
Well, i tried it, but it displays in each text box within the Detail part of the continuous form #Name, #Name, #Name, #Name, #Name, #Name, #Name, ....

I'm not sure why it's doing that.
Feb 6 '08 #5
MMcCarthy
14,534 Recognized Expert Moderator MVP
Well, i tried it, but it displays in each text box within the Detail part of the continuous form #Name, #Name, #Name, #Name, #Name, #Name, #Name, ....

I'm not sure why it's doing that.
So you created a new query qryRadioDataUni onFINAL which essentially is a select query which selects all fields from the union query?
You have set that query as the record source of your form?

If that doesn't work then you just can't filter on a union query. The only other option is to trigger a make table query and create a new table based on the union query results and use that as the record source of the form.
Feb 6 '08 #6
jmarcrum
105 New Member
So you created a new query qryRadioDataUni onFINAL which essentially is a select query which selects all fields from the union query?
You have set that query as the record source of your form?
Yeah that's what I'm finding out. If you want to filter a union query...you gotta do it WITHIN the SQL statement. You can't do it with user input. That other option you suggested might work though. I wish I had time to try, my boss is pressin' time on me.

But what I ended up doing was separating the tables and just running criteria checks on two separate forms. It'll work just as well, may take some more time though...haha.
Feb 6 '08 #7

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

Similar topics

4
1685
by: nick_faye | last post by:
hi guys, hope somebody can assist me. i have two ms access database. i have to copy the entries in database1 to my database2. however, i have to copy entries from database1 that does not exist yet in the entries in my database2. i am using INSERT INTO table_in_database2 SELECT fields_from_table_in_database1 FROM table_in_database1 IN directory_of_database1. i was trying to put a WHERE command but i always get an error. please help.
16
18418
by: Nhmiller | last post by:
I already have a report designed under Reports. When I use filtering in Forms or Tables, I see no way to select that filtered list when I am in Reports, and there appears to be no way to do the filtering when I am in Reports. I have received two replies to this message, posted a few days ago within another Subject. One requires making programming type statements. That should not be necessary -- Access must have a quick way to just click...
3
12502
by: Stewart | last post by:
Hi all! My (relatively small) database holds data on staff members and the projects (services) that they are assigned to. In my form frmStaff, I have a list of staff members - it is a continuous form. Each staff member is linked to a Service through a many-to-many relationship, using a junction table called jctStaffServices. I would like to place a Combo Box in frmStaff where you can 'filter' staff by the Service (i.e. ServiceName)...
2
4031
by: Lenin Torres | last post by:
Hi everybody I have an Union Query that works fine. I used this query as the RecordSource for a Form. That Form is used as a subform in another form. Everything works fine, except for the "Filter by form" feature. When the user tries to use Filter by form a messagebox is displayed: "There are too many controls in this form to perform a filter by form", after that,when the user exit the Filter by Form mode, Access crash, displaying a...
5
3846
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big table, we try separate this big table into twelve tables and create a view
6
1390
by: cwby1966 | last post by:
This code worked great in about 8 other places now i am tryng to use in an existing database and when i run the query it gies me a syntex error and then it makes itself into a Union query andy Idea why? See SQL below SELECT ., ., ., ., .Status, .Service, .Active, .Notes, .UNION , .dob, .doh, .aDDR, .aDDR2, ., ., ., .schhours FROM WHERE (((.Status)=IIf(nz(Forms!!statusfltr,"")="",!Status,Forms!!statusfltr)) And...
1
6056
by: mattscho | last post by:
Re: Filter By From, Apply Filter, Remove Filter Buttons in a Form. -------------------------------------------------------------------------------- Hi All, Trying to create a set of 3 buttons in a form that have the same effect as the "Filter by Form", "Apply Filter" and "Remove Filter" Buttons on the access toolbar. Help would be muchly appreciated. Cheers. In the Click() Event of 3 Command Buttons, place the following code: Code: (...
10
15195
by: dbdb | last post by:
Hi, i create a chart in ms access based on my query, then i want my chart when is it open is only show value based on my criteria. i'll try to used it in the properties apply filter using the expression, it didn't work. the chart still viewing all data. and i used the event "on open" and used "applyfilter" command then it shown an error. "that the report isn't bound with the query"
5
5549
by: phill86 | last post by:
Hi, I have a form that I have applied a filter to by using combo boxes which works fine. Is there a way to apply that filter to the forms underlying query Here is the code that I use to filter the form On Error GoTo Error_EquipFilter
0
9455
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, 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...
0
9271
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,...
0
9869
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9838
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,...
1
7242
isladogs
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...
0
6534
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5140
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...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2665
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 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...

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.