Beginner VB

Moderators: None (Apply to moderate this forum)
Number of threads: 1244
Number of posts: 2990

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Existing FIle Posted by Runt1128 on 1 Apr 2002 at 12:05 PM
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


Report
Re: Existing FIle Posted by hobby on 1 Apr 2002 at 12:29 PM
Check if the file exists before you write to it. Place your code in this order:

 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





: 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
:
:
:

Report
Re: Existing FIle Posted by Runt1128 on 1 Apr 2002 at 12:45 PM
THANKS!!!!!!!!!!!!!!!
Report
Re: Existing FIle Posted by KDivad Leahcim on 1 Apr 2002 at 1:49 PM
: Check if the file exists before you write to it. Place your code in this order:
:
:
:  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
: 
: 
: 

:
:
: : 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:
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

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.
CommonDialog1.ShowOpen
FileName = CommonDialog1.FileName

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!
Report
Re: Existing FIle Posted by DaveR on 1 Apr 2002 at 12:40 PM
: 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
Report
Re: Existing FIle Posted by Runt1128 on 1 Apr 2002 at 1:01 PM
: 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.

Report
Re: Existing FIle Posted by DaveR on 1 Apr 2002 at 3:31 PM
: : 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

Report
Re: Existing FIle Posted by Runt1128 on 2 Apr 2002 at 6:00 AM
Thanks, ill work with this a bit and see what i can do.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.