Saving a file using a common Dialog box

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
DavidNPDAsked:
Who is Participating?
 
Anthony PerkinsCommented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
DavidNPDAuthor Commented:
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
 
DavidNPDAuthor Commented:
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
 
DavidNPDAuthor Commented:
acperkins,
That works perfect for me, I have it incorperated already. Richie thank you for your quick response and code.
Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.