我有一个按钮,它调用一个宏来打开一个表。我想在该表打开时隐藏该列。宏打开表tChart1_Reportable,我想隐藏ID列。
我认为必须在VBA中通过在宏中选择RunCode操作来完成此操作。
我没有运气找到有效的代码。在网上找到了这个,但是它不能以当前形式使用:
Public Sub SetColumnHidden()
Dim dbs As DAO.Database
Dim fld As DAO.Field
Dim prp As DAO.Property
Const conErrPropertyNotFound = 3270
' Turn off error trapping.
On Error Resume Next
Set dbs = CurrentDb
' Set field property.
Set fld = dbs.TableDefs!tChart1_Reportable.Fields!ID
fld.Properties("ColumnHidden") = True
' Error may have occurred when value was set.
If Err.Number <> 0 Then
If Err.Number <> conErrPropertyNotFound Then
On Error GoTo 0
MsgBox "Couldn't set property 'ColumnHidden' " &; _
"on field '" &; fld.Name &; "'", vbCritical
Else
On Error GoTo 0
Set prp = fld.CreateProperty("ColumnHidden", dbLong, True)
fld.Properties.Append prp
End If
End If
Set prp = Nothing
Set fld = Nothing
Set dbs = Nothing
End Sub
答案 0 :(得分:0)
尽管您确实应该考虑使用查询来实现此目标,但是此代码的问题绝对是表本身在执行时不会刷新隐藏属性。
话虽如此,这可能足以解决此问题:
Public Sub SetColumnHidden()
Dim dbs As DAO.Database
Dim fld As DAO.Field
Dim prp As DAO.Property
Set dbs = CurrentDb
'Set field property.
Set fld = dbs.TableDefs!tChart1_Reportable.Fields!ID
fld.Properties("ColumnHidden") = True
DoCmd.Close acTable, "tChart1_Reportable"
DoCmd.OpenTable "tChart1_Reportable"
Set prp = Nothing
Set fld = Nothing
Set dbs = Nothing
End Sub
要通过查询进行操作,请使用Query Design
创建一个新查询,并在SQL视图中将其打开。您的查询应如下所示:
SELECT [Field1],
[Field2],
[Field3]
FROM tChart1_Reportable
基本上,添加要查看的每个字段(按字段名称),从列表中删除“ ID”字段。这将完成您尝试做的事情。至于为什么代码对您不起作用,我只能说应该如此。