编写高级过滤系统

时间:2015-02-05 23:13:31

标签: sql vba access-vba

这是我的关系

enter image description here

用户希望能够使用下拉菜单添加过滤器并相应地过滤志愿者。过滤器来自VolunteerWorkArea表(VolunteerWorkAreaId)。我这样做的方式很痛苦,可能是因为缺乏知识。每次用户添加新过滤器时,我都必须以复杂的方式更改SQL。例如,这是用户添加三个过滤器后的SQL。

SELECT DISTINCT
        Volunteer.VolunteerId AS Volunteer_VolunteerId ,
        Volunteer.VolunteerFirstName ,
        Volunteer.VolunteerLastName ,
        Volunteer.VolunteerOtherName ,
        Volunteer.VolunteerStreetAddress ,
        Volunteer.VolunteerSuburb ,
        Volunteer.VolunteerPostCode ,
        Volunteer.VolunteerHomePhone ,
        Volunteer.VolunteerMobilePhone ,
        Volunteer.VolunteerGender
FROM    Volunteer
        INNER JOIN ( VolunteerWorkArea
                     INNER JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.VolunteerWorkAreaId = VolunteerWorkAreaAllocation.VolunteerWorkAreaId
                   ) ON Volunteer.VolunteerId = VolunteerWorkAreaAllocation.VolunteerId
WHERE   VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 17
        AND Volunteer.VolunteerId IN (
        SELECT DISTINCT
                Volunteer.VolunteerId
        FROM    Volunteer
                INNER JOIN ( VolunteerWorkArea
                             INNER JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.[VolunteerWorkAreaId] = VolunteerWorkAreaAllocation.[VolunteerWorkAreaId]
                           ) ON Volunteer.[VolunteerId] = VolunteerWorkAreaAllocation.[VolunteerId]
        WHERE   VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 16
                AND Volunteer.VolunteerId IN (
                SELECT DISTINCT
                        Volunteer.VolunteerId AS Volunteer_VolunteerId
                FROM    Volunteer
                        INNER JOIN ( VolunteerWorkArea
                                     INNER
JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.[VolunteerWorkAreaId] = VolunteerWorkAreaAllocation.[VolunteerWorkAreaId]
                                   ) ON Volunteer.[VolunteerId] = VolunteerWorkAreaAllocation.[VolunteerId]
                WHERE   VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 15 ) );

因此,每次用户添加过滤器时,我都必须通过代码大量更改SQL。我很确定有一种简单的方法可以做到这一点。有人可以帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

如果您希望用户在志愿者工作区的基础上决定志愿者,那么更简单的解决方案将使用子表单。

  1. 创建一个不基于任何表的新表单
  2. 使用向导使用 volunteerWorkArea 表创建并绑定组合框 (比如说名称:combo2 )。
  3. 创建一个子表单并将其与其他两个表绑定,即志愿者& 志愿者工作区域分配图片的字段数量较少 样品
  4. 通过将visible属性设置为false来隐藏ID字段。
  5. 设置子图像控件属性,如图像所示( combo2 作为母版 field和Child as volunteerWorkAreaID
  6. enter image description here