'This tutorial explains how to
'read files, and write them into
'variables and then also to add
'to files. Quite simple, and if
'you have any questions then
'private message Skateboarder.
Option Explicit 'You should know
'what this means,
'all variables used
'must be declared.
Dim File As String 'This is the file we are going to open
'right now, we must specify it in Form_Load
Private Sub Command1_Click()
File = InputBox$("What is the file name?")
'Don't mess this up unless you want to learn
'the errors that will be given
End Sub
Private Sub Command2_Click()
If MsgBox("Alright, so we're writing to " & File & ", correct?", vbYesNo) = vbYes Then
Open "C:\FileIO.txt" For Append As #1 'Remember, Append means add to
Print #1, txtFileString
Close #1
End If
End Sub
Private Sub Command3_Click()
Dim strVar As String 'We need a string to
'write to
'Now we begin the reading part
Open File For Input As #1 'Now this is something new
'Input means to read the file
'Now, if there was no
'"C:\FileIO.txt" file then
'an error would be generated
'File Not Found would be
'the error
Do Until EOF(1) 'EOF is a boolean meaning End Of File
'1 is the number since we open
'it For Input As #1
'EOF(1) is true when we reach the
'end of the file
Line Input #1, strVar 'We read the line
'and that line is a
'string, so we
'put it into a string
'Right here we could put some code checking
'the contents of that string
'For now we'll use MsgBox and that will show us
'the contents of the text file
MsgBox strVar 'Now read the MsgBox
Loop
Close #1 'Close it up now, so we don't get
'a File Already Open error the next
'time we try to open it
End Sub
Private Sub Command4_Click()
Dim FileStr As String
Open File For Input As #1 'This should be familiar
'check out Command3_Click
FileStr = Input$(LOF(1), 1)
'Now let's explain this argument by argument
'Input$ - look it up in the object browser
'The first argument we pass is the len
'LOF is Length of File and 1 is our filenumber
'because that's what we open the file
'For Input As. our second argument tells
'the file to read from, and 1 is our
'filenumber, so let's tell them one.
'let's see this file now!
MsgBox FileStr
Close #1
End Sub
Private Sub Form_Load()
'First, we must make sure
'that we have a file to
'read from, so we check
'if the file is there
'using Dir$
File = "C:\FileIO.txt"
If Dir$(File) = "" Then 'Check if "C:\FileIO.txt" is created
Debug.Print "No tutorial file found, so it will be created."
Open File For Output As #1 'For output means we create
Close #1 'the file and write to it,
End If 'or, if it exists
'(which it doesn't because we
'checked it with Dir$)
'we clear it and write whatever we tell
'VB to write to it
'Now, we know we have a file to work with,
'so let's start writing to it
Open File For Append As #1 'Append means to add to the file
'On the other hand, For Output clears the
'file/if not there creates it, and
'writes to it from scratch whether it's
'already been made or not
Print #1, "Hello" 'Print means writing to it
'and since we specified
'For Append As #1, we use
'Print #1, this also
'applies when opening
'the file For Output As #1
'Tip: Whenever writing
'to a file using Print
Close #1 'Close it up now, so we don't get
'a File Already Open error the next
'time we try to open it
'At this point in the tutorial you should
'understand what two ways to write to
'a file, using Append or Output
End Sub