Image List must be initialized before it can be used.
GOod afternoon,
I am new to VB6 however relatively experienced in VBA.
I am attempting to load information into a ListView control via an Access DB. I have placed the following code into the form_load event.
However I receive an error on the highlighted line: "Image List must be initialized before it can be used".
When I initially added the listview, I right clicked on the control and selected properties. I have the view set as '3 - lvwReport'. I set up my 5 columns by going to the Column Headers tab and entering each. However when I go to Image Lists, all the drop downs show '<none>'
Thoughts?
Code:
Private Sub Form_Load()
Dim MyConn As ADODB.Connection
Set MyConn = New ADODB.Connection
MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Business Intelligence\VB6\Database9.mdb;"
MyConn.Open
Dim strSQL As String 'our query string
Dim oRS As ADODB.Recordset 'our recordset object
Dim lvwItem As ListItem 'this is necessary to directly reference the ListView control
Set oRS = New ADODB.Recordset
'change this SQL as appropriate for your needs
strSQL = "SELECT Date_Entered, Loan_Number, Investor, State, Doc_type FROM MailTeamTracker "
'change oConn to reflect the Connection object you are using in your program
oRS.Open strSQL, MyConn, adOpenForwardOnly, adLockReadOnly
'load the listview
Do While Not oRS.EOF
Set lvwItem = ListView1.ListItems.Add(, , oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyy")
lvwItem.SubItems(1) = oRS.Fields.Item("Loan_Number").Value
'change the date format as required
lvwItem.SubItems(2) = Format(oRS.Fields.Item("Investor").Value)
lvwItem.SubItems(3) = oRS.Fields.Item("State").Value
'change the date format as required
lvwItem.SubItems(4) = Format(oRS.Fields.Item("Doc_Type").Value)
oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing
End Sub
Re: Image List must be initialized before it can be used.
I ran into this once and it almost gave me a coronary. I'm so sorry you found this. Unfortunately, I don't recall how I resolved the issue, but a search of these forums found this solution:
Quote:
Originally Posted by
emyztik
The error, as you know, is "ImageList must be initialized before it can be used."
EDIT: Resolved by right clicking the listview, choosing "Properties", click on "Image Lists", and set all three combos to "ImageList".
Also, google search pointed me to a few threads that suggest solutions:
http://forums.devx.com/showthread.ph...lization-Error
http://vbcity.com/forums/t/112295.aspx
Unfortunately, this is the best I can do for you. Good luck!
Re: Image List must be initialized before it can be used.
I suspect you need
Code:
Set lvwItem = ListView1.ListItems.Add(, , Format(oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyy"))
Re: Image List must be initialized before it can be used.
you need to add an imagelist to your form... add some images to it... then point your ListView to the ImageList... then you should be able to add new lsit view items, and set the icon at the same time.
And there is Doogle's solution too...
-tg
Re: Image List must be initialized before it can be used.
Just to expand on my earlier thought. The 4th argument of the .Add method for a ListView Listitem is the Icon. OP has specified "mm/dd/yy" which led me to believe that they were really trying to format a Date rather than use an icon named "mm/dd/yy".
Re: Image List must be initialized before it can be used.
Please see below. Accidental double post.
Re: Image List must be initialized before it can be used.
THank you guys for your help.
I have been able to edit the code and it does pull in the values correctly now.
I read somewhere that with the report view, the image was optional, thats why I did not included it after the date.
I removed the area in red and it worked fine.
Code:
Do While Not oRS.EOF
Set lvwItem = ListView1.ListItems.Add(, , oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyy"))
lvwItem.SubItems(1) = oRS.Fields.Item("Loan_Number").Value
lvwItem.SubItems(2) = Format(oRS.Fields.Item("Investor").Value)
lvwItem.SubItems(3) = oRS.Fields.Item("State").Value
lvwItem.SubItems(4) = Format(oRS.Fields.Item("Doc_Type").Value)
oRS.MoveNext
Loop
As I said before. I am pretty fluent in VBA, but getting into VB im a bit lost in some places. Especially in the area of talking back and forth to the server. You'll be seeing alot of me! Thanks again!
Re: Image List must be initialized before it can be used.
This line should still fail...
Set lvwItem = ListView1.ListItems.Add(, , oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyy"))
just on parenthesis alone... you have 2 opening and 3 closing...
probably because you meant to use the FORMAT option, as Doogle was trying to point out...
-tg