Results 1 to 9 of 9

Thread: an exception for GoTos?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2002
    Posts
    638

    an exception for GoTos?

    i was just wondering, if you had a sub that creates object variables (such as recordsets), and in the middle of this sub you wanted to exit it, would it be a good idea to use a GoTo that jumps to the end of the sub that sets these object variables to Nothing, or is there a better way? eg:

    VB Code:
    1. Private Sub MySub()
    2.     Dim rs As ADODB.Recordset
    3.     Dim rs2 As ADODB.Recordset
    4.  
    5.     Set rs = New ADODB.Recordset
    6.     Set rs2 = New ADODB.Recordset
    7.  
    8.     If ([i]some condition[/i]) Then
    9.         If ([i]another condition[/i]) Then
    10.             'do something
    11.         Else
    12.             GoTo CleanUp
    13.         End If
    14.     Else
    15.         'do something else
    16.     End If
    17.  
    18.     'Do some more code
    19.  
    20. CleanUp:
    21.     Set rs = Nothing
    22.     Set rs2 = Nothing
    23. End Sub

    thanks

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    This is how I always do it.

    VB Code:
    1. Private rs As ADODB.Recordset
    2. Private rs2 As ADODB.Recordset
    3.  
    4. Private Sub MySub()
    5.  
    6.     Set rs = New ADODB.Recordset
    7.     Set rs2 = New ADODB.Recordset
    8.  
    9.     If (some condition) Then
    10.         If (another condition) Then
    11.             'do something
    12.         Else
    13.             doCleanUp
    14.             Exit Function
    15.         End If
    16.     Else
    17.         'do something else
    18.     End If
    19.  
    20.     'Do some more code
    21.  
    22. End Sub
    23.  
    24. Public Sub doCleanUp()
    25.     On Error Resume Next
    26.     rs.Close: Set rs = Nothing
    27.     rs2.Close: Set rs2 = Nothing
    28. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2002
    Posts
    638
    ah, yeah, that's one way. so you'd have doCleanup() in a module? what if you're not just using 2 recordsets at any one time though, and you want to set several objects to Nothing?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2002
    Posts
    638
    i suupose you could do this couldn't you?

    VB Code:
    1. Private Sub MySub()
    2.     'blah blah
    3.  
    4.     Call doCleanup(rs1, rs2)
    5. End Sub
    6.  
    7. Public Sub doCleanup(ParamArray obj() As Variant)
    8.     Dim i As Integer
    9.  
    10.     For i = 0 To UBound(obj)
    11.         Set obj(i) = Nothing
    12.     Next i
    13. End Sub

    or does that seem retarded? i also don't know if parameters are passed ByRef when using ParamArray.

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well you could have an array of recordsets, or pass them as a paramarray to the cleanup sub

    VB Code:
    1. Public RS() As RecordSet
    2.  
    3. Public Sub doCleanUp()
    4.     Dim i As Long
    5.     For i = 0 To UBound(RS)
    6.         RS(i).Close: Set RS(i) = Nothing
    7.     Next
    8. End Sub

    Or :

    VB Code:
    1. Public RS1 As RecordSet, RS2 As RecordSet '' etc.
    2.  
    3. Public Sub doCleanUp(ParamArray RS() As Variant)
    4.     Dim i As Long
    5.     For i = 0 To UBound(RS)
    6.         RS(i).Close: Set RS(i) = Nothing
    7.     Next
    8. End Sub
    9.  
    10.  
    11. '' and then call from inside the sub as follows :
    12. If (some Condition) Then
    13.     doCleanUp RS1, RS2, RS3, .... RSn
    14. End If
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    you beat me to it
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2002
    Posts
    638
    ah, cool, thanks!

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: an exception for GoTos?

    I actually use something very simliar to what you posted - but rather than have a goto I just let it fall out of the if's.. although in the situation in your example you need a variable to keep track of whether to do the following code or not.

    VB Code:
    1. Private Sub MySub()
    2.     Dim rs As ADODB.Recordset
    3.     Dim rs2 As ADODB.Recordset
    4.  
    5.     Dim FallOut as Boolean
    6.  
    7.     Set rs = New ADODB.Recordset
    8.     Set rs2 = New ADODB.Recordset
    9.  
    10.     If ([i]some condition[/i]) Then
    11.         If ([i]another condition[/i]) Then
    12.             'do something
    13.         Else
    14.  
    15.            FallOut = True
    16.         '    GoTo CleanUp
    17.  
    18.         End If
    19.     Else
    20.         'do something else
    21.     End If
    22.  
    23.  
    24.     If Not(FallOut) Then
    25.  
    26.        'Do some more code
    27.  
    28.    End If
    29.  
    30.  
    31. CleanUp:
    32.     Set rs = Nothing
    33.     Set rs2 = Nothing
    34. End Sub

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2002
    Posts
    638
    thanks for that suggestion, but i think i'll go for the option of using one sub that handles the cleaning up of objects.

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