2016-08-05 66 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 

與多個列表框的屏幕截圖 「列」: