将组合框记录为 table 字段名称,MS Access

Make combo box record as table field name , MS Access

我在表单中有组合框,我希望它的下拉值是 Table 字段 name.Table 名称是 tblCap,字段是 Year1、Year2 和 Year3.I want that in the Combo box to have dropdown list as Year1,Year2,Year3 and when that year is selected it should display related field in subform.

如有任何帮助,我们将不胜感激。

谢谢。

评论后更新。

根据@Gustav 的回答设置 RowSource 后,为了 hide/unhide TextBox 控件根据 ComboBox 的值进行设置,您需要设置它们的 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

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

  1. 打开组合框的 属性 sheet,选项卡 Data
  2. 设置RowSourceType: 值列表
  3. 设置行源:"Year1";"Year2";"Year3"

要在子表单中显示所选值(字段名称),请使用此表达式:

=[Parent]![NameOfYourCombobox]

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

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

或 (?) 在子表单上:

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