【问题标题】:Make combo box record as table field name , MS Access将组合框记录作为表字段名称,MS Access
【发布时间】:2017-05-16 07:49:39
【问题描述】:

我在表单中有组合框,我希望它的下拉值是表字段名称。表名称是 tblCap,字段是 Year1、Year2 和 Year3。我希望在组合框中有下拉列表as Year1,Year2,Year3 and when that year is selected it should display related field in subform.

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: vba ms-access combobox subform ms-access-forms


    【解决方案1】:

    这比你想象的要简单得多:

    1. 打开组合框的属性表,标签 数据
    2. 设置RowSourceType:值列表
    3. 设置行来源:"Year1";"Year2";"Year3"

    要在子表单中显示选定的值(字段名称),请使用以下表达式:

    =[Parent]![NameOfYourCombobox]
    

    在主窗体的文本框中显示子窗体的选定字段名称的值:

    =[NameOfYourSubformControl].[Form]([NameOfYourCombobox])
    

    或 (?) 在子表单上:

    =[Parent]([Parent]![NameOfYourCombobox])
    

    【讨论】:

    • 谢谢,但在那之后我将如何显示该字段,就像链接的方式一样?当从组合框 Year1 中单击时,它应该在下面的子表单中显示该字段(Year1),这将如何工作?
    • 使用@Kostas 已经展示的AfterUpdate 事件。或类似的方法。
    • 我已将该表达式添加到我的答案中。
    • 表达式是应该显示值的 TextBox 的 ControlSource。无需代码。
    • 它将如何适用于所有文本框?当在组合框中单击 2000 时,它应该显示在子表单字段 2000 中,当单击 2001 时,它将如何链接到 2001 以取消隐藏它,因为我已经在组合框中写入了值。
    【解决方案2】:

    在 cmets 之后更新。

    一旦按照@Gustav 的回答设置了RowSource,为了根据ComboBox 的值隐藏/取消隐藏TextBox 控件,您需要将它们的Visible 属性设置为True/False

    在“事件”选项卡上,将 ComboBox 控件的 AfterUpdate 设置为 [Event Procedure],并在文件隐藏代码上设置以下内容:

    Private Sub YourComboControlName_AfterUpdate()
        With Me
            Select Case .YourComboBoxName.Value
                Case "Year1":
                    With .YourSubformName.Form
                        .Your2000TextBoxControlName.ColumnHidden = false 
                        .Your2001TextBoxControlName.ColumnHidden = true
                        .Your2002TextBoxControlName.ColumnHidden = true 
                    End With
    
                Case "Year2":
                    With .YourSubformName.Form
                        .Your2000TextBoxControlName.ColumnHidden = true
                        .Your2001TextBoxControlName.ColumnHidden = false
                        .Your2002TextBoxControlName.ColumnHidden = true 
                    End With
    
                Case "Year3":
                    With .YourSubformName.Form
                        .Your2000TextBoxControlName.ColumnHidden = true 
                        .Your2001TextBoxControlName.ColumnHidden = true 
                        .Your2002TextBoxControlName.ColumnHidden= false 
                    End With
            End Select
        End With
    End Sub
    

    【讨论】:

    • 你把我弄糊涂了。没有交叉表之类的东西。也许是一个查询?
    • 2000,2001 和 2002 是字段名称。您是否希望在组合框中显示字段名称?
    • Kostas,hi , can you kindly let me know how to relate fields with combo box , like in combo box i added value , now when that value is selected it should display that column in subform?非常感谢您的帮助。
    • 如果我理解正确,在Combo_AfterUpdate() 上将子窗体中控件的ControlSource 设置为组合框的值,例如Me.YourSubformName.Form.YourYearControlName.ControlSource = Me.YourComboControlName.Value.
    • 查看更新。组合框的值不是 2000,2001,2002 而是 Year1,Year2,Year3。
    猜你喜欢
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多