+ Reply to Thread
Results 1 to 5 of 5

Thread: VBA Code Sequence Question

  1. #1
    Valued Forum Contributor
    Join Date
    04-23-2009
    Location
    IOWA
    MS-Off Ver
    2010 Professional
    Posts
    270

    VBA Code Sequence Question

    In the following Code, would the "Exit Here:" code run even if there is no error? Because sometimes I get an error when I have my close in both places.

    Private Sub Template()
        Dim rsXXXXX As New ADODB.Recordset
        
    On Error GoTo HandleError
        
        rsXXXXX.Open "Select * From tblName Where FieldName ='" & CompareString & "'", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
        If Not rsXXXXX.EOF Then
            With rsXXXXX
                
            End With
        End If
        rsXXXXX.Close
        Set rsXXXXX.ActiveConnection = Nothing
        
    ExitHere:
        rsXXXXX.Close
        Set rsXXXXX.ActiveConnection = Nothing
        Exit Sub
        
    HandleError:
        MsgBox Err.Description
        Resume ExitHere
        
    End Sub

    Thanks in advance,

    Dan
    Last edited by split_atom18; 05-28-2010 at 09:41 AM. Reason: Marked Solved & Removed Post Icon
    "I am not a rocket scientist, I am a nuclear engineer." - Split_atom18
    If my advice has been helpful to you, then please help me by clicking on the "Star" and adding to my reputation, Thanks!

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,757

    Re: VBA Code Sequence Question

    Sure. You need an Exit Sub before Exit Here.

    And this is a preferable declaration:
    Private Sub Template()
        Dim rsXXXXX     As ADODB.Recordset
    
        Set rsXXXXX = New ADODB.Recordset
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    04-23-2009
    Location
    IOWA
    MS-Off Ver
    2010 Professional
    Posts
    270

    Re: VBA Code Sequence Question

    Thank you this answers my question perfectly!!!
    I can use the preferable, side note question. Why? i.e. why have 2 statements to do one?

    Thanks in advance,

    Dan
    Last edited by shg; 05-27-2010 at 05:54 PM. Reason: deleted quote
    "I am not a rocket scientist, I am a nuclear engineer." - Split_atom18
    If my advice has been helpful to you, then please help me by clicking on the "Star" and adding to my reputation, Thanks!

  4. #4
    Valued Forum Contributor
    Join Date
    04-23-2009
    Location
    IOWA
    MS-Off Ver
    2010 Professional
    Posts
    270

    Re: VBA Code Sequence Question

    Another question on topic:

    Since my code seems to be buggy at best. (i.e. I am learning)

    I want to ensure I close connections even if there is an error. I learned that if the .close and =nothing are after the resume exithere, then I end up in an endless loop.

    So should I use:

    Private Sub Template()
        Dim rsXXXXX As New ADODB.Recordset
        
    On Error GoTo HandleError
        
        rsXXXXX.Open "Select * From tblName Where FieldName ='" & CompareString & "'", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
        If Not rsXXXXX.EOF Then
            With rsXXXXX
                
            End With
        End If
        rsXXXXX.Close
        Set rsXXXXX.ActiveConnection = Nothing
        
    ExitHere:
        Exit Sub
        
    HandleError:
        MsgBox Err.Description
        rsXXXXX.Close
        Set rsXXXXX.ActiveConnection = Nothing
        Resume ExitHere
        
    End Sub

    Thanks in advance,

    Dan
    "I am not a rocket scientist, I am a nuclear engineer." - Split_atom18
    If my advice has been helpful to you, then please help me by clicking on the "Star" and adding to my reputation, Thanks!

  5. #5
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,757

    Re: VBA Code Sequence Question

    Why? i.e. why have 2 statements to do one?
    Read this code and decide what the two msgboxes should say. Then run it.
    Sub x()
        Dim col As New Collection
        
        MsgBox col Is Nothing
        Set col = Nothing
        MsgBox col Is Nothing
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0