无需硬编码即可访问 VBA 设置来自其他 table 的表单列可见性

Access VBA set form column visibility from other table without hard coding

我的问题与以下 table 和表格有关。 image3 和 image4 是相同的表单,从 Table_2(image2) 加载数据。 Table_Setting(image1) 用于定义在MainForm(image3和image 4)的子窗体中哪些属性列应该是可见的。这意味着子表单中的列是否可见由用户在 Table_Setting(image1) 中定义。比如根据Table_Setting,如果MainForm中的BookType是"novel",那么SubForm应该显示Author,Publisher,BookName;如果 BookType 是 "text book",只显示 Publisher,PublishYear.

图片1:


图片2:


image3:


image4:


我知道下面的代码可以设置子窗体中的列是否可见。但这是硬代码版本。如果用户更新 Table_Setting table,则不够灵活。

Private Sub Form_Load()
    Select Case Forms![SubForm]!BookType
        Case "novel"
            Me.BookType.Visible = True
            Me.Author.Visible = True
            Me.Publisher.Visible = True
            Me.BookName.Visible = True
            Me.PublishYear.Visible = False
        Case "research"
            Me.BookType.Visible = True
            Me.Author.Visible = False
            Me.Publisher.Visible = False
            Me.BookName.Visible = False
            Me.PublishYear.Visible = True           
        Case "text book"    
            Me.BookType.Visible = True
            Me.Author.Visible = Falss
            Me.Publisher.Visible = True
            Me.BookName.Visible = False
            Me.PublishYear.Visible = True                   
    End Select
End Sub

我的问题:
我的问题是:是否可以编写一些代码来简单地根据 Table_Setting table 自动设置列可见性,而无需为 SubForm 中的每一列进行硬编码?因此,用户可以仅通过更新 Table_Setting table 轻松更改要显示的列。非常感谢。

更新1:
我 运行 MainForm 中的以下代码。

Private Sub Form_Load()

    Dim RST As Recordset
    Dim strBookType As String
    Dim strSQL As String

    strBookType = Me.BookType

    ' Set visible controls
    strSQL = "SELECT Attribute FROM Table_Setting WHERE BookType = '" & strBookType & "'"
    Set RST = CurrentDb.OpenRecordset(strSQL)
    If Not RST.BOF Then
        While Not RST.EOF
            Me!Table_2_DataSheet.Form.Controls(RST!Attribute).Visible = True 'Table_2_DataSheet is the subform name
            RST.MoveNext
        Wend
    End If
    RST.Close

    ' Set invisible controls
    strSQL = "SELECT DISTINCT(Attribute) FROM Table_Setting WHERE Attribute NOT IN (SELECT Attribute FROM Table_Setting WHERE BookType='" & strBookType & "')"
    Set RST = CurrentDb.OpenRecordset(strSQL)
    If Not RST.BOF Then
        While Not RST.EOF
            Me!Table_2_DataSheet.Form.Controls(RST!Attribute).Visible = False 'Table_2_DataSheet is the subform name
            RST.MoveNext
        Wend
    End If
    RST.Close

    Set RST = Nothing

End Sub



更新2:
我运行在SubForm.

下面的代码
Private Sub Form_Load()

    Dim RST As Recordset
    Dim strBookType As String
    Dim strSQL As String

    strBookType = Me.Parent.BookType

    ' Set visible controls
    strSQL = "SELECT Attribute FROM Table_Setting WHERE BookType = '" & strBookType & "'"
    Set RST = CurrentDb.OpenRecordset(strSQL)
    If Not RST.BOF Then
        While Not RST.EOF
            Me.Controls(RST!Attribute).Visible = True
            RST.MoveNext
        Wend
    End If
    RST.Close

    ' Set invisible controls
    strSQL = "SELECT DISTINCT(Attribute) FROM Table_Setting WHERE Attribute NOT IN (SELECT Attribute FROM Table_Setting WHERE BookType='" & strBookType & "')"
    Set RST = CurrentDb.OpenRecordset(strSQL)
    If Not RST.BOF Then
        While Not RST.EOF
            Me.Controls(RST!Attribute).Visible = False
            RST.MoveNext
        Wend
    End If
    RST.Close

    Set RST = Nothing

End Sub

您应该尝试以下方法

Private Sub Form_Load()

    Dim RST As Recordset
    Dim strBookType As String
    Dim strSQL as string
    Dim ctrl As Control

    strBookType = Forms![SubForm]!BookType

    ' Set visible controls
    strSQL = "SELECT Attribute FROM Table_Setting WHERE BookType='" & strBookType & "'"
    Set RST = CurrentDb.OpenRecordset(strSQL)
    If Not RST.BOF Then
        While Not RST.EOF
            Set ctrl = Me.Controls(RST!Attribute)
            ctrl.ColumnHidden = False
            RST.MoveNext
        Wend
    End If
    RST.Close

    ' Set invisible controls
    strSQL = "SELECT DISTINCT(Attribute) FROM Table_Setting WHERE Attribute NOT IN (SELECT Attribute FROM Table_Setting WHERE BookType='" & strBookType & "')"
    Set RST = CurrentDb.OpenRecordset(strSQL)
    If Not RST.BOF Then
        While Not RST.EOF
            Set ctrl = Me.Controls(RST!Attribute)
            ctrl.ColumnHidden = True
            RST.MoveNext
        Wend
    End If
    RST.Close

    Set RST = Nothing


End Sub

想法是检索属性,在它们上循环,并使用 Me.Controls(attribute).ColumnHidden

设置表单上具有属性名称的控件是否可见

编辑:

乍一看我没有意识到您试图隐藏数据表中的列。您不能为此使用 Visible 属性,您必须使用 ColumnHidden。我相应地调整了我的代码