Results 1 to 7 of 7

Thread: Easy Text Save&Load

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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:
    1. Private Sub Command1_Click() 'change this for when you want to save
    2. With CommonDialog1 'Lets you call common dialog pretty much
    3.         .InitDir = App.Path 'Where you want the program to start to show to save
    4.         .FileName = "" 'If you only want to save as a specific file, put something in between quotes
    5.         .Filter = ".txt File (*.txt)|*.txt"
    6.         .DialogTitle = "Save txt to:" 'Just what you want the caption to say
    7.         .ShowSave 'Makes it look like it saved
    This next line is optional, i just like making backups:
    VB Code:
    1. 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:
    1. Open .FileName For Output Lock Read As #1 'Opens the path the user selected
    2.         Print #1, Text1.Text ' Pastes all text1.text into the .filename
    3.         Close #1 ' Closes the file, so its not left running
    4.         End with 'closes the with statement

    ------------------

    To Load:
    VB Code:
    1. Dim FileName As String 'So we can find the filename
    2.     Dim F As Integer
    3.     CommonDialog1.Filter = "Text File (*.txt)|*.txt" 'Filters only for txt files
    4.     CommonDialog1.ShowOpen 'Open it
    5.     FileName = CommonDialog1.FileName ' So filename = something
    6.     If Len(FileName) = 0 Then 'error trap
    7.         print vbnullstring
    8.     Else
    9.         F = FreeFile
    10.         Open FileName For Input As #F 'Opens filename for loading
    11.         Text1.Text = Input$(LOF(F), F) ' Text2 = the input of Length of file(freefile), freefile
    12.         Close #F 'Closes the loading, so it isnt running
    13.     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:
    1. Dim strBuff As String
    2.     ff = FreeFile
    3. Open App.Path & "/yay.txt" For Output Lock Read As #ff 'open yay.txt
    4.         Print #ff, Text1.text '"print" text1.text into yay.txt
    5.         Close #ff 'close yay.txt

    To Load:
    VB Code:
    1. If Dir$(App.Path & "/yay.txt") <> "" Then  'If the file exists then
    2.     Dim strBuff As String
    3.     ff = FreeFile
    4.     Open App.Path & "/yay.txt" For Input As #ff ' open yay.txt
    5.     Do Until EOF(ff) 'do until end of the file
    6.        Line Input #ff, strBuff 'input line by line into strbuff
    7.     Loop
    8.     Close #ff 'close it
    9. text1.text = strbuff

    I hope you like this, if you dont ill use it as a resource

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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:
    1. 'Filters for txt files, frm files or all files.
    2. 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:
    1. 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

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    17

    Re: Easy Text Save&Load

    is there anyway to like if the files dosnt exist it creats it?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    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).

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    17

    Re: Easy Text Save&Load

    Quote Originally Posted by si_the_geek View Post
    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 :'(

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    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

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    17

    Re: Easy Text Save&Load

    Quote Originally Posted by si_the_geek View Post
    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
  •  



Click Here to Expand Forum to Full Width