Hi there folks. I am working on a program to test students with their social studies, and I am collecting their data with a listview, and would like to save it in a textfile. I have search the forum, and found numerous examples of code.

I found a recent post that uses a module and some sub calls to save a listview to a text file, and was wondering how to modify it for my needs.

The code for the module is ..
VB Code:
  1. Option Explicit
  2.  
  3. Public Function ListViewSaveToTextFile(Lvw As ListView, FileName As String) As Boolean
  4.  
  5.    Dim Li As ListItem
  6.    Dim i As Long
  7.    Dim j As Long
  8.    Dim s() As String
  9.    Dim FNr As Integer
  10.    
  11.       On Error GoTo Fehler
  12.       FNr = FreeFile
  13.       Open FileName For Output As #FNr
  14.      
  15.       For i = 1 To Lvw.ListItems.Count
  16.          Set Li = Lvw.ListItems(i)
  17.          ReDim s(Li.ListSubItems.Count)
  18.          s(0) = Li.Text
  19.          For j = 1 To Li.ListSubItems.Count
  20.             s(j) = Li.SubItems(j)
  21.          Next
  22.          If i < Lvw.ListItems.Count Then
  23.             Print #FNr, Join(s, Chr(9))
  24.          Else
  25.             Print #FNr, Join(s, Chr(9));
  26.          End If
  27.          Set Li = Nothing
  28.       Next
  29.       Close #FNr
  30.       ListViewSaveToTextFile = True
  31.       Exit Function
  32.      
  33. Fehler:
  34.       Set Li = Nothing
  35.       MsgBox "Fehler: " & Err.Number & vbCrLf & _
  36.              Err.Description, vbCritical
  37. End Function
  38.  
  39.  
  40. Public Function ListViewFillFromTextFile(Lvw As ListView, _
  41.                                 FileName As String) As Boolean
  42.  
  43.    Dim Li As ListItem
  44.    Dim s As String
  45.    Dim s1() As String
  46.    Dim s2() As String
  47.    Dim i As Long
  48.    Dim j As Long
  49.    Dim FNr As Integer
  50.      
  51.       On Error GoTo Fehler
  52.       FNr = FreeFile
  53.       Open FileName For Binary As #FNr
  54.       s = Space(LOF(FNr))
  55.       Get #FNr, , s
  56.       Close #FNr
  57.      
  58.       s1() = Split(s, vbCrLf)
  59.       For i = LBound(s1) To UBound(s1)
  60.          s2() = Split(s1(i), Chr(9))
  61.          Set Li = Lvw.ListItems.Add
  62.          Li.Text = s2(0)
  63.          For j = 1 To UBound(s2)
  64.             Li.SubItems(j) = s2(j)
  65.          Next
  66.          Set Li = Nothing
  67.       Next
  68.       ListViewFillFromTextFile = True
  69.       Exit Function
  70.      
  71. Fehler:
  72.       Set Li = Nothing
  73.       MsgBox "Fehler: " & Err.Number & vbCrLf & _
  74.              Err.Description, vbCritical
  75. End Function

And the code to save is a command...

VB Code:
  1. Private Sub CmdSave_Click()
  2. 'Save Listview
  3.    Dim FileName As String
  4.       FileName = App.Path & "\ResourceFiles\SavedData.txt" 'the file you want to save to
  5.       Me.MousePointer = vbHourglass
  6.       ListViewSaveToTextFile lvwdemo, FileName
  7.       Me.MousePointer = vbDefault
  8.  
  9.  
  10.  
  11. End Sub

The difference for what Iam looking for is my listview is called Lvwbook, and I would like to save the whole listview, and perhaps keep the columns separated by a comma in the textfile.

Would anyone know what I need to change other than the name of the listview mentioned in the code above to save the whole listview?

Thanks a lot!!