0

我有一个列表框(如图),这个列表框不与 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
4

1 回答 1

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
于 2012-08-31T14:00:49.820 回答