|
-
Sep 23rd, 2017, 09:35 PM
#1
Thread Starter
PowerPoster
Save whole listview to textfile
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!!
-
Sep 23rd, 2017, 10:35 PM
#2
Re: Save whole listview to textfile
in cmdsave, change the name of the listview and the path you want to save the file, no changes needed to module
the existing code separates the listview columns by a TAB, if you want to change to comma just change the CHR(9) to "," (or chr(44)) whereever it occurs in the module code, or maybe TABs are ok anyway
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 24th, 2017, 05:24 AM
#3
Thread Starter
PowerPoster
Re: Save whole listview to textfile
Thank you, that works great for saving.
In that same post, there was code for loading text back into the listview. I copied it, and I think I changed what was needed for my project,
VB Code:
Private Sub CmdLoad_Click()
'load Listview
Dim FileName As String
FileName = App.Path & "\ResourceFiles\SavedData.txt"
'clear Listview Items
Lvwbook.ListItems.Clear
Me.MousePointer = vbHourglass
ListViewFillFromTextFile Lvwbook, FileName
Me.MousePointer = vbDefault
End Sub
However I get an error when I try to load it says "Fehler 9 Subscript out of range"
Would that mean the program is trying to load all of the data into 1 column of the listview?
-
Sep 24th, 2017, 05:37 AM
#4
Re: Save whole listview to textfile
Would that mean the program is trying to load all of the data into 1 column of the listview?
i doubt it
is that the path\filename you saved to?
especially if you are using windows 7 or 10, writing to app.path is fraught with problems, even if it appears to work correctly, it may have saved the file to some other location, save the file to a user folder
if you debug, it should show in yellow which line causes the error
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 24th, 2017, 05:57 AM
#5
Thread Starter
PowerPoster
Re: Save whole listview to textfile
Yes it is the path I am using. I never knew app.path wouldn't be good to use anymore. I liked how it gives you the drive/folder of the app automatically. So what do people use now?
Oddly enough, it doesn't stop the program. It simply gives me the error, I press ok in a message box, and it appears to load part (but appears to load all of it) to the listview.
-
Sep 24th, 2017, 04:17 PM
#6
Re: Save whole listview to textfile
this is just a guess, but maybe the last line of the saved file is an empty line
try changing
For i = LBound(s1) To UBound(s1)
to
Code:
For i = LBound(s1) To UBound(s1) - 1
see if the error goes away
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|