我有一个列表框(如图),这个列表框不与 RecordSource 绑定,而是由最终用户根据其他控件选择动态构建的。在过去,我对此并没有太大的问题,但是在当前的动态查询情况下,列并不是绝对不变的。由于正在使用的 RecordSet 是 CrossTab 查询,因此四分之一的 CrossTab 查询可能有 5 列,接下来的 8 列和接下来的 3 列。
我实现的大多数 ListBox 都有我可以预测的静态列数,但在这种情况下,我无法一致地预测列数。
我已将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