Results 1 to 11 of 11
  1. #1
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Call to Sub (VB6)

    Hi

    I am using the CommonDialog control to open and save files.

    I have put these into subroutines which I am then calling:

    Call OpenFile
    Call SaveFile

    I am having a problem with the cancel button.

    In Sub OpenFile() I have (along with other flags)

    CommonDialog1.CancelError = True
    On Error GoTo ErrorHandler2

    ErrorHandler2
    Exit Sub

    This works. The only thing is the code then continues with the Call to SaveFile when I want it to revert to the point just before Call Open File, i.e. just close the Open File Window.

    All help most gratefully received

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Call to Sub (VB6)

    Your OpenFile procedure doesn't communicate with the calling procedure, at least it's not apparent from your post. One possibility would be to handle errors at the level of the calling procedure instead of in OpenFile itself. Another possibility is to make OpenFile a function. It could return a string value: the filename if the user clicks OK, an empty string "" if the user clicks Cancel, or it could return a Boolean: True for OK, False for Cancel. The calling procedure can then act on this return value.

  3. #3
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Call to Sub (VB6)

    Hi

    I thought I'd go for the error handling in the calling procedure. And the code now looks like this

    Call OpenFile

    CommonDialog1.CancelError = True
    On Error GoTo ErrorHandler

    ErrorHandler:
    Exit Sub

    Call SaveFile

    Now I am still doing something wrong. Because if I have ErrorHandler in this position the code does not move on to Call SaveFile, and if I put ErrorHandler anywhere else then it doesn't Exit.

    Thanks

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Call to Sub (VB6)

    Leave CommonDialog1.CancelError = True in OpenFile. Change the code to look like this:

    Sub Something()
    On Error GoTo ErrorHandler
    Call OpenFile
    Call SaveFile
    Exit Sub

    ErrorHandler:
    If Err = 32755 Then
    ' Dialog canceled - ignore
    Else
    MsgBox Err.Description, vbExclamation
    End If
    End Sub

  5. #5
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Call to Sub (VB6)

    Sorry

    This is still not preventing the code from 'running on'. Even when I put Exit Sub after Call SaveFile

    Incidentially for the final code I cannot put Exit Sub after Call SaveFile.

    OpenFile is used to get the path to a file
    SaveFile is used to get a new path to save it to

    This information is then fed (via two text boxes) into a class module.

    Regards

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Call to Sub (VB6)

    Have you removed the On Error GoTo ... part from OpenFile?

    You can insert other code between Call SaveFile and Exit Sub. The Exit Sub is meant to prevent the error handling section being executed if no error occurs.

  7. #7
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Call to Sub (VB6)

    Hi

    Thanks for this.

    There is still something adrift. The code does not work (ie it runs on) the first time Cancel is pressed, but if I use a message box like this:

    Dim Response As Integer

    Call OpenFile

    Response = MsgBox("Open?", vbYesNo)
    If Response = vbNo Then
    Exit Sub
    End If

    And click No when the code runs on, and then use the click procedure that calls Call OpenFile again, Cancel then works fine!

    Regards

    p.s. There is no error handling in OpenFile now.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Call to Sub (VB6)

    Could you attach a zip file with a stripped down copy of your project? Remove everything that isn't relevant for this problem.

  9. #9
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Call to Sub (VB6)

    Hi

    Thanks very much for this. File attached.

    Regards

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Call to Sub (VB6)

    In the OpenCompressedFile procedure, you call CommonDialog1.ShowOpen before setting CommonDialog1.CancelError = True. Since the setting in design mode is False, no error occurs if the user cancels the Open dialog. To remedy this, either move the line CommonDialog1.CancelError = True to before CommonDialog1.ShowOpen, or set CancelError to True in design mode; you don't need to set it in code then.

  11. #11
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Call to Sub (VB6)

    Hi

    Thanks very much for this. Have set CancelError to True in design mode and all works fine now.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •