FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Visual basic 6.0 help me with saving code please





andy26
hi i want to make a program so that when i put something in a list box it saves the contense to a .txt file then loads it back into the list when you start the program up again
S3nd K3ys
Make a new project with a form named Form1 and a list box named List1. Add this code and run it.
Code:

Private Sub Form_Load()
    List1.AddItem "this is an item"
    List1.AddItem "so is this:"
End Sub

Private Sub List1_Click()
    Open "c:\file.txt" For Output As #1
    Print #1, List1.Text
    Close
End Sub



When the form loads it will populate the list box with two items. Clicking on one of the two items in the list box will write that info to the file. (Note this method only writes one line, and deletes everything else in the text file, so if you want multiple entries, you'll have to do something different.)

To read from a file into a list box, use this code with the same form and list box.


Code:
Private Sub Form_Click()
    Open "c:\file.txt" For Input As #1
    Do While Not EOF(1)
    Line Input #1, rec
    List1.AddItem rec
    Loop
    Close
End Sub


Note that this code will add each item, line by line, from the text file into the list box. To really test this, open the file in notepad and add several lines of text to it, save it, then run the program and click the form to see the items added to the list box.

HTH.
andy26
hi thanx but the last code you gave me isnt working the code rec says a type mismatch
Teddy1
Should be under Form_Load and not Form_Click Razz.

Code:

Private Sub Form_Load()
    Open "c:\file.txt" For Input As #1
    Do While Not EOF(1)
    Line Input #1, rec
    List1.AddItem rec
    Loop
    Close
End Sub
S3nd K3ys
Teddy1 wrote:
Should be under Form_Load and not Form_Click Razz.

Code:

Private Sub Form_Load()
    Open "c:\file.txt" For Input As #1
    Do While Not EOF(1)
    Line Input #1, rec
    List1.AddItem rec
    Loop
    Close
End Sub


I already used Form_Load() to populate the list box. Wink
S3nd K3ys
andy26 wrote:
hi yhanx but the last code you gave me isnt working the code rec says a type mismatch


If you have option explicit on, you need to declare any variables.

Code:
Dim rec as String
andy26
o yea thanx i tryed adding a variable but i did it as an integer lol o fogot about string thanx for your help.
S3nd K3ys
You're welcome. If you need more help, don't hesitate to ask.
Teddy1
My bad... i misread Embarassed, same with me Smile. Any help needed don't hesitate to ask Razz
Related topics
Learning C#
what software for all drive of usb flash disk 98se
My first Visual Basic program!
History of the BASIC family of languages
VisualBasic 6.0 Help
Visual Basic Express Addition
Computer Programming Help Books
Visual Basic 6
how about security of visual basic 6.0??
Visual Basic 6
Best Programming Language to Use
[Visual Basic] Divide X equation
Visual Basic 6 vs Visual Basic 2010
Visual basic 6
Reply to topic    Frihost Forum Index -> Computers -> Computer Problems and Support

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.