• Log in or sign up for an account here: https://www.sythe.org/login

Save File error/help

tomanderson12

Member
Joined
Jan 21, 2008
Sythe Gold
0
Messages
53
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
 
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
 
Save File error/help
On Error Goto WhiteHorse 'lmao soz something random :P
With CommonDialog1
.CancelError = False 'dont show error if canceled
.DialogTitle = "Save" 'save box title
.Filter = "Text Files (*.txt)|*.text|All files (*.*)|*.*"
'shows what to save the file as
.FilterIndex = 1
.Flags = cdlOFNHideReadOnly Or cdlOFNOverwritePrompt Or cdlOFNPathMustExist 'file must exist etc
.InitDir = App.Path 'starts it off in the apps directory
.ShowSave 'shows the save box

WhiteHorse:
If Err.Number <> cdlCancel Then
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation, "Save" 'tells you what the error was
End If


Try that.
 
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.
 
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 !
 
Runescape Gamblingrunescape gambling
Back
Top