VB6使用adodb记录集填充列表框

时间:2017-11-07 03:16:56

标签: listbox vb6 populate

我有一个最初使用Listview控件的表单,但我需要将其更改为Listbox控件。现在这是用于listview的原始代码。我正在尝试使用adodb记录集将其更改为填充列表框,但不断出现错误。 lv1是listbox1; lv2是listbox2。列表框没有listitem。

Private Sub PopulateListView()
Dim cnPop As ADODB.Connection
Dim rsPop As ADODB.Recordset
Dim lst As ListItem
Dim lngRecs As Long

On Error GoTo ErrPopulate

Set cnPop = New ADODB.Connection
cnPop.CursorLocation = adUseClient
cnPop.Open gcnORA

Set rsPop = New ADODB.Recordset
rsPop.Open sSQL, cnPop, adOpenDynamic, adLockReadOnly

lv1.ListItems.Clear
Do While rsPop.EOF = False
    Set lst = lv1.ListItems.Add(, , rsPop!Customer_Number)
    lst.SubItems(1) = rsPop!Customer_Name
    rsPop.MoveNext
Loop

If rsPop.RecordCount > 0 Then
    SelectButtons True
End If

With lblCount
    .Caption = Format((rsPop.RecordCount), "#,##0") & IIf((rsPop.RecordCount) <= 1, " Customer", " Customers") & " found"
    .Refresh
End With

rsPop.Close
cnPop.Close
Set rsPop = Nothing
Set cnPop = Nothing

Exit Sub

ErrPopulate:
MsgBox Err.Description, vbCritical, "Populate Error"

End Sub

1 个答案:

答案 0 :(得分:1)

列表视图可以有多个列,您在上一个列表视图中显然至少有两列:客户编号和客户名称。

列表框只有一列。您需要决定如何在列表框中显示/添加两列数据。不要被列表框的columns属性混淆,当设置为多列时,控件仍然只有一列数据 - 它只是水平地蛇形和滚动而不是垂直滚动。

将项目添加到列表框:

lv1.Additem rsPop!Customer_Name

列表框文档:VB6 Listbox