Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB6: Saving ListView Data

Posted on 1999-07-02
9
Medium Priority
?
3,277 Views
Last Modified: 2008-03-10
Situation--
Transferring data between two VB forms within one VB project:
FormA contains a drop-down list box, while FormB contains a ListView control.  The user selects one item from the list box (FormA), and then presses a command button (on FormA) sending the item to the ListView box (FormB). The user needs to save the data within the ListView box on FormB for retrieval at a later date.

Question:
What is the code for saving the data within a ListView box?
 
0
Comment
Question by:admiller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 2

Expert Comment

by:Torus
ID: 1521647
the traditional method is to write the ListView content to the text file for later retrieval. I am not sure if you prefer this method.

Dim iFileNum as integer
Dim sData as string
Dim I as integer
Dim J as integer
iFileNum = Freefile
Open "YourText.text" for append as #iFilenum
For I = 0 to ListView.ListItems.count - 1
       sData = ListView.ListItems(I).text & ","
       For J = 2 to ListView.ListItems.ColumnHeaders.count
            sData = sData & ListView.ListItems(I).subitems(J-1) & ","
       Next J
       Print #iFileNum, sData
Next I  
Close #iFilenum

when you want to retrieve, just read the text file by Line Input and then
parse the string using split method. Then put back to the ListView.



0
 

Expert Comment

by:chittamuru
ID: 1521648
Write the following code in the commandbutton's click event.
FormB.ListView1.AddItem(FormA.Listbox1.Text)
You may get an error if formB is not loaded by this time. In that case first write the selected item to a text file(Write code for this in the command buttons click event). Then read this value from the same text file and add this item to the listview control of formB by writing code in the activate/load event of formB. (you can use FileSystem Object for writing and/or reading to and/or from a text file). Hope this will solve your question.

0
 

Author Comment

by:admiller
ID: 1521649
Re: Answer

Thank you for your help.  Unfortunately, it doesn't compile.

When using FormB.ListView1.AddItem(FormAQ.ListBox1.Text), an error message occurs: "Method or data member not found."  In addition, AddItem is not on the list of reserved words.

I'll re-phrase my question:
What is the complete code for saving the contents of a ListView control to file?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:Torus
ID: 1521650
Have you tried the following method?

      Dim iFileNum as integer
      Dim sData as string
      Dim I as integer
      Dim J as integer
      iFileNum = Freefile
      Open "YourText.text" for append as #iFilenum
      For I = 0 to ListView1.ListItems.count - 1
             sData = ListView1.ListItems(I).text & ","
             For J = 2 to ListView1.ListItems.ColumnHeaders.count
                  sData = sData & ListView1.ListItems(I).subitems(J-1) & ","
             Next J
             Print #iFileNum, sData
      Next I  
      Close #iFilenum

      when you want to retrieve, just read the text file by Line Input and then
      parse the string using split method. Then put back to the ListView.
0
 
LVL 2

Expert Comment

by:Torus
ID: 1521651
Some amendments. Because the first item index of the ListView is 1

It should be
  For I = 1 to ListView1.ListItems.count

0
 

Author Comment

by:admiller
ID: 1521652
Could you provide a detailed exmple?
0
 
LVL 2

Expert Comment

by:Torus
ID: 1521653
hmm... What did you  mean a detail example?
The above code I provided is to save your data in the ListView control.
It finds out how many items in your ListView and then write each item to a file
line by line.

I'm afraid you need to tell me how detail you want to know.



0
 
LVL 2

Accepted Solution

by:
Torus earned 150 total points
ID: 1521654
OK. Give you an example. Hope that it is what you want
Assume that the ListView control has 4 columns and have 3 items. See
following

Name       Address        Telephone     Age
A                US              1234567       20
B                US              4567890       21
C                UK              234567         30

In this case ListView1.Listitems.count will be 3
For I = 1 to ListView1.ListItems.count is to loop all the items

sData = ListView1.ListItems(I).text & ","
This sentence is to get the data of the first column of Items I, i.e
I = 1, sData = "A," .....

For J = 2 to ListView1.ListItems.ColumnHeaders.count
      sData = sData & ListView1.ListItems(I).subitems(J-1) & ","
Next J

The above is to get the remain column data. Because the first column data
is got by sData = ListView1.ListItems(I).text & "," , it starts from column
2 and the ListView1.Listitems.ColumnHeaders.count is 4 in this case.
 
Because the subitems index starts from 1 in the second column data, it should
be subitems(J-1).  Then append all the subitems data to sData variable.
In this case, when I = 1
sData = A,US,1234567,20,

when I = 2
sData = B,US,4567890,21,

when I = 3
sData = C, UK,23456,30,

And what you will see in the file will be
A,US,1234567,20,
B,US,4567890,21,
C,UK,23456,30,
0
 

Expert Comment

by:garylake
ID: 3882352
ListView1.Listitems.ColumnHeaders.count

should be:

Listview1.ColumnHeaders.Count
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question