Solved

Saving a file using a common Dialog box

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 start screen issues 9 55
using Access 8 75
message box in access 4 51
Excel VBA - Run Time error '1004' Application-defined or object-defined error 4 204
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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