0

我有两张桌子,我们称它们为 T1 和 T2。T1目前有近600条记录,其中一条包含ID号,另一条包含标题,所以ID和TITLE:

T1
ID   | TITLE
-----|----------
1    | Title ABC
...  | ...
201  | Title XYZ
...  | ...
411  | Title 123

T2 有一个 ID 字段、一个 Titles 字段、一个 Accepted Titles 字段和一个 Rejected Titles 字段,因此 ID、TITLES、ACCEPTED TITLES 和 REJECTED TITLES。访问表单使用多选列表框从 T1 中选择一个或多个 TITLES,但需要多个,但通常不超过十个。一旦进入 T2 的 TITLES 字段,这是与从 T1 中选择的标题相对应的记录 ID 的数字,我希望 T2 中每个 ACCEPTED TITLES 和 REJECTED TITLES 的组合框仅限于显示这些标题对应于在 TITLES 字段中输入的 ID。所以,如果我在 T2 的 TITLES 字段中,

T2
ID | TITLES        | ACCEPTED TITLES  | REJECTED TITLES 
---|---------------|------------------|----------------
1  | 1, 201, 411   |                  |                

我希望 ComboBox 的下拉菜单仅显示与输入到 TITLES 字段中的那些 ID 对应的标题。因此,以 ACCEPTED TITLES 字段为例,它可能如下所示:

T2
ID | TITLES        | ACCEPTED TITLES    | REJECTED TITLES 
---|---------------|--------------------|---------------
1  | 1, 201, 411   | | [ ] Title ABC  \/|
                     | [ ] Title XYZ   |
                     | [ ] Title 123   |

我想我应该能够构建一个 SELECT WHERE IN (...) 语句,我可以在 ACCEPTED TITLES 和 REJECTED TITLES 的“行源”属性中使用该语句。那么该列表将与为 TITLES 选择的项目一样短,而不是 600 多条记录。这也完全消除了在 ACCEPTED TITLES 或 REJECTED TITLES 下输入错误的可能性,因为这些标题只能从在 TITLES 下输入的标题中选择。但是,我还不知道如何构建这样的 SELECT 语句。

任何帮助将不胜感激。谢谢你的时间。

4

2 回答 2

0

我为 T2(TitleStatus)提出了一个稍微不同的设计。

Create Table [TitleStatus] ([TitleID] Number, [StatusID] Number);
Create Index TitleIDindex ON [TitleStatus] (TitleID) WITH PRIMARY;

然后是另一个表格来存放状态。就像是

Create Table [Statuses] ([StatusID] Number, [StatusText] String);
Create Index StatusIDindex ON [Statuses] (StatusID) WITH PRIMARY;

(此表可以用一列组织,但无论哪种方式,您都希望防止诸如“已接受”和“已接受标题”之类的状态潜入。然后您将有两条相同的记录,但您自然不知道寻找两者)

然后 T2 (TitleStatus) 看起来像

TitleStatus
TITLEID   | STATUSID
----------|----------
1         | 1
...       | ...
201       | 1
...       | ...
411       | 1
500       | 2

哪里Statuses看起来像

 StatusID | StatusText
 1        | Accepted
 2        | Rejected

你的插入应该是相当直接的形式。

您可以像这样获得所有接受的标题

Select T1.Title, Status.StatusText
From T1
Inner join TitleStatus TS on TS.TitleID = T1.ID
Inner Join Statuses S on S.StatusID = TS.StatusID
Where S.StatusText = 'Accepted'
于 2012-11-14T19:12:45.857 回答
0

我找到了解决方案。它实际上比我意识到的要简单得多:

Public Function GetTitleIDs() As String
   Dim IDData As ADODB.Recordset
   Dim SubLookup As Variant
   Dim SelectSubmissions As String

   ' Should pull the same records and in the same order as '
   ' those found in originating Listbox '
   Set IDData = CurrentProject.Connection.Execute("SELECT [Poems].[ID], [Poems].[Title] FROM Poems ORDER BY [Title];")
   SubLookup = IDData.GetRows

   ' Submissions is the name of my originating Listbox. The rest remains unaltered. '
   Dim listrow As Integer
   For listrow = 0 To Me.Submissions.ListCount - 1
      If Me.Submissions.Selected(listrow) = True Then
         SelectSubmissions = ", " & SubLookup(0, listrow) & SelectSubmissions
      End If
   Next
   If Len(SelectSubmissions) > 0 Then
      SelectSubmissions = Right(SelectSubmissions, Len(SelectSubmissions) - 1)
   End If

   GetTitleIDs = SelectSubmissions
End Function

Private Sub Form_Current()
   ' Needed to update existing Listbox entry or entries when record is loaded '
   ' in form. If record is new, Listbox(es) will simply contain no records. This '
   ' can source all records for the fields you want in order to show existing '
   ' Listbox entries without fail. '
   Me.Declined.RowSource = "SELECT [Poems].[ID], [Poems].[Title], [Poems].[Year Completed], [Poems].[Blog Location] FROM Poems ORDER BY [Title];"
   Me.Accepted.RowSource = "SELECT [Poems].[ID], [Poems].[Title], [Poems].[Year Completed], [Poems].[Blog Location] FROM Poems ORDER BY [Title];"
End Sub

Private Sub Declined_GotFocus()
   ' Presumably your subset selection list gets focus after '
   ' checking list entries in originating Listbox. '
   Me.Declined.RowSource = "SELECT [Poems].[ID], [Poems].[Title], [Poems].[Year Completed], [Poems].[Blog Location] FROM Poems WHERE [ID] IN (" & GetTitleIDs & ") ORDER BY [Title];"
End Sub

Private Sub Accepted_GotFocus()
   Me.Accepted.RowSource = "SELECT [Poems].[ID], [Poems].[Title], [Poems].[Year Completed], [Poems].[Blog Location] FROM Poems WHERE [ID] IN (" & GetTitleIDs & ") ORDER BY [Title];"
End Sub

这将仅显示在提交项下选中的已拒绝和已接受选项,将选择选项限制为仅对这些字段感兴趣的值。这是完美的。

只要分配给 IDData 的记录和您要检查的 ListBox 的表和顺序相同,ID 将始终正确排列。

于 2012-11-17T09:57:02.527 回答