无法在连续表单上设置 ComboBox.RowSource

发布于 2024-08-10 06:39:49 字数 1352 浏览 7 评论 0原文

我在设置表单上某个组合框的行源时遇到问题(被视为连续表单,尽管我在单表单模式下似乎也遇到了一些问题)。

该组合框绑定到一个名为 supplierID 的字段,旨在向用户显示某个项目的所有可能供应商的列表。我用于组合框的行源是:

SELECT DISTINCT Suppliers.name, Suppliers.supplierID
FROM Suppliers
INNER JOIN PartsSuppliers ON Suppliers.supplierID=PartsSuppliers.supplierID 
WHERE PartsSuppliers.partID = partID;

当我在查询设计器中查看此查询时(使用硬编码的partID),它工作正常 - 它为所选项目选择所有可能的供应商,并且不'不显示任何其他项目。但是,当我查看组合框中的项目时,它显示了 PartsSuppliers 表中存在的所有供应商(该表只有两列,将零件映射到可能的零件)他们的供应商)。

我还尝试在 OnFocus 事件中使用一些 VBA 设置组合框的 RowSource(对 partID 值进行硬编码),但它似乎从未更改 RowSource。我使用的 VBA 代码是:

Private Sub supplierID_GotFocus()
    Dim query As String

    query = "SELECT DISTINCT Suppliers.name, PartsSuppliers.supplierID "
    query = query & "FROM Suppliers INNER JOIN PartsSuppliers ON Suppliers.supplierID = PartsSuppliers.supplierID "
    query = query & "WHERE (((PartsSuppliers.partID)=" & partID & "));"

    supplierDropDown.RowSource = query
    supplierDropDown.Requery
End Sub

我还尝试在 RecordSet 中打开该查询,然后将该 RecordSet 设置为组合框的 RecordSet,但这也不起作用。

我做错了什么,或者我应该寻找其他方法来做出正确的下拉菜单?

注意我已经看到 组合框的自定义行源在 Access 中以连续形式,但接受的解决方案对我来说也不起作用。

I am having trouble setting the row source of a certain combo box on my form (viewed as a continuous form, although I also seem to have some problems in single-form mode).

The combo-box is bound to a field called supplierID, and is meant to present the user with a list of all the possible suppliers for an item. The row-source I am using for the combo-box is:

SELECT DISTINCT Suppliers.name, Suppliers.supplierID
FROM Suppliers
INNER JOIN PartsSuppliers ON Suppliers.supplierID=PartsSuppliers.supplierID 
WHERE PartsSuppliers.partID = partID;

When I view this query in the query designer (with a partID hard-coded), it works fine - it selects all the possible suppliers for the chosen item, and doesn't show any other items. But when I look at the items in the combo-box, it shows all the suppliers present in the PartsSuppliers table (which has just two columns, mapping the parts to the possible suppliers for them).

I have also tried to set the combo-box's RowSource using some VBA in the OnFocus event (hardcoding the partID value in), but it never seems to change the RowSource. The VBA code I am using is:

Private Sub supplierID_GotFocus()
    Dim query As String

    query = "SELECT DISTINCT Suppliers.name, PartsSuppliers.supplierID "
    query = query & "FROM Suppliers INNER JOIN PartsSuppliers ON Suppliers.supplierID = PartsSuppliers.supplierID "
    query = query & "WHERE (((PartsSuppliers.partID)=" & partID & "));"

    supplierDropDown.RowSource = query
    supplierDropDown.Requery
End Sub

I also tried opening that query in a RecordSet, and then using setting that RecordSet as the combo-box's RecordSet, but that didn't work either.

What am I doing wrong, or is there some other way that I should be looking at to make the correct drop down?

N.B. I have seen Custom row source for combo box in continuous form in Access, but that accepted solution didn't work for me either.

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

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

发布评论

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

评论(2

调妓 2024-08-17 06:39:49

使用 OnEnterOnExit 事件更改您的 RowSource

Private Sub supplierID_Enter()
    supplierDropDown.RowSource = _
        "SELECT DISTINCT Suppliers.name, PartsSuppliers.supplierID " & _
        "FROM Suppliers INNER JOIN PartsSuppliers ON Suppliers.supplierID = PartsSuppliers.supplierID " & _
        "WHERE PartsSuppliers.partID = " & partID & ";"
End Sub

Private Sub supplierID_Exit()
    supplierDropDown.RowSource = _
        "SELECT DISTINCT Suppliers.name, PartsSuppliers.supplierID " & _
        "FROM Suppliers INNER JOIN PartsSuppliers ON Suppliers.supplierID = PartsSuppliers.supplierID;"
End Sub

Use the OnEnter and OnExit events to change out your RowSource.

Private Sub supplierID_Enter()
    supplierDropDown.RowSource = _
        "SELECT DISTINCT Suppliers.name, PartsSuppliers.supplierID " & _
        "FROM Suppliers INNER JOIN PartsSuppliers ON Suppliers.supplierID = PartsSuppliers.supplierID " & _
        "WHERE PartsSuppliers.partID = " & partID & ";"
End Sub

Private Sub supplierID_Exit()
    supplierDropDown.RowSource = _
        "SELECT DISTINCT Suppliers.name, PartsSuppliers.supplierID " & _
        "FROM Suppliers INNER JOIN PartsSuppliers ON Suppliers.supplierID = PartsSuppliers.supplierID;"
End Sub
茶色山野 2024-08-17 06:39:49

一旦组合项目受到限制,我一直在努力解决如何避免数据表中留下的“空白”,并且很长一段时间找不到任何直接的选项......直到现在!

将所有可用选项保留在组合框中,但使用 OnCurrent 添加临时验证规则,可以完美地实现这一目的。

希望这可以防止有人像我一样长时间用头撞桌子。

I've been struggling with how to avoid the 'blanks' left behind in a datasheet once Combo items are restricted and couldn't find any straight forward option for a long time....until now!

Leaving all of the available options in the Combo box but adding a temporary validation rule, using OnCurrent, does the trick perfectly.

Hoping this prevents someone banging their head on the desk for as long as I have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文