ListView1 AddItem Errors

jstevens
GoldLounger
Posts: 2617
Joined: 26 Jan 2010, 16:31
Location: Southern California

ListView1 AddItem Errors

Post by jstevens »

I am encountering an error adding items to a ListView. The error message is: Property is write-only.

I have attached sample workbooks. Both workbooks should be open in memory.
Workbook1 contains the main VBA code.
Workbook2 contains a UserForm and VBA code to show the form.

The bit of code where I'm encountering the error:

Code: Select all

    ' Add an item to the ListView control
        Dim newItem As Object
        Set newItem = targetListView.ListItems.Add(1, , "File")   'Errors: Property is write-only
        newItem.SubItems(1) = "05/26/2016"
        newItem.SubItems(2) = "Some Description"
        newItem.SubItems(3) = "100.00"
Your suggestions are appreciated.
Workbook1.xlsm
Workbook2.xlsm
You do not have the required permissions to view the files attached to this post.
Regards,
John

User avatar
HansV
Administrator
Posts: 78231
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ListView1 AddItem Errors

Post by HansV »

You call

Code: Select all

    targetWorkbook.Application.Run "'Workbook2.xlsm'!UserForm1_Show"
This line opens the userform modally, i.e. code execution stops until the user closes the userform.

Next, you refer to the design of the userform:

Code: Select all

    Set targetUserForm = targetWorkbook.VBProject.VBComponents("UserForm1").Designer
But you cannot use AddItem in design view, only in runtime.

You should populate the listview control in the UserForm_Initialize event of the userform.
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2617
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: ListView1 AddItem Errors

Post by jstevens »

Hans,

Thanks for the suggestion. I got it to work.
Regards,
John