1

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

该数据库跟踪客户、建筑物、房间和房间中的设备。

我有一个搜索表单,它过滤了我所做的几乎整个数据库的联合查询。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;

搜索表单如下所示:

搜索表格

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。设备只能在一个房间里。

澄清一下,如果 Building "A", room "1100" 有三个 POC,那么如果我搜索 building A, room 1100 我会看到三个结果。我只需要看到一个结果。

我的表单上有两个文本框(隐藏以便用户看不到它们)。txtBuildingID 和 txtRoomsID。表单上的所有其他内容都基于这两个文本框进行查询。我需要这两个文本框的组合是唯一的。那是因为,如果我只搜索“A”楼并且 A 楼有三个房间,我应该会看到 Building ID 1 / Room ID 1... Building ID 1 / Room ID 2 等。

这样做的原因是因为我想使用多个条件进行过滤,但只显示一个唯一的建筑物 ID/房间 ID。这是因为如果我搜索“Smith”并且他是 A 栋 1100 室的 POC,我想查看该建筑物/房间以及有关该房间的所有信息。因为,如果史密斯不接电话,我可以打电话给“琼斯”。

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

我问了一个非常擅长编程的朋友,这让他很难过。他建议我在这里问。所以我真的很感谢你在这里的帮助。 询问

4

1 回答 1

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 以便不再有重复项。我怎么做?

于 2015-10-13T05:12:07.903 回答