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 values from a file using Input and Write
Keywordssave, load, values, file
CategoriesFiles and Directories
 
Use Write to save values and Input to read them.
 
Private Sub cmdLoad_Click()
Dim fnum As Integer
Dim i As Integer
Dim value As String

    ' Open the file.
    On Error GoTo LoadError
    fnum = FreeFile
    Open "animals.dat" For Input As fnum
    
    ' Write the values.
    For i = txtAnimal.lbound To txtAnimal.ubound
        Input #fnum, value
        txtAnimal(i).Text = value
    Next i
    
    ' Close the file.
    Close fnum
    Exit Sub
    
LoadError:
    MsgBox "Error" & Str$(Err.Number) & _
        " loading data." & vbCrLf & _
        Err.Description
End Sub

Private Sub cmdSave_Click()
Dim fnum As Integer
Dim i As Integer

    ' Open the file.
    On Error GoTo SaveError
    fnum = FreeFile
    Open "animals.dat" For Output As fnum
    
    ' Write the values.
    For i = txtAnimal.lbound To txtAnimal.ubound
        Write #fnum, txtAnimal(i).Text
    Next i
    
    ' Close the file.
    Close fnum
    Exit Sub
    
SaveError:
    MsgBox "Error" & Str$(Err.Number) & _
        " saving data." & vbCrLf & _
        Err.Description
End Sub

Private Sub cmdClear_Click()
Dim i As Integer

    For i = txtAnimal.lbound To txtAnimal.ubound
        txtAnimal(i).Text = ""
    Next i
End Sub
 
Note that the Write method puts quotes around the values so the file looks like this:

    "Ape"
    "Bear"
    "Cow"
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated