这可能是非常基本的东西,但请记住我对这些东西完全陌生。
我正在为我的 Access 数据表表单制定一个程序,该程序将:
- 调整每列的宽度以适合内容
- 将所有列的总宽度相加,然后从窗口宽度的大小中减去
- 调整其中一列的宽度以适应剩余空间
这是调整每列宽度以适应内容的代码(效果很好):
Dim Ctrl As Control
Dim Path As String
Dim ClmWidth As Integer
'Adjust column width to fit content
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is TextBox Then
Path = Ctrl.Name
Me(Path).ColumnWidth = -2
End If
Next Ctrl
我应该如何编写代码以便获得所有列的总宽度?
非常感谢!
斯特凡
解决方案
这是使 Access 数据表从此开始的代码:
替代文字 http://bayimg.com/image/galphaacp.jpg
对此:
替代文字 http://bayimg.com/image/galpnaacp.jpg
Sub AdjustColumnWidth(frm As Form, clm As String)
On Error GoTo HandleError
Dim intWindowWidth As Integer ' Window width property
Dim ctrl As Control ' Control
Dim intCtrlWidth As Integer ' Control width property
Dim intCtrlSum As Integer ' Control width property sum
Dim intCtrlAdj As Integer ' Control width property remaining after substracted intCtrSum
'Adjust column width to standard width
For Each ctrl In frm.Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is CheckBox Or TypeOf ctrl Is ComboBox Then
Path = ctrl.Name
frm(Path).ColumnWidth = 1500
End If
Next ctrl
'Get total column width
For Each ctrl In frm.Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is CheckBox Or TypeOf ctrl Is ComboBox Then
Path = ctrl.Name
intCtrlWidth = frm(Path).ColumnWidth
If Path <> clm Then
intCtrlSum = intCtrlSum + intCtrlWidth
End If
End If
Next ctrl
'Adjust column to fit window
intWindowWidth = frm.WindowWidth - 270
intCtrlAdj = intWindowWidth - intCtrlSum
frm.Width = intWindowWidth
frm(clm).ColumnWidth = intCtrlAdj
Debug.Print "Totalt (Ctrl): " & intCtrlSum
Debug.Print "Totalt (Window): " & intWindowWidth
Debug.Print "Totalt (Remaining): " & intCtrlAdj
Debug.Print "clm : " & clm
HandleError:
GeneralErrorHandler Err.Number, Err.Description
Exit Sub
End Sub
调用程序的代码:
Private Sub Form_Load()
Call AdjustColumnWidth(Me, "txtDescription")
End Sub