Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSave and load data when a program starts and stops by using a text file
DescriptionThis example shows how to save and load data when a program starts and stops by using a text file in Visual Basic 6.
Keywordssave, load, control
CategoriesControls, Files and Directories, Software Engineering
 
The LoadData subroutine opens a text file and reads control values with the Input statement. It reads the choices and current value for a ComboBox and ListBox, and the text in TextBoxes.
 
Private Sub LoadData()
Dim fnum As Integer
Dim txt As String
Dim num_items As Integer
Dim i As Integer
Dim selected_index As Integer
Dim is_selected As Integer

    ' Clear all controls.
    cboAnimals.Clear
    lstColors.Clear
    For i = 0 To 3
        Text1(i).Text = ""
    Next i

    ' Open the configuration file.
    On Error GoTo NoFile
    fnum = FreeFile
    Open App.Path & "\config.dat" For Input As fnum

    ' Read the cboAnimals data.
    ' Get the number of items.
    Input #fnum, num_items
    ' Get the items
    For i = 0 To num_items - 1
        Input #fnum, txt
        cboAnimals.AddItem txt
    Next i
    ' Get the selected item index.
    Input #fnum, selected_index
    cboAnimals.ListIndex = selected_index

    ' Read the lstColors data.
    ' Get the number of items.
    Input #fnum, num_items
    ' Get the items.
    For i = 0 To num_items - 1
        Input #fnum, txt
        lstColors.AddItem txt
    Next i

    ' Get the text boxes.
    Input #fnum, txt
    Text1(0).Text = txt

    Input #fnum, txt
    Text1(1).Text = txt

    Input #fnum, txt
    Text1(2).Text = txt

    Input #fnum, txt
    Text1(3).Text = txt

    ' Close the file.
    Close fnum

NoFile:

End Sub
 
Subroutine SaveData saves the values when the form is closing.
 
Private Sub SaveData()
Dim fnum As Integer
Dim txt As String
Dim num_items As Integer
Dim i As Integer

    ' Open the configuration file.
    On Error GoTo NoFile
    fnum = FreeFile
    Open App.Path & "\config.dat" For Output As fnum

    ' Save the cboAnimals data.
    ' Save the number of items.
    Write #fnum, cboAnimals.ListCount
    ' Save the items
    For i = 0 To cboAnimals.ListCount - 1
        Write #fnum, cboAnimals.List(i)
    Next i
    ' Save the selected item's index.
    Write #fnum, cboAnimals.ListIndex

    ' Read the lstColors data.
    ' Save the number of items.
    Write #fnum, lstColors.ListCount
    ' Save the items.
    For i = 0 To lstColors.ListCount - 1
        Write #fnum, lstColors.List(i)
    Next i

    ' Save the text boxes.
    Write #fnum, Text1(0).Text
    Write #fnum, Text1(1).Text
    Write #fnum, Text1(2).Text
    Write #fnum, Text1(3).Text

    ' Close the file.
    Close fnum

NoFile:

End Sub
 
This program also contains code that lets the user add choices to the ComboBox and ListBox.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated