【问题标题】:MS access add item to combo box if non in list如果不在列表中,则 MS 访问将项目添加到组合框
【发布时间】:2012-07-14 22:49:27
【问题描述】:

我有一个访问表单,如果组合框中没有,我想知道如何在组合框中添加项目。 我的组合框处于值模式。

【问题讨论】:

    标签: ms-access combobox


    【解决方案1】:

    不幸的是,如果不将表单更改为设计模式并添加新值,则无法永久更改行源。使用代码可以做到这一点,但是当人们工作时这不是一个好主意。最简单的事情是创建一个小表并将值添加到其中。当您关闭表单时,新值将被保存。

    Allen Browne 描述了如何做到这一点:http://allenbrowne.com/ser-27.html

    这是他展示的想法之一:

    Private Sub CategoryID_NotInList(NewData As String, Response As Integer)
        Dim strTmp As String
    
        'Get confirmation that this is not just a spelling error.
        strTmp = "Add '" & NewData & "' as a new product category?"
        If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
    
            'Append the NewData as a record in the Categories table.
            strTmp = "INSERT INTO Categories ( CategoryName ) " & _
                "SELECT """ & NewData & """ AS CategoryName;"
            DBEngine(0)(0).Execute strTmp, dbFailOnError
    
            'Notify Access about the new record, so it requeries the combo.
            Response = acDataErrAdded
        End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      您需要将属性 limit to list 设置为 true。

      然后在 On Not In List 事件中添加一些代码,该代码可能会将值添加到组合框中。 Here is a tutorial.或者你可以查看其他答案。

      请注意,通常最好使用存储组合框值的表格。使用值列表,除非您禁用快捷菜单,否则用户可以右键单击组合框选择 Edit List Items... 并修改列表,即使它设置为限制为列表...这有效地破坏了您尝试放置的任何限制在球场上。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-02
        • 2013-12-05
        • 2021-11-04
        • 1970-01-01
        相关资源
        最近更新 更多