Private Sub Form_Load()
Dim clmX As ColumnHeader
Dim itmX As ListItem
Dim i As Integer
For i = 1 To 3
Set clmX = ListView1.ColumnHeaders.Add()
clmX.Text = "Col" & i
Next i
'添加 10 个具有相同图标的项目到列表中
For i = 1 To 10
Set itmX = ListView1.ListItems.Add()
itmX.SmallIcon = 1
itmX.Text = "ListItem " & i
itmX.SubItems(1) = "Subitem 1"
itmX.SubItems(2) = "Subitem 2"
Next i
End Sub
' Set View property to Report.
ListView1.View = lvwReport
'为数据访问对象声明对象变量。
Dim myDb As Database, myRs As Recordset
'设置 Database 为 BIBLIO.MDB 数据库。
' IMPORTANT: the Biblio.mdb must be on your
' machine, and you must set the correct path to
' the file in the OpenDatabase function below.
Set myDb = DBEngine.Workspaces(0) _
.OpenDatabase("c:\Program Files\VB\BIBLIO.MDB")
'设置 recordset 为 "Authors" 表。
Set myRs = _
myDb.OpenRecordset("Authors", dbOpenDynaset)
While Not myRs.EOF
Set itmX = ListView1.ListItems. _
Add(, , CStr(myRs!Author),1) 'Author 字段。
'若 AuthorID 字段不为空,则将 SubItem 1 设置为此字段。
If Not IsNull(myRs!Au_id) Then
itmX.SubItems(1) = CStr(myRs!Au_id) ' Author ID。
End If
'若 birth 字段不为空,则将 SubItem 2 设置为此字段。
If Not IsNull(myRs![Year Born]) Then
itmX.SubItems(2) = myRs![Year Born]
End If
myRs.MoveNext '移动到下一条记录。
Wend
End Sub