Solved

Common Dialog and Cancel Button

Posted on 2000-02-16
5
736 Views
Last Modified: 2010-05-18
I am using the Common Dialog control to allow a user to save an access report to a word doc(acutally RTF).  If the user press cancel on the save as form of the common Dialog i want it to exit the sub all together.

So far no such luck in it working.  It either stills saves the document or drops to the default save as for RTF files.

TIA
John
0
Comment
Question by:JTD3
5 Comments
 
LVL 1

Expert Comment

by:SeanGraflund
ID: 2527754
JTD3,

try this in your sub ..


Sub SaveAsRTF()

On Error Goto Err_Handle


'This is where your code is

Err_Handle:
    Exit Sub






0
 

Author Comment

by:JTD3
ID: 2527807
I have encluded the on error routine, but it does not trigger the exit of the function.

Any other ideas?

John
0
 
LVL 10

Expert Comment

by:brewdog
ID: 2527877
Here is code that we use to do just what you're talking about:

Private Function FileDialog() as String

On Error GoTo FileDialogError

FileDialog = ""                   'start as empty string so we always have a string

With CommonDialog1
    .DialogTitle = sTitle
    .Filter = sFilter
    .Flags = lFlags
    .FileName = sFileName
    .ShowSave
End With

FileDialog = CommonDialog1.FileName

Exit Function

FileDialogError:
    MsgBox Err.Number & ", " & Err.Description
    FileDialog = ""

End Function

This way if they cancel and/or hit some kind of an error, this function still returns a string. You can do the same thing with the code by declaring a string variable, setting it to an empty string, and then setting it to an empty-string on the error, too.

Hope that helps . . .

brewdog
0
 
LVL 6

Accepted Solution

by:
blakeh1 earned 20 total points
ID: 2528093
Here is a routine thaqt will check for the cancel error
Private Sub Command1_Click()
             Dim oCmnDlg As CommonDialog
             Dim strFormat As String
             Dim strFileName As String
             Set oCmnDlg = Me!cdlgSave.Object
             On Error GoTo Export_Err
             With oCmnDlg
                ' Clear the file name before prompting.
                .FileName = ""
                ' Prompt the user if the file already exists.
                .Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
                ' Determine if the cancel button was pressed.
                .CancelError = True
 
                .Filter = _
                "MS-DOS Text (*.txt)|*.txt|Rich Text Format (*.rtf)|*.rtf" _
                & "|Microsoft Excel (*.xls)|*.xls"
 
                .FilterIndex = 1
 
                ' Default the initial directory to "C:\My Documents".
                .InitDir = "C:\My Documents"
                .ShowSave
 
                ' Determine what file type was selected; then save the
                ' file in that format.
                Select Case .FilterIndex
                   Case 1 ' MS-DOS Text Format.
                      strFormat = acFormatTXT
 
                   Case 2 ' Rich Text Format.
                      strFormat = acFormatRTF
 
                   Case 3 ' Microsoft Excel Format.
                      strFormat = acFormatXLS
                End Select
                strFileName = .FileName
             End With
             DoCmd.OutputTo acOutputTable, "Generaltbl", _
                strFormat, strFileName
             Exit Sub
 
Export_Exit:
             Exit Sub
 
Export_Err:
             ' Check to see if the cancel button was pressed.
             If Err.Number = 32755 Then 'cancel was used
                Resume Export_Exit
             Else
                MsgBox Err.Number & ",    " & Err.Description
             End If
             Resume Export_Exit
End Sub
0
 

Author Comment

by:JTD3
ID: 2529006
Worked like a charm.  Thank you very much.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

920 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

14 Experts available now in Live!

Get 1:1 Help Now