Save context of listbox

Posted on 2005-02-25
Medium Priority
Last Modified: 2010-05-02
How do I save the context of my listbox so that when i open my document.  All my items will still be there.
Question by:Jason_Kha
LVL 23

Expert Comment

ID: 13406647
When you say context, do you mean the contents of what is in the list box saved to a document so that you can read from a file to load the contents of the listbox back into the listbox when you re open the application or something like that ? Or are you referring to something else ?
LVL 37

Expert Comment

by:Harisha M G
ID: 13406675
Hi Jason_Kha,
    You can use the Index property of the listbox to go through the items and add them to a file.


Author Comment

ID: 13406715
gecko au2003 Yeah thats what I meant I don't know what I was thinking typing "context".
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 23

Expert Comment

ID: 13406954
Here is one PAQ with regards to writing a text file :


Here is a good website to take you through using the FSO ( File System Object ).

The File System Object (FSO) enables you to manipulate the files, folders and drives as well as read and write to sequential files. Before using the FSO, you have to add the "Microsoft Scripting Runtime Library" to the current project by selecting "Project", "References" from the menu bar. Alternatively you can use the CreateObject function to create the reference at run-time.

If you would like I could paste the code in here in a little while and show you were all the code goes or you can try and use them sites to figure it out yourself :) Upto you ;)
LVL 23

Expert Comment

ID: 13406962
I forgot to paste this site :)


oopppss sorry
LVL 23

Expert Comment

ID: 13407008
Here is a PAQ of one of my questions and I think glass cookie has an answer for you :)

LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 13407096
Option Explicit

Private fileName As String

Private Sub Form_Load()
    fileName = App.Path & "\List.txt"
    If Dir(fileName) <> "" Then
        Dim inputLine As String
        Debug.Print "Loading ListBox data from file..."
        Open fileName For Input As #1
        While Not EOF(1)
            Line Input #1, inputLine
            List1.AddItem (inputLine)
        Close #1
        Debug.Print "Loading initial ListBox data..."
        ' put some data in our listbox
        Dim i As Integer
        For i = 1 To 10
            List1.AddItem ("Item" & i)
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Debug.Print "Saving ListBox data to file..."
    Dim i As Integer
    Open fileName For Output As #1
    For i = 1 To List1.ListCount
        Print #1, List1.List(i - 1)
    Close #1
End Sub

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question