[RESOLVED] Saving the Contents of a Listview
Im unsure of what is the most efficient way to save the contents of a list view. Then when the form loads again i need it to read from the database file or where ever it is. It ha to save the name and description as the list view has 2 column headers. Any thought of how or to what type of file i should save it to i would be grateful :)
Re: Saving the Contents of a Listview
You can save it to a local file and then load it back from a file:
VB Code:
Private Sub btnSaveToFile_Click()
'=================================
Dim i%, j%
Dim strHeader As String
Dim strLine As String
Open App.Path & "\listview.txt" For Output As #1
With ListView1.ListItems
strHeader = "Headers:"
For j = 1 To ListView1.ColumnHeaders.Count
strHeader = strHeader & ListView1.ColumnHeaders(j) & ";"
Next j
Print #1, Left(strHeader, Len(strHeader) - 1)
For i = 1 To .Count
strLine = strLine & .Item(i).Text & ";"
For j = 1 To ListView1.ColumnHeaders.Count - 1
strLine = strLine & .Item(i).SubItems(j) & ";"
Next j
Print #1, Left(strLine, Len(strLine) - 1)
strLine = ""
Next i
End With
Close #1
End Sub
Private Sub btnLoadFromFile_Click()
'==================================
Dim pos%, i%, j%, strLine$
Dim varAllValues As String
Dim MyValues() As String
ListView1.ColumnHeaders.Clear
ListView1.ListItems.Clear
If Dir(App.Path & "\listview.txt") = "" Then Exit Sub
Open App.Path & "\listview.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, strLine
If Not InStr(1, LCase(strLine), "headers") > 0 Then
MyValues() = Split(strLine, ";", , vbTextCompare)
Set itm = ListView1.ListItems.Add(, , MyValues(0))
For j = 1 To UBound(MyValues)
Set LSI = itm.ListSubItems.Add(, , MyValues(j))
Next j
Else
If InStr(1, LCase(strLine), "headers:") > 0 Then
pos = InStrRev(strLine, ":")
strLine = Mid(strLine, pos + 1)
MyValues() = Split(strLine, ";", , vbTextCompare)
With ListView1.ColumnHeaders
For j = 0 To UBound(MyValues)
.Add , , MyValues(j)
Next j
End With
End If
End If
Loop
Close #1
End Sub
Re: Saving the Contents of a Listview
kk thanks a bunch ill test it and see if it works :)
Re: Saving the Contents of a Listview
oh it will... he gave me the same source code for the same question like 8 months ago... :)
Re: Saving the Contents of a Listview
Kk though i was looking to write more to a *.adb file rather than a plain *.txt file but at the moment i just need to get the program in working order so this will do for now ill figure the rest out another time :)
Re: Saving the Contents of a Listview
What's an "*.adb" ? :confused:
Re: Saving the Contents of a Listview
Quote:
Originally Posted by RhinoBull
What's an "*.adb" ? :confused:
Probably a text file to which he wishes to assign his own extension.
Re: Saving the Contents of a Listview
Im not to sure myself though i remember seeing it done on my mates project it is called an ADB file thats all i know. I thought at first it mean Access Data Base but that is just a guess but anyway what Rhino gave me works fine :D
Re: [RESOLVED] Saving the Contents of a Listview
Quote:
Originally posted by Hack
Probably a text file to which he wishes to assign his own extension.
Its an Office Suite, maybe this will help with the .adb extension :wave:
Re: [RESOLVED] Saving the Contents of a Listview
Hmmm. Well, I don't see any points using third party soft just to store few things - unless you need to store more than what you've mentioned text file is more than enough. Otherwise consider using MS Access database or at least stay away from any proprietary formats that nobody's using.