これはおそらく非常に基本的なことですが、私はこれらのことにまったく慣れていないことを覚えておいてください.
次のことを行う Access データシート フォームの手順に取り組んでいます。
- コンテンツに合わせて各列の幅を調整する
- すべての列の合計幅を合計し、ウィンドウの幅のサイズから引きます
- 残りのスペースに合わせて列の 1 つの幅を調整する
これは、コンテンツに合わせて各列の幅を調整するコードです (正常に動作します)。
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