Adblock breaks this site

Save File error/help

Discussion in 'Programming General' started by tomanderson12, Sep 18, 2008.

  1. tomanderson12

    tomanderson12 Member

    Joined:
    Jan 21, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0
    Save File error/help

    Hey, I recently formatted my harddrive due to some error on the hard drive i couldnt seem to fix anyway if any one remembers i created a notepad a while back and you could save files so i wanted to create the program again but when i went to code the save file it came up with an error

    Code:
    Dim filelocation As String
    
    'loads save as box
       CommonDialog1.ShowSave
        filelocation = CommonDialog1.filename
    ' append saves over file if it assists
        Open filelocation For Append As #1
            Print #1, Text1.Text
        Close #1
    Thats the code for the save, it works and you can save a file but if you click save then change your mind about saving the file and close the dialog box it 'crashes' the entire notepad
     
  2. htaed

    htaed Forum Addict
    Banned

    Joined:
    Dec 19, 2005
    Posts:
    336
    Referrals:
    0
    Sythe Gold:
    0
    Save File error/help

    Code:
    Dim filelocation As String
    
    on Error goto Err:
    'loads save as box
       CommonDialog1.ShowSave
        filelocation = CommonDialog1.filename
    ' append saves over file if it assists
        Open filelocation For Append As #1
            Print #1, Text1.Text
        Close #1
    exit sub
    Err:
    Msgbox "Error!", vbCritical, "Error"
    
    Maybe this will help you
     
  3. IVistaI

    IVistaI Forum Addict

    Joined:
    Nov 6, 2007
    Posts:
    281
    Referrals:
    0
    Sythe Gold:
    0
    Save File error/help


    Try that.
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Save File error/help

    When you close the dialog, it doesn't automatically cancel out the entire method. You need to put handling code to make sure that your application ceases to execute that particular code if cancel is clicked.

    An easy way to do this would be to check the validity of the string.
    Code:
    Dim filelocation As String
    Dim File As Integer
    File = FreeFile
    'loads save as box
       CommonDialog1.ShowSave
        filelocation = CommonDialog1.filename
        If filelocation=vbNullString Then Exit Sub
    ' append saves over file if it assists
        Open filelocation For Append As #File
            Print #File, Text1.Text
        Close #File
    I don't actually have VB6 installed, so I couldn't test it, but try that out and see if it works. The people who posted the "On Error GoTo bullshit" are just asking for bad structure. In example, what if you got an error in the middle of a massive operation which uses a lot of memory? You'd get a memory leak.

    Edit: I also think I should mention why I declared an Integer called "File" and assigned it to FreeFile. In simple, VB's file opening requires you to specify a unique number for each file. If you use a number that is already open, then your program will simply crash. The best way to manage things in this situation is FreeFile. FreeFile is something provided by VB6 which simply stands for any free file number available. Therefore, by using this way of file I/O, you are avoiding unnecessary errors.
     
  5. tomanderson12

    tomanderson12 Member

    Joined:
    Jan 21, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0
    Save File error/help

    Thank you Swan for explaining what it is and what to avoid, my opinion is that theres no point giving someone a code without saying what it does

    Thanks again


    Edit : Tried it and it works brilliantly !
     
< Project Ideas :) | Programming Humor >


 
 
Adblock breaks this site