PDA

Click to See Complete Forum and Search --> : Easy Text Save&Load


|2eM!x
Mar 16th, 2005, 09:10 PM
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:
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:
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:

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:
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:

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:
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 :D

Al42
Feb 14th, 2007, 12:47 PM
A couple of things |2eM!x doesn't mention:

If you set CommonDialog1.CancelError = True, pressing the cancel button on the dialog will raise error number 32755, so you can trap it and not try to open a file. (You can rename CommonDialog1 to whatever you wish, of course.)

And you can filter for more than 1 file type, such as: 'Filters for txt files, frm files or all files.
CommonDialog1.Filter = "Text Files (*.txt)|*.txt|VB Form Files (*.frm)|*.frm|ALl Files (*.*)|*.*"
You can also set the default file type that will show when the dialog comes up with:CommonDialog1.FilterIndex = 3 'All Files will be the default, since All Files is the 3rd one in the list

JamesVB
Oct 1st, 2010, 01:24 PM
is there anyway to like if the files dosnt exist it creats it?

si_the_geek
Oct 1st, 2010, 01:52 PM
If you Open the file with For Output it will be created if needed, and will contain anything you write to it before the Close (so if you want a blank file, don't write anything to it).

JamesVB
Oct 2nd, 2010, 06:02 AM
If you Open the file with For Output it will be created if needed, and will contain anything you write to it before the Close (so if you want a blank file, don't write anything to it).

lol i know that it will just give a error no file found but i would and iv been trying to find it but i dont know if i can :'(

si_the_geek
Oct 2nd, 2010, 07:14 AM
I'm afraid it isn't clear what you mean.

Opening it for Output will not give that error - it will be automatically created instead.

Opening it for Input will (if it does not exist), so you should pre-emptively check if it exists using the Dir() function, eg:
If Dir("C:\folder\file.ext") = "" Then
'not found
Else
'it exists, so open it
End If

JamesVB
Oct 2nd, 2010, 09:44 AM
I'm afraid it isn't clear what you mean.

Opening it for Output will not give that error - it will be automatically created instead.

Opening it for Input will (if it does not exist), so you should pre-emptively check if it exists using the Dir() function, eg:
If Dir("C:\folder\file.ext") = "" Then
'not found
Else
'it exists, so open it
End If
well with me it dosnt create it automaticly say's file not found on vb when i debuging it, so the program crashe's it says not responding