Solved

Saving a file using a common Dialog box

Posted on 2002-04-28
5
404 Views
Last Modified: 2012-06-27
I have a VB application that has Crystal Reports integrated into it. The code below causes the report object  that I am currently using to be saved to a specific name “SaveReport.RPT” in the current application path.  Could somebody help me modify this code so that when the button is clicked it will open up a common dialog box (defaulted to the  current App path)  and prompt the user for a file name to save the report to? I also need it to warn if there is a file that exists already with the proposed file name.
Thanks

Private Sub cmdSave_Click()
    m_Report.SaveAs App.Path & "\SavedReport.RPT", cr80FileFormat
    MsgBox "Report has been saved as SavedReport.RPT in" & vbCrLf & App.Path, vbInformation
End Sub
0
Comment
Question by:DavidNPD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6975187
add a common dialog to your project:Something like...

With CDiag1
    .DefaultExt = "rpt"
    .Filter = "Crystal files (*.rpt)|*.rpt"
    .InitDir = App.Path
    .ShowSave
    If .FileName <> "" Then
        If Dir$(.FileName, vbArchive) <> "" Then
            m_Report.SaveAs .FileName, cr80FileFormat
            MsgBox "Report has been saved as SavedReport.RPT in" & vbCrLf & App.Path, vbInformation
        Else
            If MsgBox("File already exist. Overwrite?", vbOKCancel, App.EXEName) = vbOK Then
                         m_Report.SaveAs .FileName, cr80FileFormat
            End If
        End If
    End If
End With
0
 

Author Comment

by:DavidNPD
ID: 6975221
Richie,
This is exactly what I need, and works great except for one small error. Even if there is no file in the directory to be overwritten I am still being advised that a file exists? Is their a slight error in the "If Then" statement?
Dave
0
 

Author Comment

by:DavidNPD
ID: 6975226
I have been looking at this a little more. If I do try to save it as the same name as a file that exists, I DO NOT get the error but I get the "Report has been saved as SavedReport.RPT" message. Are the statement reversed and also can I have it actually say the correct saved file name as opposed to the hardcoded ""SavedREport.rpt" one I was using?
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 80 total points
ID: 6975252
Richie is on the right track, however I would suggest the following as it is easier:

On Error GoTo ErrHandler        ' Add this line

With CDiag1
   .DefaultExt = "rpt"
   .Filter = "Crystal files (*.rpt)|*.rpt"
   .InitDir = App.Path
   .CancelError = True          ' Add this line
   .Flags = cdlOFNOverwritePrompt   ' Add this line
   .ShowSave
   m_Report.SaveAs .FileName, cr80FileFormat
End With

Exit Sub

ErrHandler:
' More than likely Cancel from the Common Dialog box was pressed, otherwise check for other errors.


Anthony
0
 

Author Comment

by:DavidNPD
ID: 6975267
acperkins,
That works perfect for me, I have it incorperated already. Richie thank you for your quick response and code.
Dave
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Put text in a picture ASP.NET C# 2 73
Windows Script Host failed (Access is denied.) error 6 449
How to Add / Edit Windows Menu 4 80
pop out of webbrowser1 control vba6 5 50
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question