Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 33,923 times

Related Categories

Load/Save listboxes to a file

mrjdesign

This is a simple way to learn the basics of reading and writing data from text files (or other ASCI files) into or out from your list boxes. The code provided below is made suitable for pasting to a separate modlue so you can resuse it for any list box form any form in your application at all times.

Note
You can invoke this and the next two functions by starting on opening a form use the Private Sub Form_Load()to initiate the loading of the values to your form on start up, or call the function in a separate module by using a command button.

Examples

'load the contents of listitems.txt into lstItems
Call LoadListFromFile(App.Path & "\listitems.txt",lstItems)

'save lstItems to listitems.txt, without clearing it
Call SaveListToFile(App.Path & "\listitems.txt",lstItems)

'save lstItems to listitems.txt, and clear it
Call SaveListToFile(App.Path & "\listitems.txt",lstItems,True)

Put the following code in a module

'Read data to a list.box form an ASCII file
'paste this in a module
Public Function LoadListFromFile(ByRef SourceFile As String, _ ByRef ToFormList As ListBox) On Error GoTo ErrEvt Dim TextLine As String, FN As Integer ToFormList.Clear FN = FreeFile Open SourceFile For Input As #FN ' Open file. Do While Not EOF(FN) ' Loop until end of file. Line Input #FN, TextLine ' Read line into variable. If TextLine <> LineToRem Then ToFormList.AddItem(TextLine) End If Loop Close #FN ' Close file. Exit Function 'this error handler will skip the nasty problem 'if your text file isnt conformed with an extra linebreak at the end 'to avoid saying INPUT PAST EOF ErrEvt: Select Case Err.Number Case 51 Err.Clear ' just bail out from this Case Else ' do nothing End Select Resume Next End Function

'============================================ 'Outputting list boxes to text files
'Call with blnClearList=true to clear the listbox afterwards Public Function SaveListToFile(ByVal strPrintToFile As String, _ ByRef lstFormList As ListBox, Optional ByVal blnClearList As Boolean = False) Dim I As Long 'longs are quicker than Integers so I normally use those Dim FN As Integer FN = FreeFile 'print each line in the list to a new text file Open strPrintToFile For Output As #FN 'Add all Items to the opened file For I = 0 To lstFormList.ListCount - 1 Print #FN, lstFormList.List(I) Next I Close #FN 'thats it... your file is updated

'clear the listbox?
If blnClearList = True Then lstFormList.Clear End Function '============================================


©2001 - All Rights Reserved - MRJ Design
The source code samples and information pertained within this document are considered copyrighted material and may not be re-distributed by electronic or other media in any form or fashion whatsoever, withou

Comments

  • Re: [1724] Load/Save listboxes to a file

    Posted by psl on 15 Dec 2006


    Call LoadListFromFile(App.Path & "\listitems.txt",lstItems)

    my error happens with the "&" between App.Path and
    "\listitems.txt"

  • To work!!!

    Posted by jfontaine on 27 Jan 2002

    For this code to work you may have to change the module code at the [code]LoadListFromFile[/code] function, it says [code]ListToForm As String[/code] if you get an error, when running your project, ch...