如何将数据加载到多列列表框VBA VB6?

时间:2016-08-05 19:36:11

标签: vba loops listbox vb6

我正在尝试循环并逐行将数据加载到列表框中,我需要在多列中使用不同的数据。我一直在收到错误,但我不确定我做错了什么。

错误是:

  

“错误的参数数量或无效的属性赋值。”

它出现在第一个List1.List(R, Z)

Dim R As Integer
Dim Z As Integer
R = 0
List1.Clear

For i = 3 To 8
STATS = "dynamic data not a string"
If STATS = 0 Then GoTo nexti
NAMEE = "only a string"
REALNAMEE = "for this example"
CREATBY = "to avoid extra code"
CREATEDT = "but is dynamic"
EXT = "a 6th value"
Path = "a 7th dynamically loaded value"


Z = 0
'if we are here lets add item to list.
List1.List(R, Z) = STATS
     Z = Z + 1
          List1.List(R, Z) = NAMEE
     Z = Z + 1
          List1.List(R, Z) = REALNAMEE
     Z = Z + 1
          List1.List(R, Z) = CREATBY
     Z = Z + 1
          List1.List(R, Z) = CREATEDT
     Z = Z + 1
          List1.List(R, Z) = EXT
     Z = Z + 1
          List1.List(R, Z) = Path
     Z = Z + 1
    R = R + 1
nexti:
Next i

1 个答案:

答案 0 :(得分:1)

如果您真的想在VB6中使用ListBox来显示多个列,这是使用一个列的示例。我强烈推荐ListView,因为这基本上是一个适合您想要的软糖 - 只是为了证明它可以完成。

TAB 字符分隔每一列只有在用固定数量的字符格式化每一列时才能正确显示格式 - 但如果你真的想让它看起来不错,那就留给你添加。

我添加了第二个函数,向您展示如何从所选行中检索单个列。

Option Explicit

Const COLUMN_DELIMITER  As String = vbTab
Const NUM_COLUMNS       As Integer = 7

Private Sub Command1_Click()

    Dim intCol  As Integer
    Dim strRow  As String
    Dim varCols As Variant

    If List1.ListIndex <> -1 Then
        strRow = List1.List(List1.ListIndex)
        varCols = Split(strRow, COLUMN_DELIMITER)

        MsgBox "Check Immediate Window for Selected Columns"
        Debug.Print "SELECTED ROW INDEX: " & List1.ListIndex

        For intCol = 0 To UBound(varCols)
            Debug.Print varCols(intCol)
        Next intCol
    End If

End Sub

使用随机数据加载初始列表框:

Private Sub Form_Load()

    Const NUM_ROWS  As Integer = 10

    Dim intRow      As Integer
    Dim intCol      As Integer
    Dim strRow      As String

    List1.Clear

    ' Create Tab-Delimited List Box
    For intRow = 1 To NUM_ROWS

        ' Start of Row - add first column
        strRow = "Item " & intRow

        For intCol = 1 To NUM_COLUMNS - 1
            strRow = strRow & COLUMN_DELIMITER & "Col" & intCol + 1
        Next intCol

Debug.Print "Adding Row: " & strRow
        List1.AddItem strRow, intRow - 1
        strRow = ""

    Next intRow

End Sub

带有多个“列”的ListBox的屏幕截图: