2016-08-05 68 views
0

我想循环并逐行加载数据到列表框中,我需要多列中的不同数据。我不断收到错误,但我不确定自己做错了什么。如何将数据加载到多列列表框VBA 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 
+4

感觉就像土拨鼠日。不知道我想不断地看着这个问题一遍又一遍,但这里...看起来你做了一些改变,我建议在我的最后一篇文章 - 但现在你已经删除了'List'.AddItem'方法,你绝对需要新的一排。就像我以前所问 - LINE给你的错误。你定义列表框的列是7吗?当你将它用作字符串时,为什么要比较STATS = 0?如果这是VB6而不是VBA,请确保您所示的多列示例符合您的环境。 – dbmitch

+0

我相信ListView更适合你想要做的事情。但是人们使用来分隔列表控件中的列 - 如果你敢走这条路线,下面是一个例子http://www.vbforums.com/showthread.php?350118-RESOLVED-Columns-property-of-List- box-How-to-use – dbmitch

+0

这不是土拨鼠日,我删除了最后一个问题,并且创建了一个新的 “遵守”规则。我看到我仍然得到一个否定的投票,不知道为什么 –

回答

1

如果你真的想用一个列表框在VB6中显示多列下面是一个使用中的一个例子。我强烈推荐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 

与多个列表框的屏幕截图 “列”: