Cancel code based on cancel in browser window

Design by Sue

Registered User.
Local time
Today, 12:46
Joined
Jul 16, 2010
Messages
809
I am using the following function found on the web to open a browser window to allow the user to select the location for a download of an excel file. All is working correctly EXCEPT if the user clicks the cancel button on the browser window the code that follows runs anyway. How can I cancel the following code if the cancel button is selected in the browser window?



Public Function GetFolderName(Optional OpenAt As String) As String
Dim lCount As Long

GetFolderName = vbNullString

With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = OpenAt
.Show
For lCount = 1 To .SelectedItems.Count
GetFolderName = .SelectedItems(lCount)
Next lCount
End With
End Function

Private Sub ExportExcel_Click()
GetFolderName

DoCmd.OutputTo acOutputReport, "SOP30DayRPT", acFormatXLS, "SOP30DayRPT - " & Format(Date, "mmddyyyy") & ".xls"

End Sub


The screenshot shows the cancel button in the browser window that I am talking about.
 

Attachments

  • Screen Shot 2024-08-26 at 11.40.34 AM.png
    Screen Shot 2024-08-26 at 11.40.34 AM.png
    26.6 KB · Views: 21
Not sure why you are looping, but Getfolder name will either be Null or ZLS if Cancel is selected.
Why not walk the code and find out what is returned?
I do not see where you are using it for the OutputTo anyway?
 
Not sure why you are looping, but Getfolder name will either be Null or ZLS if Cancel is selected.
Why not walk the code and find out what is returned?
I do not see where you are using it for the OutputTo anyway?
I have to admit I don't know why this works but by putting the GetFolderName in the output code this seems to work. I did not write the code for these but adapted what I found on line.

So if I understand your reply I can put an if statement in the GetFolderName function and if Null or ZLS then exit sub else continue?
 
Your code uses .SHOW as a trigger, but it actually does more than that. It is a function that returns TRUE (or -1) if the user clicked a file. Test the value of .SHOW before doing anything else.
 
Your code uses .SHOW as a trigger, but it actually does more than that. It is a function that returns TRUE (or -1) if the user clicked a file. Test the value of .SHOW before doing anything else.
 

Attachments

  • Screen Shot 2024-08-26 at 12.05.44 PM.png
    Screen Shot 2024-08-26 at 12.05.44 PM.png
    15.7 KB · Views: 23
That is a screenshot of what is displayed when I select Show - and it stays that way no matter what I do, cancel or select the downloads folder
 
Code:
Public Function GetFolderName(Optional OpenAt As String) As String
    Dim lCount As Long
    Dim strFolderName As String
    'GetFolderName = vbNullString

    strOpenAt = "F:\Temp\"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = OpenAt

        '.Show
        If .Show = 0 Then
            MSGBOX "Cancel was selected"
            Exit Function
        End If

        For lCount = 1 To .SelectedItems.Count
            strFolderName = .SelectedItems(lCount)
        Next lCount
    End With
    GetFolderName = strFolderName
End Function
 
Well I checked on Google.

Tried that code and it does not work, it does not end the code if the cancel is pushed.
 
Code:
Public Function GetFolderName(Optional OpenAt As String) As String
    Dim lCount As Long
    Dim strFolderName As String
    'GetFolderName = vbNullString

    strOpenAt = "F:\Temp\"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = OpenAt

        '.Show
        If .Show = 0 Then
            MSGBOX "Cancel was selected"
            Exit Function
        End If

        For lCount = 1 To .SelectedItems.Count
            strFolderName = .SelectedItems(lCount)
        Next lCount
    End With
    GetFolderName = strFolderName
End Function
Thanks - getting closer now.
 
You still have to use it in your output file?
 
Thanks - getting closer now.
Thank you for providing the code - that is really helpful but one more question and I am sure this is something I should be able to see but how do I end the code at the MsgBox. My DoCmd.OutputTo acOutputReport, "SOP30DayRPT", acFormatXLS, "SOP30DayRPT - " & Format(Date, "mmddyyyy") & ".xls" still runs
 
I would test to see if anything is returned by GetFolderName.
No need for msgbox, that was just there to show the logic.
 
Tried adding the strCancel and referencing it in my ExcelReport code - both locations it shows True but still skips over the exit sub and runs the code... Not sure what I need to do.


Public Function GetFolderName(Optional OpenAt As String) As String
Dim lCount As Long
Dim strFolderName As String
Dim strCancel As String
'GetFolderName = vbNullString

strOpenAt = "F:\Temp\"
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = OpenAt

'.Show
If .Show = 0 Then
'MsgBox "Cancel was selected"
strCancel = True
Exit Function

End If

For lCount = 1 To .SelectedItems.Count
strFolderName = .SelectedItems(lCount)
Next lCount
End With
GetFolderName = strFolderName


End Function




Private Sub ExportExcel_Click()
GetFolderName

If strCancel = True Then
Exit Sub
End If
DoCmd.OutputTo acOutputReport, "SOP30DayRPT", acFormatXLS, "SOP30DayRPT - " & Format(Date, "mmddyyyy") & ".xls"

End Sub
 
Last edited:
Tried adding the strCancel and referencing it in my ExcelReport code - both locations it shows True but still skips over the exit sub and runs the code... Not sure what I need to do.


Public Function GetFolderName(Optional OpenAt As String) As String
Dim lCount As Long
Dim strFolderName As String
Dim strCancel As String
'GetFolderName = vbNullString

strOpenAt = "F:\Temp\"
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = OpenAt

'.Show
If .Show = 0 Then
'MsgBox "Cancel was selected"
strCancel = True
Exit Function

End If

For lCount = 1 To .SelectedItems.Count
strFolderName = .SelectedItems(lCount)
Next lCount
End With
GetFolderName = strFolderName


End Function




Private Sub ExportExcel_Click()
GetFolderName

If strCancel = True Then
Exit Sub
End If
DoCmd.OutputTo acOutputReport, "SOP30DayRPT", acFormatXLS, "SOP30DayRPT - " & Format(Date, "mmddyyyy") & ".xls"

End Sub
I tried changing the strCancel=0 and thought it was working but now I can't get the code to run, it only exits sub,
 
I would test to see if anything is returned by GetFolderName.
No need for msgbox, that was just there to show the logic.
If the user selects a folder the GetFolderName does show that folder

I am just not getting referencing the GetFolderName correctly in the ExportExcel code
 
StrCancel is a string not boolean, so how can it be True? Plus it is a local variable so out of scope in excel proc. Plus you have not even declared it there, so looks like you do not have Option Explicit set?
Try what I suggested.
 
StrCancel is a string not boolean, so how can it be True? Plus it is a local variable so out of scope in excel proc. Plus you have not even declared it there, so looks like you do not have Option Explicit set?
Try what I suggested.
Yeah - I know I am picking at this because I don't know what to do. What are you referring to as "Try what I suggested" I think I have been doing that but I am sure i missed something
 
Yeah - I know I am picking at this because I don't know what to do. What are you referring to as "Try what I suggested" I think I have been doing that but I am sure i missed something
If you are referring to your statement
"I would test to see if anything is returned by GetFolderName.
No need for msgbox, that was just there to show the logic."

I know that the GetFolderName "GetFolderName = strFolderName" shows the folder I selected. And if I don't do anyting about the cancel the file is saved to the correct location. (sorry I did added the Option Explicit)
 
Walk the code with F8 and see what is returned from GetFolder.
It is a Function, so should return the Folder selected or nothing?
 

Users who are viewing this thread

Back
Top Bottom