vba 使用 Dlookup 返回访问中的多个项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11059589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:30:38  来源:igfitidea点击:

Return multiple items in access using Dlookup

ms-accessvbaaccess-vba

提问by Matthew Optional Meehan

DealID is set using a Combobox and it needs to Display up to 3 MID's in the field below it

DealID 是使用组合框设置的,它需要在其下方的字段中显示最多 3 个 MID

Private Sub DealID_AfterUpdate()
Dim strFilter As String
strFilter = "DealID = " & Me!DealID
Me!MID = DLookup("MID", "DealContent", strFilter)
Exit_ProductID_AfterUpdate:
Exit Sub
End Sub

That's the code I'm using the obvious limitation is Dlookup only returns the first result it finds. That creates 2 problems but Ill focus on the first, It won't display more than one MID

这就是我使用的代码,明显的限制是 Dlookup 只返回它找到的第一个结果。这会产生 2 个问题,但我会专注于第一个,它不会显示多个 MID

so how I can get the 1-3 MIDs to be displayed?

那么如何才能显示 1-3 个 MID?

The second issue I have is more in-depth but if anyone wants to help, a personal chat would be appreciated. Basically The form above is a child form and I need it to save a separate entry in the forms table for each Mount ID.

我的第二个问题更深入,但如果有人想提供帮助,我们将不胜感激。基本上上面的表单是一个子表单,我需要它在表单表中为每个安装 ID 保存一个单独的条目。

If anyone would like to help but doesn't understand(as is often the case with my submissions), I think screen sharing on Skype is the best option.

如果有人想提供帮助但不明白(就像我提交的内容一样),我认为在 Skype 上共享屏幕是最好的选择。

回答by Christian Specht

As already said in a comment, you can't use DLookupto return more than one value.

正如评论中已经说过的,您不能使用DLookup返回多个值。

You need to select the first three MIDs into a Recordset, loop through them and append them to Me!MID:

您需要将前三个MIDs选择到 a 中Recordset,循环遍历它们并将它们附加到Me!MID

Dim RS As DAO.Recordset
Dim SQL As String

'ordering is only important if you want the FIRST three MIDs.
'If you don't care, just omit the "order by MID" part.
SQL = "select top 3 MID from DealContent where DealID = xxx order by MID"

Set RS = CurrentDb.OpenRecordset(SQL)
Do While Not RS.EOF
    Me!MID = Me!MID & RS("mid") & " "
    RS.MoveNext
Loop
RS.Close
Set RS = Nothing

Note that this example uses DAO, which is the default (and recommended by MS) data access technology in newer Access versions. Older versions used ADO as default.
DAO works there as well, you just need a reference to Microsoft DAO x.x Object Library.

请注意,此示例使用 DAO,这是较新 Access 版本中的默认(并由 MS 推荐)数据访问技术。旧版本默认使用 ADO。
DAO 也可以在那里工作,您只需要引用Microsoft DAO x.x Object Library.