Solved

Saving a file using a common Dialog box

Posted on 2002-04-28
5
403 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
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

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

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

713 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