473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Search form to remove duplicates from union query

70 64KB
I'm not sure if I have a SQL problem or a vba problem so if I've posted in the wrong section, I'm sorry.

I have a search form which uses a union query as its RecordSource. I need to use a union query because I need to search through both Building Facility Managers and points of contact for rooms. I need to be able to search by Last Name, First Name, Organization, Building Name, Room Name, etc.

The way I've set the search form up, is I have search boxes which are all combo boxes. The user selects one or more combo box value and then hits search. At which point the union query is filtered. I have two text boxes which are bound to BuildingFK and RoomsFK. So whatever the user searches by, they should see in those two text boxes.

The problem I'm having is that those two text boxes sometimes contains duplicate data. For instance, suppose I search for BuildingName "A" RoomName "1100". Because there are three different people who are listed as the POC for that room and/or the facility manager for building "A" I see three records all of BuildingID 1, RoomsID 10. As you can see in the attachment, record 1 of 3.

I don't care if there are duplicates in the union query (and in turn the record source for the form). I just need to only see a unique building ID and a unique room ID in txtBuildingID and txtRoomsID. Its fine if there are more than 1 record, as long as the values in both text boxes are unique. For an example, you could search for LastName "Smith" and see that he owns BuildingID 1, RoomsID 10, 11, and 12.

Which brings out an important point. The combination of txtBuildingID and txtRoomsID must be unique. But there can be multiple records of txtBuildingID by itself. This is because as I said, a search of "Smith" could return Building ID 1, Room ID 10, Building ID 1, Room ID 11, building ID 1, Room ID 12, etc.

How can I do that?

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2. Me.RecordSource = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
  3. & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _
  4. & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _
  5. & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _
  6. & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _
  7. & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
  8. & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _
  9. & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _
  10. & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK"
  11. Dim strWhere As String
  12. Dim lngLen As Long
  13.      If Not IsNull(Me.cboSearchLastName) Then
  14.           strWhere = strWhere & "[LastName] ='" & Me.cboSearchLastName & "' AND "
  15.              End If
  16.              If Not IsNull(Me.cboSearchFirstName) Then
  17.           strWhere = strWhere & "[FirstName] ='" & Me.cboSearchFirstName & "' AND "
  18.              End If
  19.              If Not IsNull(Me.cboSearchOrganization) Then
  20.           strWhere = strWhere & "[OrganizationFK] =" & Me.cboSearchOrganization & " AND "
  21.              End If
  22.              If Not IsNull(Me.cboSearchShopName) Then
  23.           strWhere = strWhere & "[ShopNameFK] =" & Me.cboSearchShopName & " AND "
  24.              End If
  25.              If Not IsNull(Me.cboSearchOfficeSym) Then
  26.           strWhere = strWhere & "[OfficeSymFK] =" & Me.cboSearchOfficeSym & " AND "
  27.              End If
  28.              If Not IsNull(Me.cboSearchBuildingName) Then
  29.           strWhere = strWhere & "[BuildingFK] =" & Me.cboSearchBuildingName & " AND "
  30.              End If
  31.              If Not IsNull(Me.cboSearchRoomName) Then
  32.           strWhere = strWhere & "[RoomsPK] =" & Me.cboSearchRoomName & " AND "
  33.              End If
  34.         Call MsgBox(strWhere, vbOKOnly, "Debug")
  35.     lngLen = Len(strWhere) - 5
  36.     If lngLen <= 0 Then
  37.         MsgBox "No criteria", vbInformation, "Nothing to do."
  38.     Else
  39.         strWhere = Left$(strWhere, lngLen)
  40.         Call MsgBox(strWhere, vbOKOnly, "Debug")
  41.       '  MsgBox "No Records Found."
  42.       Dim Msg As VbMsgBoxResult
  43.  
  44. 'If DCount("*", "strWhere") = 0 Then
  45. ' Msg = MsgBox("No corresponding records to your search criteria." & vbCrLf & vbCrLf & _
  46.  '   "Do you want to try again?", vbCritical + vbYesNo)
  47.  ' If Msg = vbYes Then
  48.   '  'Code to open a form
  49. '  Else
  50. '    'Code to open other form
  51. '  End If
  52. Me.Filter = strWhere
  53. Me.FilterOn = True
  54. End If
  55. End Sub
  56. Private Sub txtBuildingID_AfterUpdate()
  57. Me.lstFacilityMgr.Requery
  58. End Sub
  59. Private Sub txtRoomsID_AfterUpdate()
  60. Me.lstRoomsPOC.Requery
  61. End Sub
  62.  
  63. 'Private Sub Form_Load()
  64. 'set the controls to empty on load
  65. 'Me.cboSearchBuildingName = ""
  66. 'Me.cboSearchRoomName = ""
  67. 'Me.cboSearchLastName = ""
  68. 'End Sub

Attached Images
File Type: jpg duplicates.jpg (32.3 KB, 243 views)
File Type: jpg relationship2.0.jpg (68.6 KB, 223 views)
Oct 7 '15 #1
7 1558
ittechguy
70 64KB
I'm guessing I could use DCOUNT to remove duplicates, somehow. I would need to specify what I consider a duplicate, which would be txtBuildingID and txtRoomsId combined.
Oct 7 '15 #2
ittechguy
70 64KB
Or can this be done w/ SQL?
Oct 9 '15 #3
ittechguy
70 64KB
I'm not sure if it matters, but I've changed my code as I was having problems before.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit  'always set this  It will point out errors with field/vaiable names
  3.  
  4.  
  5. Private Sub cboSearchLastName_AfterUpdate()
  6.     Me.cboSearchFirstName.Requery
  7. End Sub
  8.  
  9. Private Sub cboSearchOrganization_AfterUpdate()
  10.     Me.cboSearchShopName.Requery
  11. End Sub
  12.  
  13. Private Sub cboSearchShopName_AfterUpdate()
  14.     Me.cboSearchOfficeSym.Requery
  15. End Sub
  16.  
  17. Private Sub cmdReset_Click()
  18.     Me.cboSearchBuildingName = ""
  19.     Me.cboSearchRoomName = ""
  20.     Me.cboSearchOrganization = ""
  21.     Me.cboSearchShopName = ""
  22.     Me.cboSearchOfficeSym = ""
  23.     Me.cboSearchLastName = ""
  24.     Me.cboSearchFirstName = ""
  25.     Me.FilterOn = False
  26. End Sub
  27. Private Sub Form_Current()
  28.     Me.lstFacilityMgr.Requery
  29.     Me.lstRoomsPOC.Requery
  30. End Sub
  31.  
  32. Private Sub cmdSearch_Click()
  33.     Dim strWhere As String
  34.     Dim lngLen As Long
  35.     Dim startStr As String
  36.     If Not IsNullOrEmpty(Me.cboSearchLastName) Then
  37.         startStr = IIf(strWhere = "", "", " AND ")
  38.         strWhere = strWhere & startStr & "[LastName] ='" & Me.cboSearchLastName & "'"
  39.     End If
  40.     If Not IsNullOrEmpty(Me.cboSearchFirstName) Then
  41.         startStr = IIf(strWhere = "", "", " AND ")
  42.         strWhere = strWhere & startStr & "[FirstName] ='" & Me.cboSearchFirstName & "'"
  43.     End If
  44.     If Not IsNullOrEmpty(Me.cboSearchOrganization) Then
  45.         startStr = IIf(strWhere = "", "", " AND ")
  46.         strWhere = strWhere & startStr & "[OrganizationFK] =" & Me.cboSearchOrganization
  47.     End If
  48.     If Not IsNullOrEmpty(Me.cboSearchShopName) Then
  49.         startStr = IIf(strWhere = "", "", " AND ")
  50.         strWhere = strWhere & startStr & "[ShopNameFK] =" & Me.cboSearchShopName
  51.     End If
  52.     If Not IsNullOrEmpty(Me.cboSearchOfficeSym) Then
  53.         startStr = IIf(strWhere = "", "", " AND ")
  54.         strWhere = strWhere & startStr & "[OfficeSymFK] =" & Me.cboSearchOfficeSym
  55.     End If
  56.     If Not IsNullOrEmpty(Me.cboSearchBuildingName) Then
  57.         startStr = IIf(strWhere = "", "", " AND ")
  58.         strWhere = strWhere & startStr & "[BuildingFK] =" & Me.cboSearchBuildingName
  59.     End If
  60.     If Not IsNullOrEmpty(Me.cboSearchRoomName) Then
  61.         startStr = IIf(strWhere = "", "", " AND ")
  62.         strWhere = strWhere & startStr & "[RoomsPK] =" & Me.cboSearchRoomName
  63.     End If
  64.     If Not IsNullOrEmpty(Me.cboSearchEquipmentName) Then
  65.         startStr = IIf(strWhere = "", "", " AND ")
  66.         strWhere = strWhere & startStr & "[EquipmentNameFK] =" & Me.cboSearchEquipmentName
  67.     End If
  68.     If Not IsNullOrEmpty(Me.cboSearchEquipmentSerialNum) Then
  69.         startStr = IIf(strWhere = "", "", " AND ")
  70.         strWhere = strWhere & startStr & "[SerialNum] ='" & Me.cboSearchEquipmentSerialNum & "'"
  71.     End If
  72.         If Not IsNullOrEmpty(Me.cboSearchEquipmentIP) Then
  73.         startStr = IIf(strWhere = "", "", " AND ")
  74.         strWhere = strWhere & startStr & "[EquipmentIP] ='" & Me.cboSearchEquipmentIP & "'"
  75.     End If
  76.     Call MsgBox(strWhere, vbOKOnly, "Debug")
  77.     lngLen = Len(strWhere) - 5
  78.     If lngLen <= 0 Then
  79.         MsgBox "No criteria", vbInformation, "Nothing to do."
  80.     Else
  81.         'strWhere = Left$(strWhere, lngLen)
  82.         MsgBox strWhere
  83.          If DCount("*", "qryRecordSet", strWhere) = 0 Then
  84.                 MsgBox "No corresponding records to your search criteria." & vbCrLf & vbCrLf
  85.                 Me.FilterOn = False
  86.                 Me.cboSearchBuildingName = ""
  87.                 Me.cboSearchRoomName = ""
  88.                 Me.cboSearchOrganization = ""
  89.                 Me.cboSearchShopName = ""
  90.                 Me.cboSearchOfficeSym = ""
  91.                 Me.cboSearchLastName = ""
  92.                 Me.cboSearchFirstName = ""
  93.          Else
  94.             Me.Filter = strWhere
  95.             Me.FilterOn = True
  96.         End If
  97.     End If
  98. End Sub
  99.  
  100.  
  101.  
  102. Function IsNullOrEmpty(val As Variant) As Boolean
  103.    'First conditional validates for Nothing
  104.    'Second condition validates for an Empty String situation "" or "     "
  105.    Dim ret As Boolean: ret = False
  106.    If IsMissing(val) Then
  107.       ret = True
  108.    ElseIf (val Is Nothing) Then
  109.       ret = True
  110.    ElseIf (val & vbNullString = vbNullString) Then
  111.       ret = True
  112.    ElseIf (Len(Trim(val)) <= 0) Then
  113.       ret = True
  114.    End If
  115.  
  116.    IsNullOrEmpty = ret
  117. End Function
  118.  
  119.  
Oct 9 '15 #4
ittechguy
70 64KB
I've spent way too much time researching this.

Best I figure, I need to further filter results AFTER the recordsource has been filtered by the search boxes. Seems this is either done w/ DCount or with RecordCount. The important thing is that the combination of txtBuildingID and txtRoomsID is unique. Or tblFacilityMgr.BuildingFK and tblRooms.RoomsPK, whichever is easier.

I don't think recordcount can look in two fields. So I think I need DCount. Seems if my life depended on it, I can't get the syntax right.
Oct 12 '15 #5
ittechguy
70 64KB
I'm making progress. Below is the DCount code which counts the records of txtBuildingID and txtRoomsID. I've tested it and confirmed it works.

Expand|Select|Wrap|Line Numbers
  1. If DCount("*", "qryRecordSet", "BuildingFK = " & Me.txtBuildingID & " And RoomsPK = " & Me.txtRoomsID > 1) Then
  2.                   MsgBox "There are duplicates!" & vbCrLf & vbCrLf
  3.                   'Code to remove duplicate records here
  4.                   End If
Now, I need to remove the records which are duplicates. Not from the union query, but from the form recordsource. How do I do that?

It has to have something to do with my filter variable, strWhere. An idea I had was to remove one record from strWhere until there are no more duplicates. Not sure if thats even possible.
Oct 13 '15 #6
NeoPa
32,556 Expert Mod 16PB
FYI:
I'm not ignoring this thread because I don't like you. I've looked at it, but frankly it's far too involved and complex a question to post on a forum. I would be surprised if this were not true of many other experts.

It's your responsibility (albeit an onerous one sometimes as simplifying complicated problems is often very tricky) to present something which is straightforward enough to work on without taking hours out of our already busy days. It's also true that you need to provide enough detail to work with and getting a balance isn't easy, so I'm not criticising. Nevertheless, in the real world we need to have the time in order to help you, and if a question takes this long just to grasp what it is you're asking for, many (most) will avoid it.
Oct 16 '15 #7
hvsummer
215 128KB
It not like I want to avoid like everyone usually do, but the fact, too complex situation that make me can't understand any piece of your picture ==
if you need help, breakdown it into basic, simple model, and we find the way to solve it, then you can apply that model into your complex DB. I alway do this when I ask everyone here in this forum.
hope you could find the way soon. sometime we can only rely on ourself if everything become uncontrolable (mean you can't simply the problem) situation.
Oct 16 '15 #8

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

Similar topics

2
by: misschristalee | last post by:
I'm having a brain blockage day.... Scenario: Search Form with 6 text boxes Query has same six fields Each has this IIF: IIf(IsNull(!!),"",!!) with each dictating the correct text box of...
2
by: mattytee123 | last post by:
I have about 20 tables, of which I would like to do a union query and count of how many of each different code there is? The simplified verson of the table is structured like this. Code ...
3
by: Sim Zacks | last post by:
I am using 8.0 beta 1 on an RH 8 Linux server. I have a union query that I am converting from access (where it worked) and it is returning duplicates. The only difference between the two rows is...
16
by: tyrfboard | last post by:
I've been searching for awhile now on how to remove duplicates from a table within an Access db and have found plenty of articles on finding or deleting duplicates. All I want to do is remove them...
3
by: NoodNutt | last post by:
G'day ppl. Can anyone assist me with the correct structure of the following in a Union Query. tblBookings.FinYear tblBookings.DepPrefPay tblBookings.IntPrefPay tblBookingsFinPrefPay
4
by: spam | last post by:
If I run the following query in Access 2002 then I get the expected result: SELECT * FROM CSVImport UNION SELECT * FROM AssetTemp; I get the contents of both tables with no duplicates. If I...
5
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed...
6
by: jmarcrum | last post by:
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...
1
greeni91
by: greeni91 | last post by:
Hi, I am currently trying to create a report that list all the Features that have been checked and the metrology used to check said feature. I have created a UNION query that will combine the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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
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
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...

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.