私はリストボックスを持っています(図を参照)。このリストボックスはRecordSourceに関連付けられておらず、エンドユーザーによって他のコントロールの選択に基づいて動的に構築されます。これまで私はこれに関してあまり問題を抱えていませんでしたが、この現在の動的クエリの状況では、列は完全に一定ではありません。使用されているRecordSetはCrossTabクエリであるため、CrossTabクエリの4分の1には、5列、次の8列、および次の3列が含まれる場合があります。
私が実装したリストボックスのほとんどには、予測できる静的な量の列がありますが、この状況では、列の数を一貫して予測することはできません。
ColumnHeads
プロパティをに設定したYes
ので、問題はありません。設定した操作ColumnHeads
の前に、VBAでプロパティをリセットすることもできます。AddItem
問題のリストボックス(lstCategoryPG)内/周辺で操作するために使用しているロジック:
If lstCatType.ListIndex >= 0 Then
'Application.Echo False 'Turn off Screen Updating
Dim crt As String: crt = "crt_CategoryPG" 'Cross-Tab Query
Dim cttbl As String: cttbl = CreateCTTable(crt) 'Create Table to store the Cross-Tab information
Dim sql As String: sql = SQLSelect(cttbl)
Dim flds As DAO.Recordset: Set flds = CurrentDb.OpenRecordset(sql)
Dim fldwd As String 'Store the Field Width pattern
fldwd = "0"";0"";2""" 'Handles `tid` and `cid` columns in the ListBox
'Assign the number of columns based on the number of fields in CTtable
lstCategoryPG.ColumnCount = flds.Fields.Count
Dim fld As Long
For fld = 3 To (flds.Fields.Count - 1)
fldwd = fldwd & ";.75"""
Next
flds.Close: Set flds = Nothing
lstCategoryPG.ColumnWidths = fldwd
sql = SQLSelect(cttbl, , ("tid = " & lstCatType.Value))
lstCategoryPG.Enabled = True
lstCategoryPG.ColumnHeads = True
RefreshControl CurrentDb, lstCategoryPG, sql, , False
'Application.Echo True 'Turn Screen Updating back on
End If