Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Existing FIle

Runt1128Runt1128 Posts: 18Member
In my program, the user can click a command button to save information which is being displayed in a text box. The file name is also provided by the user in another text box called FileName. I want to be able to detect the file and if it exists, a message box will pop up asking the user if they want to overwrite the existing file or cancel. I can get the file detected but it still saves. Any ideas?

Private Sub cmdFile_Click()
Dim Error
Dim i As Integer
cmdATP_Click
Open FileName For Output Access Write As #1
Print #1, ReportText
Close #1

If Dir(FileName) <> vbNullString Then
Error = MsgBox("This File Already Exists, Over Write?", vbYesNo, "File Error")
End If

End Sub


Comments

  • hobbyhobby Posts: 84Member
    Check if the file exists before you write to it. Place your code in this order:

    [code]
    Private Sub cmdFile_Click()
    Dim Error
    Dim i As Integer
    Dim FileExists as Boolean
    cmdATP_Click
    If Dir(FileName) <> vbNullString Then
    FileExists = True
    Error = MsgBox("This File Already Exists, Over Write?", vbYesNo, "File Error")
    End If

    If Error = vbYes OR Not FileExists then
    Open FileName For Output Access Write As #1
    Print #1, ReportText
    Close #1
    End if
    End Sub


    [/code]


    : In my program, the user can click a command button to save information which is being displayed in a text box. The file name is also provided by the user in another text box called FileName. I want to be able to detect the file and if it exists, a message box will pop up asking the user if they want to overwrite the existing file or cancel. I can get the file detected but it still saves. Any ideas?
    :
    : Private Sub cmdFile_Click()
    : Dim Error
    : Dim i As Integer
    : cmdATP_Click
    : Open FileName For Output Access Write As #1
    : Print #1, ReportText
    : Close #1
    :
    : If Dir(FileName) <> vbNullString Then
    : Error = MsgBox("This File Already Exists, Over Write?", vbYesNo, "File Error")
    : End If
    :
    : End Sub
    :
    :
    :

  • DaveRDaveR Posts: 98Member
    : In my program, the user can click a command button to save information which is being displayed in a text box. The file name is also provided by the user in another text box called FileName. I want to be able to detect the file and if it exists, a message box will pop up asking the user if they want to overwrite the existing file or cancel. I can get the file detected but it still saves. Any ideas?
    :
    : Private Sub cmdFile_Click()
    : Dim Error
    : Dim i As Integer
    : cmdATP_Click
    : Open FileName For Output Access Write As #1
    : Print #1, ReportText
    : Close #1
    :
    : If Dir(FileName) <> vbNullString Then
    : Error = MsgBox("This File Already Exists, Over Write?", vbYesNo, "File Error")
    : End If
    :
    : End Sub

    put the file check before you do the file output and i would reccomend using the Microsotf Common Dialog ocx to make a pop up of where to save it and all that other stuff
  • Runt1128Runt1128 Posts: 18Member
    THANKS!!!!!!!!!!!!!!!
  • Runt1128Runt1128 Posts: 18Member
    : put the file check before you do the file output and i would reccomend using the Microsotf Common Dialog ocx to make a pop up of where to save it and all that other stuff


    well i got the program to work just fine. Thanks anyways. Microsot Common Dialog ocx? never worked with it. can you build on this a little more? or point me to some reading material on this.
    Thanks again.

  • KDivad LeahcimKDivad Leahcim Posts: 3,948Member
    : Check if the file exists before you write to it. Place your code in this order:
    :
    : [code]
    : Private Sub cmdFile_Click()
    : Dim Error
    : Dim i As Integer
    : Dim FileExists as Boolean
    : cmdATP_Click
    : If Dir(FileName) <> vbNullString Then
    : FileExists = True
    : Error = MsgBox("This File Already Exists, Over Write?", vbYesNo, "File Error")
    : End If
    :
    : If Error = vbYes OR Not FileExists then
    : Open FileName For Output Access Write As #1
    : Print #1, ReportText
    : Close #1
    : End if
    : End Sub
    :
    :
    : [/code]
    :
    :
    : : In my program, the user can click a command button to save information which is being displayed in a text box. The file name is also provided by the user in another text box called FileName. I want to be able to detect the file and if it exists, a message box will pop up asking the user if they want to overwrite the existing file or cancel. I can get the file detected but it still saves. Any ideas?
    : :
    : : Private Sub cmdFile_Click()
    : : Dim Error
    : : Dim i As Integer
    : : cmdATP_Click
    : : Open FileName For Output Access Write As #1
    : : Print #1, ReportText
    : : Close #1
    : :
    : : If Dir(FileName) <> vbNullString Then
    : : Error = MsgBox("This File Already Exists, Over Write?", vbYesNo, "File Error")
    : : End If
    : :
    : : End Sub
    : :
    : :
    : :
    :
    :

    Here's my version:
    [code]
    Private Sub cmdFile_Click()

    If Dir$(FileNameBox.Text) = "" Then If MsgBox("File exists. Overwrite?", vbYesNo) = vbNo Then Exit Sub
    Open FileNameBox.Text For Output As 1
    Print #1, TextToSave.Text
    Close 1

    End Sub
    [/code]
    To use the commondialog control:

    Ctrl+T
    -or-
    Right-click toolbox, click Components
    -or-
    (Menu) Project - Components

    Place a check next to Microsoft Common Dialog Control. Click OK.

    Place a copy of new control on your form.
    [code]
    CommonDialog1.ShowOpen
    FileName = CommonDialog1.FileName
    [/code]
    Look it up in the help for, it has a lot of uses, including ShowSave, Printer, Fonts, Colors. Also look at CancelError (error raised when cancel is clicked) so you can know when the user cancels. FileTitle, Filters, FilterIndex will also be helpful.

    Hope this helps!
  • DaveRDaveR Posts: 98Member
    : : put the file check before you do the file output and i would reccomend using the Microsotf Common Dialog ocx to make a pop up of where to save it and all that other stuff
    :
    :
    : well i got the program to work just fine. Thanks anyways. Microsot Common Dialog ocx? never worked with it. can you build on this a little more? or point me to some reading material on this.
    : Thanks again.
    :
    :
    goto project compents and then scroll through and serch for something called Microsoft Common Dialog. This is used all the time by programs for files (go into Wordpad and click save that is the common dialog). if you want some help coding the common dialog you can do something like this

    cdlFile.AppPath (path of the file you want to access and the file name, so if you wanted to access c:help.doc app.path would contain c:help.doc

    you will also need to set up some proeprties so that they can only choose which type of document to choose

    this is done by setting the Filter Proerty (ie "cdl.Filer = "Word Documents|*.doc"

    | = shift +

    you will also have to display the cdl this is done by cdl.showsave

  • Runt1128Runt1128 Posts: 18Member
    Thanks, ill work with this a bit and see what i can do.
Sign In or Register to comment.