Well, i had to search a long time to find all this code, and to make sure it all works. To begin, there are numerous ways to open a text file. I am going to show you the two that i think are the easiest, and best way to do it.
CommonDialog
First off we have something called commondialog. It lets you load and save in almost the same coding. It looks as if you were to click file/open in your internet browser. Its use it to let the user select where to save
To add to to your form do this:
Right click on the long slender column on the left of your screen, click components and goto microsoft common dialog control 6.0, check it and click apply. On your Bar on the left, click the new item that appears and add it to your form.
To Save:
VB Code:
Private Sub Command1_Click() 'change this for when you want to save
With CommonDialog1 'Lets you call common dialog pretty much
.InitDir = App.Path 'Where you want the program to start to show to save
.FileName = "" 'If you only want to save as a specific file, put something in between quotes
.Filter = ".txt File (*.txt)|*.txt"
.DialogTitle = "Save txt to:" 'Just what you want the caption to say
.ShowSave 'Makes it look like it saved
This next line is optional, i just like making backups:
VB Code:
FileCopy .FileName, App.Path & "\filebak.bak" 'just backs it up before you save it
Now, to save text1.text into that file, we have to "print" it into it like so:
VB Code:
Open .FileName For Output Lock Read As #1 'Opens the path the user selected
Print #1, Text1.Text ' Pastes all text1.text into the .filename
Close #1 ' Closes the file, so its not left running
End with 'closes the with statement
------------------
To Load:
VB Code:
Dim FileName As String 'So we can find the filename
Dim F As Integer
CommonDialog1.Filter = "Text File (*.txt)|*.txt" 'Filters only for txt files
CommonDialog1.ShowOpen 'Open it
FileName = CommonDialog1.FileName ' So filename = something
If Len(FileName) = 0 Then 'error trap
print vbnullstring
Else
F = FreeFile
Open FileName For Input As #F 'Opens filename for loading
Text1.Text = Input$(LOF(F), F) ' Text2 = the input of Length of file(freefile), freefile
Close #F 'Closes the loading, so it isnt running
End If
Now onto the other way of loading and saving,I'm not really aware on its name, i just know it is not flexible. Its for use if you want the user to have a set path to where you want to save and load.
To Save:
VB Code:
Dim strBuff As String
ff = FreeFile
Open App.Path & "/yay.txt" For Output Lock Read As #ff 'open yay.txt
Print #ff, Text1.text '"print" text1.text into yay.txt
Close #ff 'close yay.txt
To Load:
VB Code:
If Dir$(App.Path & "/yay.txt") <> "" Then 'If the file exists then
Dim strBuff As String
ff = FreeFile
Open App.Path & "/yay.txt" For Input As #ff ' open yay.txt
Do Until EOF(ff) 'do until end of the file
Line Input #ff, strBuff 'input line by line into strbuff
Loop
Close #ff 'close it
text1.text = strbuff
I hope you like this, if you dont ill use it as a resource