'==================================================================
' Purpose: to fill a ListView control with data from a table or
' query
' Returns: 1 to indicate if the function was successful
' 0to indicate if the function was un-successful
' From: http://support.microsoft.com/kb/q155178/
' Edit: Date 14/08/2007 by levanduyet@yahoo.com
'==================================================================
'
Dim intTotCount As Integer
Dim intCount1 As Integer, intCount2 As Integer, intAutosize As Integer
Dim colNew As ColumnHeader, NewLine As ListItem
On Error GoTo FillListviewFrRecordset_Error
If TypeName(objListview) <> "ListView" Then
FillListviewFrRecordset = 0
Exit Function
End If
'Setup the ListView
With objListview
.View = lvwReport '3 Set View property to Report
.FullRowSelect = True
.MultiSelect = False
.Gridlines = True
.LabelEdit = lvwManual
.HideColumnHeaders = False
End With
' Clear the ListView control.
objListview.ListItems.Clear
objListview.ColumnHeaders.Clear
' Set Column Headers.
For intCount1 = 0 To rs.Fields.Count - 1
Set colNew = objListview.ColumnHeaders.ADD(, , rs(intCount1).Name)
Next intCount1
' Set Total Records Counter.
rs.MoveLast
intTotCount = rs.RecordCount
rs.MoveFirst
' Loop through recordset and add Items to the control.
For intCount1 = 1 To intTotCount
Set NewLine = objListview.ListItems.ADD(, , rs(0).Value)
For intCount2 = 1 To rs.Fields.Count - 1
NewLine.SubItems(intCount2) = rs(intCount2).Value
Next intCount2
rs.MoveNext
Next intCount1
If bAutoSize = True Then
intAutosize = LvAutosizeCols(objListview)
End If
FillListviewFrRecordset = 1
Exit Function
FillListviewFrRecordset_Error:
FillListviewFrRecordset = 0
' Ignore Error 94 which indicates you passed a NULL value.
If Err = 94 Then
Resume Next
Else
' Otherwise display the error message.
MsgBox "Error: " & Err.Number & Chr(13) & _
Chr(10) & Err.Description
End If
End Function