Click to See Complete Forum and Search --> : Loading and saving items to and from a listbox?


BlazinWild
May 11th, 2001, 09:12 PM
I am making a program for my High School senior project and it includes a Log In form. When a user logs in, their name is added to a listbox. Is there a way to save the contents of this listbox to a file of some sort when the user logs in, and also be loaded from this file to the listbox when the form is loaded again? If this is possible, could someone please help me?

Thanks

Robert Moy
May 11th, 2001, 11:10 PM
Hello:
Use list1.AddItem. I would recommend that you save listbox to a database
here code using ADO and Listbox to save information to Oracle database

Private Sub Command4_Click()
List1.AddItem (Text1.Text)
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields(1) = Val(List1.Text)
Adodc1.Refresh
End Sub

Good Luck

Robert Moy
May 11th, 2001, 11:31 PM
Hello:

I forgot to mention to retreive the data. You need to do this:

List2.AddItem (Adodc1.Recordset.Fields(0))
Adodc1.Recordset.MoveNext

Good Luck

It can use DAO and Access to do this. Read more about DAO.
You find ADO by using Project and then Component and then find Microsoft ADO then follow steps to link your Database to VB.

John G Duffy
May 12th, 2001, 09:23 AM
Below is two simple routines. The first will save your data to disk and the second will reload it to the Listbox. These routine assume your listbox is named "List1". Also you might want to change the fielname in the Open statements to suit your needs.


private Sub cmdSave_Click()
Dim X
Open "C:\MyFile.txt" for Output as #1
for X = 0 to List1.ListCount - 1
print #1, List1.List(X)
next X
Close #1
End Sub
'
'
private Sub cmdLoad_Click()
Dim Temp as string
List1.Clear ' clear current entries
Open "C:\MyFile.txt" for input as #1
Do Until EOF(1)
Line input #1, Temp
List1.AddItem Temp
Loop
Close #1
End Sub





John G