在我提出这个问题之前,我必须承认我是SQL的新手但在这里:
我有3个表: Crown Facility , tblSubProjects 和 lnkSubProjectFacility 。
我使用lnkSubProjectFacility将设施分配给子项目,反之亦然。当用户从表单中的组合框(cboAddFacSubProject)中选择子项目时,组合框中的AfterUpdate
事件会导致2个列表框填充lnkSubProjectFacility中的关联记录表记录。
左侧列表框( lstAddSubProjectFacilities )显示与子项目关联的所有设施,右侧( lstAddSubProFac )显示用户可以分配的所有可用剩余设施,一个命令按钮,从右侧列表框中删除一个可用的工具并将其移到左侧(从而在 lnkSubProjectFacility 表中创建一个新记录)。
一切正常,但我似乎无法使用下面的VBA代码在组合框的AfterUpdate
事件中使用正确的可用设施填充正确的列表框:
Private Sub cboAddFacSubProject_AfterUpdate()
Dim strSQL As String, strSQL2 As String
'String SQL statment variable for the LEFT listbox - to display all ASSIGNED facilities
strSQL = "SELECT [CROWN Facility].FACILITY_ID, " & _
"[CROWN Facility].FACILITY_NAME, " & _
"lnkSubProjectFacility.SUBPROJECT_ID " & _
"FROM [CROWN Facility] INNER JOIN lnkSubProjectFacility " & _
"ON [CROWN Facility].FACILITY_ID = lnkSubProjectFacility.FACILITY_ID " & _
"WHERE lnkSubProjectFacility.SUBPROJECT_ID =" & Me.cboAddFacSubProject & " " & _
"ORDER BY [CROWN Facility].FACILITY_NAME"
'String SQL statment variable for the RIGHT listbox - to display all AVAILABLE facilities
strSQL2 = "SELECT [CROWN Facility].FACILITY_ID, " & _
"[CROWN Facility].FACILITY_NAME, " & _
"lnkSubProjectFacility.SUBPROJECT_ID " & _
"FROM [CROWN Facility] LEFT JOIN lnkSubProjectFacility " & _
"ON [CROWN Facility].FACILITY_ID = lnkSubProjectFacility.FACILITY_ID " & _
"WHERE lnkSubProjectFacility.SUBPROJECT_ID <>" & Me.cboAddFacSubProject & " " & _
"ORDER BY [CROWN Facility].FACILITY_NAME"
'RowSource for the LEFT listbox - to display all assigned facilities
lstAddSubProjectFacilities.RowSource = strSQL
'RowSource for the RIGHT listbox - to display all available facilities
lstAddSubProFac.RowSource = strSQL2
'This is just updating a label showing the count of items in the left listbox
lblListCt.Caption = lstAddSubProjectFacilities.ListCount & " Facilities Selected"
End Sub
执行此代码后,左侧列表框中的所有内容都很好,该子项目的所有已分配记录都正确显示。但是,尽管右侧列表框不包括左侧显示的所有设施,但它也不包括分配给该表中任何其他子项目的任何设施。
除了搜索此问题的小时数之外,我还尝试在最后添加strSQL2
(这当然不起作用)以及许多其他内容,例如:IS NULL
变量更改JOIN类型。最近,我将WHERE
更改为AND
,但不返回任何内容。
我相信有一个相当简单的解决方案,但我非常感谢有任何帮助让我到那儿!
注意:我使用的是Access 2010,但我认为这没有任何区别。
编辑:这是lnkSubProjectFacility表的结构:
SUBPROJECT_ID FACILITY_ID
7 20000003
7 20000025
7 20000027
8 20010302
8 20021781
9 20040035
9 20044392
10 20045465
17 10000282
17 10000452
17 10000844
21 20000005
21 20000019
21 20000026
CROWN设施表结构:
FACILITY_ID FACILITY_NAME
20000003 Barnes
20000025 Bio-Medical Applications
20000027 Barnes Center
20010302 Atlantic
20021781 Anthonys Hospital
20040035 Black Hawk
20044392 Ames
20045465 Arnold
10000282 BETHANY
10000452 ANDOVER
10000844 Ankeny
20000005 Columbia
20000019 Baptist
20000026 Childrens Hospital
tblSubProjects表格结构:
SUBPROJECT_ID SUBPROJECT
7 Service Project1
7 Service Project1
7 Service Project1
8 Service Project2
8 Service Project2
9 Service Project3
9 Service Project3
10 Service Project4
17 CatheterReduction1
17 CatheterReduction1
17 CatheterReduction1
21 Patient Access3
21 Patient Access3
21 Patient Access3
答案 0 :(得分:0)
我将假设lnkSubProjectFacility旨在将Facility与tblSubProjects相关联。对于schema
,lnkSubProjectFacility似乎是唯一一个我会改变的表lnkSubProjectFacility
SPF_Id, SubProject_Id, Facility_Id
1 7 20000003
2 7 20000025
3 7 20000027
4 8 20010302
. . .
. . .
. . .
尝试尽可能地规范化数据,以便更容易编写查询。
如果您正在尝试编写一个查询,该查询将获取尚未分配子项目的所有工具:
select [CROWN FACILITY].FACILITY_ID, [CROWN FACILITY].FACILITY_NAME
from [CROWN FACILITY] as CF
where not exists (select *
from lnkSubProjectFacility as lSPF
where CF.FACILITY_ID == lSPF.FACILITY_ID)
我认为查询应该有效,很难说我什么时候无法测试。 有时连接不是答案。连接可以看起来更好,但有时子查询更好