Solved

Saving a file using a common Dialog box

Posted on 2002-04-28
5
399 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
  • 3
5 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
acperkins,
That works perfect for me, I have it incorperated already. Richie thank you for your quick response and code.
Dave
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now