Results 1 to 4 of 4

Thread: Proper way of canceling process?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    What is the proper way to let the user cancel a running process?

    For example, I have an application which when the user clicks the start button, a function is called and begins it's processing.. I have included a Cancel button on the form just in case the user does not want to complete the processing.

    What I did is created a msgbox that pops up when the user hits Cancel which asks them if they really wish to cancel the process. If they hit Yes, then it stop, otherwise, it continues.

    Here's the code I used:

    Code:
    Private Sub cmdCancel_Click()
        Dim xVar As Integer
        
        xVar = MsgBox("Are you sure you wish to cancel?", _
              vbQuestion + vbYesNo)
        
        If xVar = 6 Then
            '-- cancel operation
            Unload Me
        End If
        
    End Sub
    But I think I'm missing something because the following situation occurs:

    When the user clicks Cancel and hits Yes, the Form unloads but an error occurs because the process that is being canceled was using an ADO recordset and I put RS.Close in the Form_Unload event to close the connection but it seems that the process wants to keep continuing. The error states "Operation is not allowed when the object is closed."

    This is obviously due to the fact that I'm closing the recordset but I have no choice. I do not want to keep it open.

    My question is, how do I stop the process dead in it's tracks if the user selects Yes so that it does not try to process any further?

    Any help would be appreciated..

    Dan

  2. #2
    Guest
    Try this:

    Code:
    Private Sub cmdCancel_Click()
        Dim xVar As Integer
        
        xVar = MsgBox("Are you sure you wish to cancel?", _
              vbQuestion + vbYesNo)
        
        If xVar = 6 Then
            '-- cancel operation
            Unload Me
            Set Form1 = Nothing
            End
        End If
        
    End Sub

  3. #3
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Thumbs up give this a go

    In the function or subroutine where the ADO recordset is
    processing, use an error handling routine, like this:
    Code:
    On Error GoTo Database_Error
    
    'put your loop or process or whatever here
    myRecordset.PerformWhateverFunctionYouAreDoing
    
    Exit Sub 'exit the subroutine
    
    Database_Error:
         Select Case Err.Number
              Case 1234567890 'replace this with the number of
    'the error that occurs.  I have seen that error before but
    'I can't remember the number
                   Exit Sub 'leave the subroutine without 
    'showing an error message
               Case Else 'other error occured
                    msgbox "Error & " Err.Number & ": " & Err.Description, vbCritical
         End Select
    
    End Sub


    [Edited by DrewDog_21 on 09-16-2000 at 01:33 PM]

  4. #4
    Guest

    just try exit sub

    If you want to ignore the message just put
    "On error resume next" under whatever subroutine the error occurs


    Like this:

    Private Sub cmdCancel_Click()
    On error resume next
    Dim xVar As Integer

    xVar = MsgBox("Are you sure you wish to cancel?", _
    vbQuestion + vbYesNo)

    If xVar = 6 Then
    '-- cancel operation
    Unload Me
    End If

    End Sub









    Or you can go a step further and do this

    Private Sub cmdCancel_Click()
    On error goto getdahellouttahere
    Dim xVar As Integer

    xVar = MsgBox("Are you sure you wish to cancel?", _
    vbQuestion + vbYesNo)

    If xVar = 6 Then
    '-- cancel operation
    Unload Me
    End If
    getdahellouttahere:
    exit sub
    End Sub




    but the most simple way would to just:

    Private Sub cmdCancel_Click()
    Dim xVar As Integer

    xVar = MsgBox("Are you sure you wish to cancel?", _
    vbQuestion + vbYesNo)

    If xVar = 6 Then
    '-- cancel operation
    Unload Me <-----Replace this simply with "Exit Sub"
    End If

    End Sub






    Have Phun



    "There is more than one way to skin a cat"




Posting Permissions

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



Click Here to Expand Forum to Full Width