[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 731
  • Last Modified:

CommonDialog Cancel button handling in VB6

I am using Common Dialog to open the specified directory and give the user an ability to select the file and import it in.
But if I click the Cancel and don't select any file - it errorws out.  I need to know how to handle the Cacel button.
0
Aquarus
Asked:
Aquarus
  • 2
1 Solution
 
nffvrxqgrcfqvvcCommented:
You can have it throw and error or you can just ignore cancel and check for a valid filename location.

If you add this line then an cdlCancel = 32755 (&H7FF3) will be thrown which you could trap.
CommonDialog1.CancelError = True

No error will be thrown
CommonDialog1.CancelError = False
0
 
HooKooDooKuCommented:
There are two possible ways to handle this.

1. Before showing the window, you can set the common dialog property "CanelError" = False.  Then you will have to look at the contents of the return value to determine if the user cancled.

2. Add an error handler.  Catch Error #32755 to know the user clicked "Cancel"
  ...
  On Error Goto CatchError
  CommonDialog1.ShowOpen
  'Do stuff with selected File Here
  Exit Sub
CatchError:
  if Err.Number = 32755 then
    'Do stuff here based on the fact user clicked Cancel
  else
    'Some other Error happened - handle it here
  endif

0
 
nffvrxqgrcfqvvcCommented:
Forgot to attach the example by using cdlOFNFileMustExist flag it prevents the user from adding bogus information in this case the file must exists.
Private Sub Command1_Click()

Dim szFileName As String

CommonDialog1.CancelError = False
CommonDialog1.Flags = cdlOFNFileMustExist
CommonDialog1.ShowOpen
szFileName = CommonDialog1.FileName

If LenB(szFileName) <> 0 Then
  Debug.Print szFileName
Else
  Debug.Print "Cancel"
End If

CommonDialog1.FileName = vbNullString

End Sub

Open in new window

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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now