显示列表框中的所有记录

时间:2016-08-28 23:48:53

标签: ms-access filter combobox listbox

我目前正在建立数据库,记录教师和学生是谁以及何时安排上课。我有一个列表框从正在从表中获取数据的组合框过滤的查询中获取数据。

过滤器正在运行,但无法显示查询中的所有记录。

有没有办法在组合框中添加一个选项,将查询中的所有记录拉到列表框而不是过滤的几个?

PS。优选地,通过使用嵌入式宏,必要时使用VB

组合框的行源('Teachers'是表格):

SELECT [Teachers].[TeacherID], [Teachers].[Forename], [Teachers].[Surname] 
FROM Teachers 
ORDER BY [Surname], [Forename], [TeacherID];

列表框的行源('TodayLessons'是查询,'TeacherID'正在使用表单上组合框的条件):

SELECT [TodayLessons].[LessonID], [TodayLessons].[SlotID], [TodayLessons].[Lesson Date], 
       [TodayLessons].[Lesson Time], [TodayLessons].[TeacherID], [TodayLessons].[StudentID] 
FROM TodayLessons 
ORDER BY [Lesson Date], [Lesson Time], [TeacherID];

行源导致问题(我尝试在'UNION'之前添加'FROM Teacher')

SELECT -99 AS TeacherID, "* All *" AS Forename, "" AS Surname UNION SELECT [Teachers].[TeacherID], [Teachers].[Forename], [Teachers].[Surname] FROM Teachers ORDER BY [Surname], [Forename], [TeacherID];

2 个答案:

答案 0 :(得分:1)

如果您使用子表单而不是列表框(但更好地与开发人员和最终用户进行交互),那么您可以设置Filter on Empty Master =否。这意味着如果Link Master Fields是null表示子(子表单)将显示所有记录而不是其中任何记录。

答案 1 :(得分:1)

如果您想使用列表框,可以这样做:

Combobox,使用UNION查询添加“all”选项:

SELECT -99 AS TeacherID, "* All *" AS Forename, "" AS Surname
UNION
SELECT [Teachers].[TeacherID], [Teachers].[Forename], [Teachers].[Surname] 
FROM Teachers 
ORDER BY [Surname], [Forename], [TeacherID];

列表框:

SELECT [TodayLessons].[LessonID], [TodayLessons].[SlotID], [TodayLessons].[Lesson Date], 
       [TodayLessons].[Lesson Time], [TodayLessons].[TeacherID], [TodayLessons].[StudentID] 
FROM TodayLessons 
WHERE (TeacherID = Forms!yourForm!cboTeachers)
   OR (Forms!yourForm!cboTeachers = -99)
ORDER BY [Lesson Date], [Lesson Time], [TeacherID];

如果选择了组合框的特殊条目,WHERE子句的“OR”部分会使所有记录为True。

-99只是一个任意数字,如果TeacherID是自动编号,您也可以使用0代替。