hi....here is the modified code...just check this out....look for the
BOLDED lines....these are where i made some changes....watch
GREEN lines for comments....
the problem is within your listview manipulation code.....means when you are adding listitems.....you have mismatched total no. of columns and listitems.....total no. of columns in the listview should match with total no. of listitems you want to add....
the problem is you adding 5 listitems to the control where you are having only 3 columns....so that's why the error is coming....
just follow this code and hope you will solve ur problem...
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim listitem As listitem
this is for adding the columns to the lv control
Dim colx As Columnheader
this is for measuring column widths
Dim colWidth As Double
Private Sub Form_Load()
Set con = New ADODB.Connection
con.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=\\asfserver\itp$\Product_tabletest.mdb")
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
StrSql = "Select"
StrSql = StrSql & "[supplier_name],[type],[fax_no],[contact_person] from [Supplier]"
rs.Open StrSql, con
ListView1.View = lvwReport
checking the width of listview control and divided by 5...so that all columns become of same width...here we used 5 because the control will have 5 columns
colWidth = ListView1.Width / 5
here we are adding the columns
you need similar no. of columns as much no. of listitems you want to add to the listview. now if you want to hide any column from displaying, just set its width to 0.
With ListView1
.ColumnHeaders.Clear
Set colx = .ColumnHeaders.ADD(, , ("Supplier No"), colWidth)
Set colx = .ColumnHeaders.ADD(, , ("Supplier Name"), colWidth)
Set colx = .ColumnHeaders.ADD(, , ("Type"), colWidth)
Set colx = .ColumnHeaders.ADD(, , ("Fax No"), colWidth)
Set colx = .ColumnHeaders.ADD(, , ("Contact Person"), colWidth)
End With
adding rows to listview
With ListView1
.ListItems.Clear
If rs.RecordCount > 0 Then
rs.MoveFirst
While Not rs.EOF()
Set listitem = .ListItems.ADD(, , (supplier_no))
listitem.SubItems(1) = rs!supplier_name
listitem.SubItems(2) = rs!Type
listitem.SubItems(3) = IIf(IsNull(rs!fax_no), "", rs!fax_no)
listitem.SubItems(4) = rs!contact_person
rs.MoveNext
Wend
End If
End With
closing the recordset so the memory can be utilized again and also releasing its current instance from the memory
If rs.state = adStateOpen Then rs.Close
Set rs = Nothing
End Sub
Hope this helps.....please make a feedback here...
regards
Shouvik