如何在VBA Access 2010中的“最适合”中设置所有列宽度

时间:2016-11-16 16:17:23

标签: vba ms-access-2010

我知道设置列宽最适合具有“标题”名称的字段,以下代码有效:

Private Sub Form_Load()

Forms![Contacts]![Title].ColumnWidth = -2

End sub

数据表视图中的所有列的解决方案是一行还是两行VBA代码?

非常感谢...

1 个答案:

答案 0 :(得分:0)

Dim f As Object
Set f = Forms![Contacts]
' Or Set f = Me.Form

Dim c As Control
For Each c In f.Controls

    With c

        ' If textbox
        If .ControlType = acTextBox Then

            ' Adjust to best fit width
            .ColumnWidth = -2
        End If
    End With
Next c

Set c = Nothing
Set f = Nothing