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:
Option Explicit Public Function ListViewSaveToTextFile(Lvw As ListView, FileName As String) As Boolean Dim Li As ListItem Dim i As Long Dim j As Long Dim s() As String Dim FNr As Integer On Error GoTo Fehler FNr = FreeFile Open FileName For Output As #FNr For i = 1 To Lvw.ListItems.Count Set Li = Lvw.ListItems(i) ReDim s(Li.ListSubItems.Count) s(0) = Li.Text For j = 1 To Li.ListSubItems.Count s(j) = Li.SubItems(j) Next If i < Lvw.ListItems.Count Then Print #FNr, Join(s, Chr(9)) Else Print #FNr, Join(s, Chr(9)); End If Set Li = Nothing Next Close #FNr ListViewSaveToTextFile = True Exit Function Fehler: Set Li = Nothing MsgBox "Fehler: " & Err.Number & vbCrLf & _ Err.Description, vbCritical End Function Public Function ListViewFillFromTextFile(Lvw As ListView, _ FileName As String) As Boolean Dim Li As ListItem Dim s As String Dim s1() As String Dim s2() As String Dim i As Long Dim j As Long Dim FNr As Integer On Error GoTo Fehler FNr = FreeFile Open FileName For Binary As #FNr s = Space(LOF(FNr)) Get #FNr, , s Close #FNr s1() = Split(s, vbCrLf) For i = LBound(s1) To UBound(s1) s2() = Split(s1(i), Chr(9)) Set Li = Lvw.ListItems.Add Li.Text = s2(0) For j = 1 To UBound(s2) Li.SubItems(j) = s2(j) Next Set Li = Nothing Next ListViewFillFromTextFile = True Exit Function Fehler: Set Li = Nothing MsgBox "Fehler: " & Err.Number & vbCrLf & _ Err.Description, vbCritical End Function
And the code to save is a command...
VB Code:
Private Sub CmdSave_Click() 'Save Listview Dim FileName As String FileName = App.Path & "\ResourceFiles\SavedData.txt" 'the file you want to save to Me.MousePointer = vbHourglass ListViewSaveToTextFile lvwdemo, FileName Me.MousePointer = vbDefault 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!!




Reply With Quote