VB6 ListView not behaving properly

I have a program that works on another computer.  Basically I want to populate a list view and the code is below.  On this computer, I get a Run-time error 380, Invalid property value on the "itmVar.SubItems(1)="DEF" line.  I finished the program on the other computer but now need to use a list view on the computer that's having the problem.  One more tidbit:  This morning I decided to repair VB6 from the CD, and right after I did that, it worked.  I did some other things (non-coding), and then a couple hours later came back to the exact same project, ran it, and got the error again.  I then repaired (no success), uninstalled and reinstalled (still no success).  If anyone has any suggestions, I'd very much appreciate it.  One more thing:  I'm using MS WIndows Common Controls 6.0 (SP6), which is pointing to MSCOMCTL.OCX.

Thanks Experts :-)
Dim itmVar As ListItem
        Set itmVar = Me.lvwClassCode.ListItems.Add()
        itmVar.Text = "ABC"
        itmVar.SubItems(1) = "DEF"

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

To me it is absolutely unclear what you want to do here. At first - you can't fix coding error (like runtime error) by re-installing VB or so, runtime error is about always a logical error in your program, it is EXTREMELY unlikely you will fix your source by re-installing VB in any case.

Then this line is a bit odd:

Set itmVar = Me.lvwClassCode.ListItems.Add()

i assume itmVar is some temporary list or so, then you would assume someone assigns an item dimed as a listItem to another list item, not to the "Add" function. To really help you with this line one would need to know what lvwClassCode is and what you want to do with it. But i could be tempted to assume the "Add()" just slipped in here? or did you just mean it vice versa - is ListItems.Add() a function to import list items from a list? Then you would need to declare it rather like Me.lvwClassCode.ListItems.Add(itmVar), but in this example it is obvious this makes no sense as itmVar is empty - it has just been declared in the line above.

The error itself - runtime error 380 - actually just meaned you called a property that doesn't exist. here it is itmVar.SubItems. Subitems is simply not a meber of ListItem. I am really unsure what you think subItem is, but subItem is not available in ListItem. Did you mean ListViewItem?
jruheAuthor Commented:
The reason I reinstalled VB is because it was intermittently working with the same code.

lvwClassCode is the name of a List View control manually added on my form.

You're partially right -- It's hanging on subitems, but not in the way you may be thinking. . .  Subitems (when using the immediate window after the error occurs) is actually a property off of itmVar (using intellisense, it is a choice from the dropdown).  However, when typing in something like (? itmVar.SubItems(1)) it gives me the same error message.


Set itmVar = Me.lvwClassCode.ListItems.Add(, , "ABC")
itmVar.SubItems(1) = "DEF"
Ensure Business Longevity with As-A-Service

Using the as-a-service approach for your business model allows you to grow your revenue stream with new practice areas, without forcing you to part ways with existing clients just because they don’t fit the mold of your new service offerings.

jruheAuthor Commented:
No--still got the same message.  Thanks for trying though!  Does anyone have an alternative (Microsoft) control that may be as good as or better than List View?  Basically, I want to store data in columns and rows, write some code so that when the user clicks on a column heading, it'll sort by that column.  

It's very frustrating because it works on another computer that I can't use right now--that's why I know it's not the code.  It has to be something with the ocx file, a version of the patch, something happening in the memory that wasn't happening yesterday morning (recall that after I ran the repair yesterday morning, it worked once.  then, I did other things (not with list view--just couldn't get back to it) and then it failed about 3 hours later when I revisited it).

Keep the ideas coming please!

do you define any column header?

Also set the view to 3-lvwReprt from property
jruheAuthor Commented:
Keep in mind that the program works on any other computer except this one, and it worked on this one yesterday morning and now it doesn't (although no changes were made to the code).  Therefore, I don't think it's the code.  I think it's the OCX or a version issue or some kind of conflict.  Any thoughts on that?

(In answer to your question, column headers are defined and the view property is set to 3--i tried to keep the sample code as simple as possible to show that even the simplest of commands doesn't work)
I doubt that there is a problem with the control.
Try this code:

Option Explicit
Private Sub Form_Load()
   Dim itmVar  As ListItem
   lvwClassCode.View = lvwReport
   lvwClassCode.ColumnHeaders.Add , , "MainItem"
   lvwClassCode.ColumnHeaders.Add , , "SubItem 1"
   Set itmVar = Me.lvwClassCode.ListItems.Add(, , "ABC")
   itmVar.SubItems(1) = "DEF"
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jruheAuthor Commented:
EDDYKT & danaseaman--I've got another fire right now so I need to put this on the back burner 'til tomorrow.  I'll try both of your suggestions then (dana's first, then EDD's, to be sure that one doesn't fix the other).
jruheAuthor Commented:
To Dana--to my happy surprise, your solution worked.  Obviously the program that I am using has a lot more to it than what I showed, including a ResetData and a RefreshData sub, as well as other subroutines.  

Here's the thing:  Same code, different computer--doesn't work.  AND, on the bad computer, two days ago it worked for about 5 minutes (and I didn't change any code) after I did a repair on the VB Install.  So I went ahead to the site that EDDYKT provided.  I downloaded the msi file and am getting "Could not access network location Tools\VB\Controls\Controls_Backup."  So I'm going to stick with Dana's solution, but I'm going to give EDDY points as well because I think the solution will work into the future.  

Before I close this issue:  Dana (or anyone):  Do you have any idea why it would have:
1. Not worked as coded on the laptop but would work on another machine
2. Worked after I ran the repair install on VB (no modifications to the code)
3. Stopped working about 3 hours later (no modifications to the code)
4. Continues working on all other machines it's run on except this one

Thanks all.  Will close the issue today.

My best guess is that MsComCtl.ocx is OK and something in your code is resetting/removing your columns.
You can test this in your application by adding this line of code before you add subitems:

Debug.Print "ColumnHeaders Count", lvwClassCode.ColumnHeaders.Count

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.