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: - Private Sub cmdSearch_Click()
-
Me.RecordSource = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
-
& " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _
-
& " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _
-
& " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _
-
& " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _
-
& " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
-
& " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _
-
& " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _
-
& " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK"
-
Dim strWhere As String
-
Dim lngLen As Long
-
If Not IsNull(Me.cboSearchLastName) Then
-
strWhere = strWhere & "[LastName] ='" & Me.cboSearchLastName & "' AND "
-
End If
-
If Not IsNull(Me.cboSearchFirstName) Then
-
strWhere = strWhere & "[FirstName] ='" & Me.cboSearchFirstName & "' AND "
-
End If
-
If Not IsNull(Me.cboSearchOrganization) Then
-
strWhere = strWhere & "[OrganizationFK] =" & Me.cboSearchOrganization & " AND "
-
End If
-
If Not IsNull(Me.cboSearchShopName) Then
-
strWhere = strWhere & "[ShopNameFK] =" & Me.cboSearchShopName & " AND "
-
End If
-
If Not IsNull(Me.cboSearchOfficeSym) Then
-
strWhere = strWhere & "[OfficeSymFK] =" & Me.cboSearchOfficeSym & " AND "
-
End If
-
If Not IsNull(Me.cboSearchBuildingName) Then
-
strWhere = strWhere & "[BuildingFK] =" & Me.cboSearchBuildingName & " AND "
-
End If
-
If Not IsNull(Me.cboSearchRoomName) Then
-
strWhere = strWhere & "[RoomsPK] =" & Me.cboSearchRoomName & " AND "
-
End If
-
Call MsgBox(strWhere, vbOKOnly, "Debug")
-
lngLen = Len(strWhere) - 5
-
If lngLen <= 0 Then
-
MsgBox "No criteria", vbInformation, "Nothing to do."
-
Else
-
strWhere = Left$(strWhere, lngLen)
-
Call MsgBox(strWhere, vbOKOnly, "Debug")
-
' MsgBox "No Records Found."
-
Dim Msg As VbMsgBoxResult
-
-
'If DCount("*", "strWhere") = 0 Then
-
' Msg = MsgBox("No corresponding records to your search criteria." & vbCrLf & vbCrLf & _
-
' "Do you want to try again?", vbCritical + vbYesNo)
-
' If Msg = vbYes Then
-
' 'Code to open a form
-
' Else
-
' 'Code to open other form
-
' End If
-
Me.Filter = strWhere
-
Me.FilterOn = True
-
End If
-
End Sub
-
Private Sub txtBuildingID_AfterUpdate()
-
Me.lstFacilityMgr.Requery
-
End Sub
-
Private Sub txtRoomsID_AfterUpdate()
-
Me.lstRoomsPOC.Requery
-
End Sub
-
-
'Private Sub Form_Load()
-
'set the controls to empty on load
-
'Me.cboSearchBuildingName = ""
-
'Me.cboSearchRoomName = ""
-
'Me.cboSearchLastName = ""
-
'End Sub
7 1558
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.
Or can this be done w/ SQL?
I'm not sure if it matters, but I've changed my code as I was having problems before. - Option Compare Database
-
Option Explicit 'always set this It will point out errors with field/vaiable names
-
-
-
Private Sub cboSearchLastName_AfterUpdate()
-
Me.cboSearchFirstName.Requery
-
End Sub
-
-
Private Sub cboSearchOrganization_AfterUpdate()
-
Me.cboSearchShopName.Requery
-
End Sub
-
-
Private Sub cboSearchShopName_AfterUpdate()
-
Me.cboSearchOfficeSym.Requery
-
End Sub
-
-
Private Sub cmdReset_Click()
-
Me.cboSearchBuildingName = ""
-
Me.cboSearchRoomName = ""
-
Me.cboSearchOrganization = ""
-
Me.cboSearchShopName = ""
-
Me.cboSearchOfficeSym = ""
-
Me.cboSearchLastName = ""
-
Me.cboSearchFirstName = ""
-
Me.FilterOn = False
-
End Sub
-
Private Sub Form_Current()
-
Me.lstFacilityMgr.Requery
-
Me.lstRoomsPOC.Requery
-
End Sub
-
-
Private Sub cmdSearch_Click()
-
Dim strWhere As String
-
Dim lngLen As Long
-
Dim startStr As String
-
If Not IsNullOrEmpty(Me.cboSearchLastName) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[LastName] ='" & Me.cboSearchLastName & "'"
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchFirstName) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[FirstName] ='" & Me.cboSearchFirstName & "'"
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchOrganization) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[OrganizationFK] =" & Me.cboSearchOrganization
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchShopName) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[ShopNameFK] =" & Me.cboSearchShopName
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchOfficeSym) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[OfficeSymFK] =" & Me.cboSearchOfficeSym
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchBuildingName) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[BuildingFK] =" & Me.cboSearchBuildingName
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchRoomName) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[RoomsPK] =" & Me.cboSearchRoomName
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchEquipmentName) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[EquipmentNameFK] =" & Me.cboSearchEquipmentName
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchEquipmentSerialNum) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[SerialNum] ='" & Me.cboSearchEquipmentSerialNum & "'"
-
End If
-
If Not IsNullOrEmpty(Me.cboSearchEquipmentIP) Then
-
startStr = IIf(strWhere = "", "", " AND ")
-
strWhere = strWhere & startStr & "[EquipmentIP] ='" & Me.cboSearchEquipmentIP & "'"
-
End If
-
Call MsgBox(strWhere, vbOKOnly, "Debug")
-
lngLen = Len(strWhere) - 5
-
If lngLen <= 0 Then
-
MsgBox "No criteria", vbInformation, "Nothing to do."
-
Else
-
'strWhere = Left$(strWhere, lngLen)
-
MsgBox strWhere
-
If DCount("*", "qryRecordSet", strWhere) = 0 Then
-
MsgBox "No corresponding records to your search criteria." & vbCrLf & vbCrLf
-
Me.FilterOn = False
-
Me.cboSearchBuildingName = ""
-
Me.cboSearchRoomName = ""
-
Me.cboSearchOrganization = ""
-
Me.cboSearchShopName = ""
-
Me.cboSearchOfficeSym = ""
-
Me.cboSearchLastName = ""
-
Me.cboSearchFirstName = ""
-
Else
-
Me.Filter = strWhere
-
Me.FilterOn = True
-
End If
-
End If
-
End Sub
-
-
-
-
Function IsNullOrEmpty(val As Variant) As Boolean
-
'First conditional validates for Nothing
-
'Second condition validates for an Empty String situation "" or " "
-
Dim ret As Boolean: ret = False
-
If IsMissing(val) Then
-
ret = True
-
ElseIf (val Is Nothing) Then
-
ret = True
-
ElseIf (val & vbNullString = vbNullString) Then
-
ret = True
-
ElseIf (Len(Trim(val)) <= 0) Then
-
ret = True
-
End If
-
-
IsNullOrEmpty = ret
-
End Function
-
-
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.
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. - If DCount("*", "qryRecordSet", "BuildingFK = " & Me.txtBuildingID & " And RoomsPK = " & Me.txtRoomsID > 1) Then
-
MsgBox "There are duplicates!" & vbCrLf & vbCrLf
-
'Code to remove duplicate records here
-
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 ...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |