我有一个Listbox(图),这个列表框没有绑定到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
答案 0 :(得分:0)
暂定的破解修复解决方案对我有用,直到我能够深入了解为什么没有从查询字段中提取列标题。
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, fldhd As String 'Store the Field Width pattern and Field Header Row
--> fldhd = "-1;-1;Category"
fldwd = "0"";0"";2.5""" '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"""
--> fldhd = fldhd & ";" & flds.Fields(fld).Name
Next
flds.Close: Set flds = Nothing
lstCategoryPG.ColumnHeads = True
lstCategoryPG.ColumnWidths = fldwd
sql = SQLSelect(cttbl, , ("tid = " & lstCatType.Value))
lstCategoryPG.Enabled = True
RefreshControl CurrentDb, lstCategoryPG, sql, , False
--> lstCategoryPG.AddItem fldhd, 0
'Application.Echo True 'Turn Screen Updating back on
End If