' 1) Create a new project. Form1 will be created by default.
' 2) Add a Textbox to the form.
' 3) Set the MultiLine property of the Textbox to TRUE.
' 4) Set the Scrollbars property of the Textbox 2 - Verticle.
' 5) Add four Command Buttons below the Textbox.
' 6) Paste the following code into the declarations section of Form1:
' --- Begin code for Form1
Option Explicit
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Sub Command1_Click()
Dim strFileName As String
Dim f As Integer
On Error GoTo err_Command1_Click
'Create
Call CreateFile(False)
Exit Sub
err_Command1_Click:
MsgBox Err.Description
End Sub
Private Sub Command2_Click()
Call CreateFile(True)
End Sub
Private Sub Command3_Click()
Call GrabFile(0)
End Sub
Private Sub Command4_Click()
Call GrabFile(1)
End Sub
Private Sub Command5_Click()
Call GrabFile(2)
End Sub
Private Sub Form_Load()
Dim strText As String
Me.Caption = "Reading/Writing Text Files"
Command1.Caption = "&Create"
Command2.Caption = "&Append"
Command3.Caption = "&Input$"
Command4.Caption = "I&nput#"
Command5.Caption = "&LInput#"
strText = "This program shows how to read and write to text files." & vbCrLf & vbCrLf
strText = strText & "The Create button will create a new file or overwrite an existing one." & vbCrLf & vbCrLf
strText = strText & "The Append button will create a new file or add to an existing one." & vbCrLf & vbCrLf
strText = strText & "The Input$ button demonstrates the use of the Input$ statement." & vbCrLf & vbCrLf
strText = strText & "The Input# button demonstrates the use of the Input statement." & vbCrLf & vbCrLf
strText = strText & "The LInput# button demonstrates the use of the Line Input statement." & vbCrLf & vbCrLf
strText = strText & "Note that the Input$ will input a specified number of characters." & vbCrLf & vbCrLf
strText = strText & "Input# will treat a comma and end of line as the same." & vbCrLf & vbCrLf
strText = strText & "The Line Input# statement is used to read an entire line of text up to the EOL character." & vbCrLf & vbCrLf
strText = strText & "The following line would be read in one iteration with Line Input# but 6 with Input #:"
strText = strText & "a,b,c,d,e,f" & vbCrLf & vbCrLf
strText = strText & "All of this text could be read with one iteration of Input$."
Text1.Text = strText
End Sub
Private Sub CreateFile(ByVal bAppend As Boolean)
Dim strMessage As String
Dim strFileName As String
Dim f As Integer
strFileName = Trim$(InputBox$("Filename to create:", "Create", "C:\Junk.txt"))
If strFileName <> "" Then
'Prompt user if file exists
If GetFileAttributes(strFileName) > 0 Then
If bAppend Then
strMessage = "File exists, ok to append?"
Else
strMessage = "File exists, ok to overwrite?"
End If
If MsgBox(strMessage, vbOKCancel) <> vbOK Then
Exit Sub
End If
End If
'Ok to write or append here
f = FreeFile
If bAppend Then
Open strFileName For Append As #f
Else
Open strFileName For Output As #f
End If
Print #f, Text1.Text
Close f
End If
End Sub
Private Sub GrabFile(ByVal intInputMethod As Integer)
Dim strFileName As String
Dim f As Integer
Dim strText As String
On Error GoTo err_GrabFile
strFileName = Trim$(InputBox$("Open file:", "Open", "C:\Junk.txt"))
If strFileName <> "" Then
'Make sure file exists
If GetFileAttributes(strFileName) > 0 Then
Text1.Text = ""
f = FreeFile
Open strFileName For Input As #f
Select Case intInputMethod
Case 0 'Input$
'Input$ accepts a length and a LFN
'LOF(F) returns the length of the file
'so this statement returns the entire file
Debug.Print "---------- Input$ ----------"
strText = Input$(LOF(f), f)
Debug.Print strText
Text1.Text = strText
Debug.Print "----------------------------"
Debug.Print ""
Case 1 'Input #
'Input# will stop reading at a comma or end of line
Debug.Print "---------- Input # ----------"
Do While Not EOF(f)
Input #f, strText
Debug.Print strText
Text1.Text = Text1.Text & strText & vbCrLf
Loop
Debug.Print "----------------------------"
Debug.Print ""
Case 2 'Line Input #
'Line Input# will read the entire line
Debug.Print "---------- Line Input # ----------"
Do While Not EOF(f)
Line Input #f, strText
Debug.Print strText
Text1.Text = Text1.Text & strText & vbCrLf
Loop
Debug.Print "----------------------------"
Debug.Print ""
End Select
Else
MsgBox "File does not exist."
End If
End If
Exit Sub
err_GrabFile:
MsgBox Err.Description
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
' --- End code for Form1 ---