-
Mar 16th, 2005, 09:10 PM
#1
Thread Starter
Admodistrator
Easy Text Save&Load
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
-
Feb 14th, 2007, 12:47 PM
#2
Re: OpenFileDialog
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:
VB Code:
'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:
VB Code:
CommonDialog1.FilterIndex = 3 'All Files will be the default, since All Files is the 3rd one in the list
Last edited by Al42; Aug 24th, 2007 at 12:25 PM.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Oct 1st, 2010, 12:24 PM
#3
Junior Member
Re: Easy Text Save&Load
is there anyway to like if the files dosnt exist it creats it?
-
Oct 1st, 2010, 12:52 PM
#4
Re: Easy Text Save&Load
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).
-
Oct 2nd, 2010, 05:02 AM
#5
Junior Member
Re: Easy Text Save&Load
Originally Posted by si_the_geek
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 :'(
-
Oct 2nd, 2010, 06:14 AM
#6
Re: Easy Text Save&Load
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:
Code:
If Dir("C:\folder\file.ext") = "" Then
'not found
Else
'it exists, so open it
End If
-
Oct 2nd, 2010, 08:44 AM
#7
Junior Member
Re: Easy Text Save&Load
Originally Posted by si_the_geek
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:
Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|