MSAccess:从一对多关系中选择一行

发布于 2025-01-06 21:46:49 字数 279 浏览 0 评论 0原文

在MS Access 2010中,我有TableA与TableB一对多相关,TableB与TableC一对多相关。 TableC 有 1 个数字字段和 1 个日期字段(除了查找字段之外)。

FormA 的数据源是连接 TableA 和 TableB 的选择查询。我想向 FormA 添加一个额外的只读字段,其中包含 TableC 中最新日期行中的数字字段。

执行此操作的最佳方法是什么?我是否应该在 TableB 中创建一个字段并创建一个更新查询(或宏?)来在连接查询运行之前填充该列?

谢谢。

In MS Access 2010, I have TableA related one-to-many to TableB, and TableB related one-to-many to TableC. TableC has one numeric field and one date field (in addition to the lookup field).

FormA's datasource is a select query that joins TableA and TableB. I want to add one additional read-only field to FormA that contains the numeric field from TableC from the row with the most recent date.

What's the best way to go about doing this? Should I create a field in TableB and create an update query (or macro?) that populates that column prior to the join query running?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携余温的黄昏 2025-01-13 21:46:50

这是一个表单,您需要只读,因此 DlookUp 应该适合:http://support.microsoft.com/ kb/208786

= DLookup("[numeric field]", "TableC", "[LookupKey] = " & [PK] & " AND TheDate = DMax(""TheDate"", ""TableC"", ""LookupKey=" & [PK] & """)")

OP的编辑有两个问题,第一个是它表明date是一个可接受的字段名称,它是一个保留字,不应该使用。其次,它表明 DlookUp 中需要方括号,但实际上不需要。

或者:

Dim rs As DAO.Recordset

s = "SELECT TOP 1 c.[numeric field] " _
  & "FROM TableC c " _
  & "WHERE c.LookupKey= " & Me.[PK]
  & "ORDER BY c.TheDate DESC"

set rs = currentDB.Openrecordset(s)
Me.SomeControl = rs![numeric field]

This is a form and you require read only, so DlookUp should suit : http://support.microsoft.com/kb/208786

= DLookup("[numeric field]", "TableC", "[LookupKey] = " & [PK] & " AND TheDate = DMax(""TheDate"", ""TableC"", ""LookupKey=" & [PK] & """)")

There are two problems with the edit by the OP, the first is that it suggests that dateis an acceptable field name, it is a reserved word and should not be used. Secondly, it suggests that square brackets are needed in DlookUp, they are not.

Alternatively:

Dim rs As DAO.Recordset

s = "SELECT TOP 1 c.[numeric field] " _
  & "FROM TableC c " _
  & "WHERE c.LookupKey= " & Me.[PK]
  & "ORDER BY c.TheDate DESC"

set rs = currentDB.Openrecordset(s)
Me.SomeControl = rs![numeric field]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文