I am using a ListView control in my VB Form and I want to save all the data in my ListView control. It has 2 columns and the number of rows can keep increasing depending on the user. Please help me with this query. Thanks in advance.

Dani AI

Generated

Short answer and recommendation: SaveSetting/GetSetting can persist a small, per-user ListView state in the registry, but it is not ideal for large or shared data. For (two columns, variable rows) the pattern below will save column count and rows, then restore them. As pointed out, a database (or even a CSV/XML file) is the better long-term solution for growing data.

' Simple VB6 pattern: saves and restores a ListView to the registry
Const APP_NAME = "MyVBApp"

Public Sub SaveListViewToRegistry(lv As ListView)
    Dim i As Long, c As Long
    Dim li As ListItem
    Dim colCount As Long

    colCount = lv.ColumnHeaders.Count
    SaveSetting APP_NAME, "ListView", "Columns", CStr(colCount)
    SaveSetting APP_NAME, "ListView", "Count", CStr(lv.ListItems.Count)

    For i = 1 To lv.ListItems.Count
        Set li = lv.ListItems(i)
        SaveSetting APP_NAME, "ListView", "Item" & i & "_0", li.Text
        For c = 1 To colCount - 1
            SaveSetting APP_NAME, "ListView", "Item" & i & "_" & c, li.SubItems(c)
        Next c
    Next i
End Sub

Public Sub LoadListViewFromRegistry(lv As ListView)
    Dim i As Long, c As Long
    Dim count As Long, colCount As Long
    Dim li As ListItem, itemText As String

    lv.ListItems.Clear
    count = Val(GetSetting(APP_NAME, "ListView", "Count", "0"))
    colCount = Val(GetSetting(APP_NAME, "ListView", "Columns", "1"))

    For i = 1 To count
        itemText = GetSetting(APP_NAME, "ListView", "Item" & i & "_0", "")
        Set li = lv.ListItems.Add(, , itemText)
        For c = 1 To colCount - 1
            li.SubItems(c) = GetSetting(APP_NAME, "ListView", "Item" & i & "_" & c, "")
        Next c
    Next i
End Sub

Notes and troubleshooting

  • SaveSetting stores strings under HKEY_CURRENT_USER\Software\VB and VBA Program Settings\<AppName>\ListView\<Key>. It is per-user and not suitable for large amounts of data or sensitive info.
  • Use DeleteSetting to remove old keys (clear the section) before re-saving to avoid stale entries. Check VB6 docs for exact DeleteSetting usage.
  • Convert non-string values to strings on save and parse them on load. Watch for commas/newlines if switching to CSV; quote/escape fields.
  • For anything more than a few dozen rows, implement a simple file format (CSV or XML) or use ADO + a local DB (Access/SQL Server/SQLite) as recommended—that scales better and is easier to query.

Recommended Answers

All 4 Replies

I think you need to use database to save the data not SaveSetting.

Can you help me in doing that( I'm a newbie ), please give me a sample code.

You need to create tables in desired database.
Use ADO to connect VB to database.
Store data from front end application into database.

programming 101

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.