Save Data into Txt Files / Notepad

Previously we have learned about file database operation in Visual Basic, this time we will applicate it to a program, file operations that we will use is Save file, overview about the program that we will make, we will write visitor’s book data of a company, and save it in a data file with txt format (notepad), well below the tutorial:

1. First run VB with Standard EXE program,
2. Add 5 Labels, 4 Text boxes, 2 Command Buttons and 1 list box, then change the properties like this:
Tool BoxPropertiesDescription
Form1Start UpCenter Screen
Label1CaptionTitle)
Label2No
Label3Name
Label4From
Label5Purpose
Text1Text(Blank)
Text2
Text3
Text4
Command1CaptionClear
Command2Save
AllFont Size10

Don’t forget to set the wide, for example like this:

3. Before write the script, save the program first
4. If its already, add the following script:

Dim Data As Integer
Dim Name1(100), No1(100), From1(100), Purpose1(100) As String

Private Sub Command1_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.SetFocus
Command2.Enabled = True
End Sub

Private Sub Command2_Click()
'If text1 until text4 are blank when command2 "SAVE" clicked, so there will be a message
If Text1 = "" Or Text2 = "" Or Text3 = "" Or Text4 = "" Then
MsgBox "Please, Complete The Form !", vbInformation, "Blues Pedia"
Else

'Add data to list1
Data = Data + 1
No1(Data) = Trim(Text1)
Name1(Data) = Trim(Text2)
From1(Data) = Trim(Text3)
Purpose1(Data) = Trim(Text4)
Senteces = No1(Data) & " : " & Name1(Data) & " : " & From1(Data) & " : " & Purpose1(Data)
List1.AddItem Senteces
Command2.Enabled = False

'Open and make file (first time) guest.txt
Open App.Path & "\guest.txt" For Output As #1
'Save data to file guest.txt
For i = 1 To Data
Print #1, No1(i), Name1(i), From1(i), Purpose1(i)
Next i

'Close file guest.txt
Close #1
End If
End Sub

Private Sub Form_Load()
Data = 0
List1.clear
End Sub

5. Run the program


First fill all text box columns
Text1 (Number): Visitors list
Text2 (Name): Name
Text3 (From): From (Can address, organization, company)
Text4 (Purpose): Importance, that visitor comes with what purpose

Then click Save, then automatically List1 will contain the data that has been written, as well as the data guest.txt, (Check the storage location of program, then there will be guest.txt file)

So this is enough for tutorial this time about how to save data to txt file / notepad,
Thank you, may be useful

Popular Posts