Results 1 to 6 of 6

Thread: Save whole listview to textfile

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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:
    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!!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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:
    1. Private Sub CmdLoad_Click()
    2. 'load Listview
    3.    Dim FileName As String
    4.       FileName = App.Path & "\ResourceFiles\SavedData.txt"
    5.       'clear Listview Items
    6.       Lvwbook.ListItems.Clear
    7.       Me.MousePointer = vbHourglass
    8.       ListViewFillFromTextFile Lvwbook, FileName
    9.       Me.MousePointer = vbDefault
    10. 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?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
  •  



Click Here to Expand Forum to Full Width