如何从Access中的表单记录源中删除重复项

时间:2015-10-09 05:29:41

标签: sql forms vba duplicates dcount

我为数据库和编程的有限(但仍在慢慢增长)知识构建了一个相当复杂的数据库。所以,我非常感谢你的帮助。

数据库跟踪客房,建筑物,房间和房间内的设备。

我有一个搜索表单,用于过滤我几乎查询整个数据库的联合查询。 SQL如下:

SELECT tblcustomer.organizationfk, 
   tblcustomer.shopnamefk, 
   tblcustomer.officesymfk, 
   tblcustomer.lastname, 
   tblcustomer.firstname, 
   tblfacilitymgr.buildingfk, 
   tblrooms.roomspk, 
   tblbuilding.buildingname, 
   tblrooms.roomname, 
   tblcabinet.cabinetname, 
   tblequipment.cabinetfk, 
   tblequipment.equipmentnamefk, 
   tblequipment.equipmentbrandfk, 
   tblequipment.equipmentnetworktypefk 
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) 
   LEFT JOIN (tblcabinet 
              LEFT JOIN tblequipment 
                     ON tblcabinet.cabinetpk = tblequipment.cabinetfk) 
          ON tblrooms.roomspk = tblcabinet.roomsfk 
UNION 
SELECT tblcustomer.organizationfk, 
   tblcustomer.shopnamefk, 
   tblcustomer.officesymfk, 
   tblcustomer.lastname, 
   tblcustomer.firstname, 
   tblrooms.buildingfk, 
   tblrooms.roomspk, 
   tblbuilding.buildingname, 
   tblrooms.roomname, 
   tblcabinet.cabinetname, 
   tblequipment.cabinetfk, 
   tblequipment.equipmentnamefk, 
   tblequipment.equipmentbrandfk, 
   tblequipment.equipmentnetworktypefk 
FROM   ((tblbuilding 
     INNER JOIN tblrooms 
             ON tblbuilding.buildingpk = tblrooms.buildingfk) 
    LEFT JOIN (tblcabinet 
               LEFT JOIN tblequipment 
                      ON tblcabinet.cabinetpk = tblequipment.cabinetfk) 
           ON tblrooms.roomspk = tblcabinet.roomsfk) 
   INNER JOIN (tblcustomer 
               INNER JOIN tblroomspoc 
                       ON tblcustomer.customerpk = tblroomspoc.customerfk) 
           ON tblrooms.roomspk = tblroomspoc.roomsfk;

搜索表单如下所示:

Search Form

cmdsearch的代码在这里:

 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 txtBuildingID_AfterUpdate()
Me.lstFacilityMgr.Requery
End Sub

Private Sub txtRoomsID_AfterUpdate()
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.cboSearchEquipmentSerialNo) Then
    startStr = IIf(strWhere = "", "", " AND ")
    strWhere = strWhere & startStr & "[SerialNoFK] =" & Me.cboSearchEquipmentSerialNo
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

我的搜索表单完美无缺。我遇到的问题是某些搜索会返回重复的结果。这是因为联合查询包含重复的结果。我不知道如何生成包含我需要的所有信息的查询,而不是重复结果。因为,一个客户可以是多个建筑物的设施管理员,一个建筑物可以有多个设施管理者。一个客户可以是多个房间的联系点,一个房间可以有多个POC。设备只能在一个房间内。

澄清,如果建筑&#34; A&#34; ,房间&#34; 1100&#34;有三个POC然后如果我搜索建筑物A,房间1100我看到三个结果。我只需看一个结果。

我的表单上有两个文本框(隐藏,因此用户不会看到它们)。 txtBuildingID和txtRoomsID。表单上的其他所有内容都基于这两个文本框进行重新查询。我需要这两个文本框的组合是唯一的。那是因为,如果我只搜索建筑物&#34; A&#34;和建筑A有三个房间,我应该看到建筑物ID 1 /房间ID 1 ...建筑物ID 1 /房间ID 2等。

这是因为我想使用多个条件进行过滤,但只显示唯一的建筑物ID /房间ID。这是因为如果我搜索史密斯&#34;他是A楼1100室的POC,我想看看那栋楼/房间以及有关那个房间的所有信息。因为,如果史密斯没有接听他的电话,我可以致电&#34;琼斯&#34;。

我不关心删除哪条记录,只要我在两个文本框之间有唯一的记录。我对SQL知之甚少,但从我知之甚少,我不认为这可以用SQL来完成。我想我需要使用DCount来计算记录,然后删除重复项。我不知道该怎么做。我在谷歌上度过了几天,我甚至还没有接近解决方案。充其量,如果我得到正确的语法,我可以计算重复的数量(buildingfk和roomspk之间超过1个唯一记录),但是我不知道如何删除重复项。或者,如果我需要指定要删除的副本。就个人而言,只要他们被删除,我就不在乎。

我问过我的一位朋友,他非常善于编程,并且让他难过。他建议我问这里。所以我非常感谢你的帮助。 Query

1 个答案:

答案 0 :(得分:0)

我正在取得一些进展。

我已经能够成功计算记录数量。

我使用的代码在这里:

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

MsgBox仅用于故障排除。但我测试了它并确认它有效。现在,我需要以某种方式将其应用于表单过滤器。那就是Me.Filter。目前,那就是Me.Filter = strWhere。

基本上,我需要进一步滤除strWhere,以便不再有重复。我该怎么做?