我的问题与以下表格和表格有关。 image3和image4是相同的形式,它从Table_2(image2)加载数据。 Table_Setting(image1)用于定义哪个属性列应该在MainForm的SubForm中可见(image3和image 4)。这意味着子表单中的列是否可见是由用户在Table_Setting(image1)中定义的。例如,根据Table_Setting,如果MainForm中的BookType是“新颖的”,那么SubForm应该显示Author,Publisher,BookName;如果BookType是“课本”,则只显示Publisher,PublishYear。
image1:
image2:
image3:
image4:
我知道以下代码可以设置SubForm中的列是否可见。但这是硬编码版本。如果用户更新Table_Setting表,它就不够灵活。
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表自动设置列可见性,而不需要为SubForm中的每一列硬编码?因此,用户可以通过仅使用Table_Setting表轻松更改要显示的列。非常感谢。
更新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
答案 0 :(得分:1)
您应该尝试以下方法
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
。我相应调整了我的代码