将ListBox绑定到MS Access 2003中的查询

时间:2022-02-22 15:38:35

I have a very simple application with two tables, ParentTable and ChildrenTable. Each child in the table has a foreign key to a parent (parent-child relationship is one-to-many).

我有一个非常简单的应用程序,有两个表,ParentTable和ChildrenTable。表中的每个子项都有父项的外键(父子关系是一对多)。

When I display a form with parent info, I want to display a ListBox with all the parents children.

当我显示包含父信息的表单时,我想显示一个包含所有父项子项的ListBox。

I already got a ListBox that displays all the children, but I'm not sure how to change the query so that I see only the active parents children (the parent which I am looking at its form right now). It goes like this:

我已经有了一个显示所有子项的ListBox,但是我不知道如何更改查询以便我只看到活动的父项子项(我现在正在查看其表单的父项)。它是这样的:

SELECT ChildrenTable.IdNumber, ChildrenTable.FirstName, ChildrenTable.LastName FROM ChildrenTable ORDER BY [FirstName];

I guess I'm looking for something like:

我想我正在寻找类似的东西:

WHERE ChildrenTable.ParentIdNumber == <active parent>.IdNumber

1 个解决方案

#1


4  

A form named frmParent includes a text box control named txtIdNumber which displays the IdNumber from ParentTable.

名为frmParent的表单包含一个名为txtIdNumber的文本框控件,该控件显示来自ParentTable的IdNumber。

Then the query for your list box rowsource can reference the value in txtIdNumber:

然后查询列表框rowsource可以引用txtIdNumber中的值:

SELECT c.IdNumber, c.FirstName, c.LastName
FROM ChildrenTable AS c
WHERE c.ParentIdNumber = Forms!frmParent!txtIdNumber
ORDER BY c.FirstName;

You can update the list box as you move through ParentTable records in frmParent by using this code for the form's "On Current" event:

通过在表单的“On Current”事件中使用此代码,您可以在frmParent中移动通过ParentTable记录时更新列表框:

Private Sub Form_Current()
    Me.YourListBoxName.Requery
End Sub

#1


4  

A form named frmParent includes a text box control named txtIdNumber which displays the IdNumber from ParentTable.

名为frmParent的表单包含一个名为txtIdNumber的文本框控件,该控件显示来自ParentTable的IdNumber。

Then the query for your list box rowsource can reference the value in txtIdNumber:

然后查询列表框rowsource可以引用txtIdNumber中的值:

SELECT c.IdNumber, c.FirstName, c.LastName
FROM ChildrenTable AS c
WHERE c.ParentIdNumber = Forms!frmParent!txtIdNumber
ORDER BY c.FirstName;

You can update the list box as you move through ParentTable records in frmParent by using this code for the form's "On Current" event:

通过在表单的“On Current”事件中使用此代码,您可以在frmParent中移动通过ParentTable记录时更新列表框:

Private Sub Form_Current()
    Me.YourListBoxName.Requery
End Sub