Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. YAVH - Yet another VBA horror

YAVH - Yet another VBA horror

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasehelpcareer
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rob Grainger
    wrote on last edited by
    #1

    Well another from the goldmine of coding horrors that is VBA... Here, two tables have similar structures, and need to be processed similarly (don't ask)... Naturally, I paraphrase to protect the innocent.

    Sub SomeSub()
    Dim rs As Recordset
    Dim tblname As String
    Dim i As Integer

    On Error GoTo Err\_Handler
    
    tblname = "Table1"
    
    For i = 0 To 1
        Set rs = db.OpenRecordset("SELECT \* FROM " & tblname & " WHERE ... ")
    
        ' Code to process records here
        ' Including...
    

    NextTable:

        tblname = "Table2"
    Next i
    
    ' Remainder of code
    
    Exit Sub
    

    Err_Handler:

    ' Code to log error
    Resume NextTable   ' Note that there's no real error-checking, just logging.
    

    End Sub

    (I should mention that code to process records here expands to a couple of hundred lines) Why, oh why, not define another procedure and call it...

    Sub SomeSub()
    ' Replacement for above
    ProcessRecords "Table1"
    ProcessRecords "Table2"
    End Sub

    Sub ProcessRecords(tableName AS String)
    ' Code to process records here
    ' When errors occur, can simply return (ideally after some real error-checking)
    End Sub

    Makes you want to hurl.

    O H O 3 Replies Last reply
    0
    • R Rob Grainger

      Well another from the goldmine of coding horrors that is VBA... Here, two tables have similar structures, and need to be processed similarly (don't ask)... Naturally, I paraphrase to protect the innocent.

      Sub SomeSub()
      Dim rs As Recordset
      Dim tblname As String
      Dim i As Integer

      On Error GoTo Err\_Handler
      
      tblname = "Table1"
      
      For i = 0 To 1
          Set rs = db.OpenRecordset("SELECT \* FROM " & tblname & " WHERE ... ")
      
          ' Code to process records here
          ' Including...
      

      NextTable:

          tblname = "Table2"
      Next i
      
      ' Remainder of code
      
      Exit Sub
      

      Err_Handler:

      ' Code to log error
      Resume NextTable   ' Note that there's no real error-checking, just logging.
      

      End Sub

      (I should mention that code to process records here expands to a couple of hundred lines) Why, oh why, not define another procedure and call it...

      Sub SomeSub()
      ' Replacement for above
      ProcessRecords "Table1"
      ProcessRecords "Table2"
      End Sub

      Sub ProcessRecords(tableName AS String)
      ' Code to process records here
      ' When errors occur, can simply return (ideally after some real error-checking)
      End Sub

      Makes you want to hurl.

      O Offline
      O Offline
      Oakman
      wrote on last edited by
      #2

      Oh please. Your way might be slightly more elegant, but it is neither more robust, nor faster. You haven't fixed anything, nor really changed anything.

      The practical reason for freedom is that freedom seems to be the only condition under which any kind of substantial moral fiber can be developed — we have tried law, compulsion and authoritarianism of various kinds, and the result is nothing to be proud of. ~ Albert Jay Nock

      D J R 3 Replies Last reply
      0
      • O Oakman

        Oh please. Your way might be slightly more elegant, but it is neither more robust, nor faster. You haven't fixed anything, nor really changed anything.

        The practical reason for freedom is that freedom seems to be the only condition under which any kind of substantial moral fiber can be developed — we have tried law, compulsion and authoritarianism of various kinds, and the result is nothing to be proud of. ~ Albert Jay Nock

        D Offline
        D Offline
        Dan Pratt
        wrote on last edited by
        #3

        Ehhh.. from a maintainability perspective, if they share the same processing logic having the code in a single place is an improvement. World-shaking? No. Improvement? Yes. http://en.wikipedia.org/wiki/Don't_repeat_yourself[^]

        1 Reply Last reply
        0
        • O Oakman

          Oh please. Your way might be slightly more elegant, but it is neither more robust, nor faster. You haven't fixed anything, nor really changed anything.

          The practical reason for freedom is that freedom seems to be the only condition under which any kind of substantial moral fiber can be developed — we have tried law, compulsion and authoritarianism of various kinds, and the result is nothing to be proud of. ~ Albert Jay Nock

          J Offline
          J Offline
          Jeremy Hutchinson
          wrote on last edited by
          #4

          I'd view it as a huge improvement. Take a look at how you'd be hopping around the code on error conditions with what amounts to goto statements (resume NextTable). The original posters change would be the first thing I'd do as a refactor.

          S 1 Reply Last reply
          0
          • J Jeremy Hutchinson

            I'd view it as a huge improvement. Take a look at how you'd be hopping around the code on error conditions with what amounts to goto statements (resume NextTable). The original posters change would be the first thing I'd do as a refactor.

            S Offline
            S Offline
            supercat9
            wrote on last edited by
            #5

            Jeremy Hutchinson wrote:

            Take a look at how you'd be hopping around the code on error conditions with what amounts to goto statements (resume NextTable).

            VB6 error handling is icky regardless. Depending upon local variable usage, that 'for loop' style may be reasonable. Not great, but given the lack of nice array initializers in VB6 I wouldn't call it horrible. The alternative would be to have a "select case" statement at the start of the loop to set the table name.

            1 Reply Last reply
            0
            • R Rob Grainger

              Well another from the goldmine of coding horrors that is VBA... Here, two tables have similar structures, and need to be processed similarly (don't ask)... Naturally, I paraphrase to protect the innocent.

              Sub SomeSub()
              Dim rs As Recordset
              Dim tblname As String
              Dim i As Integer

              On Error GoTo Err\_Handler
              
              tblname = "Table1"
              
              For i = 0 To 1
                  Set rs = db.OpenRecordset("SELECT \* FROM " & tblname & " WHERE ... ")
              
                  ' Code to process records here
                  ' Including...
              

              NextTable:

                  tblname = "Table2"
              Next i
              
              ' Remainder of code
              
              Exit Sub
              

              Err_Handler:

              ' Code to log error
              Resume NextTable   ' Note that there's no real error-checking, just logging.
              

              End Sub

              (I should mention that code to process records here expands to a couple of hundred lines) Why, oh why, not define another procedure and call it...

              Sub SomeSub()
              ' Replacement for above
              ProcessRecords "Table1"
              ProcessRecords "Table2"
              End Sub

              Sub ProcessRecords(tableName AS String)
              ' Code to process records here
              ' When errors occur, can simply return (ideally after some real error-checking)
              End Sub

              Makes you want to hurl.

              H Offline
              H Offline
              Hired Mind
              wrote on last edited by
              #6

              Eeeewww. Yeah I would definitely change that code. But if you do change it, make sure you change it to use query parameters instead of building SQL from scratch. It's not so bad when the tablename is hard-coded into the method itself but if passed in from outside, it's a huge security hole. (Some future slave might decide to call the method with user-provided input, and it won't be properly escaped) It's so hard to find good slaves today...

              Before .NET 4.0, object Universe = NULL;

              1 Reply Last reply
              0
              • R Rob Grainger

                Well another from the goldmine of coding horrors that is VBA... Here, two tables have similar structures, and need to be processed similarly (don't ask)... Naturally, I paraphrase to protect the innocent.

                Sub SomeSub()
                Dim rs As Recordset
                Dim tblname As String
                Dim i As Integer

                On Error GoTo Err\_Handler
                
                tblname = "Table1"
                
                For i = 0 To 1
                    Set rs = db.OpenRecordset("SELECT \* FROM " & tblname & " WHERE ... ")
                
                    ' Code to process records here
                    ' Including...
                

                NextTable:

                    tblname = "Table2"
                Next i
                
                ' Remainder of code
                
                Exit Sub
                

                Err_Handler:

                ' Code to log error
                Resume NextTable   ' Note that there's no real error-checking, just logging.
                

                End Sub

                (I should mention that code to process records here expands to a couple of hundred lines) Why, oh why, not define another procedure and call it...

                Sub SomeSub()
                ' Replacement for above
                ProcessRecords "Table1"
                ProcessRecords "Table2"
                End Sub

                Sub ProcessRecords(tableName AS String)
                ' Code to process records here
                ' When errors occur, can simply return (ideally after some real error-checking)
                End Sub

                Makes you want to hurl.

                O Offline
                O Offline
                oggenok64
                wrote on last edited by
                #7

                What i really like the most is the "Err_Handler". So beautiful, so classical, so elegant :-)

                1 Reply Last reply
                0
                • O Oakman

                  Oh please. Your way might be slightly more elegant, but it is neither more robust, nor faster. You haven't fixed anything, nor really changed anything.

                  The practical reason for freedom is that freedom seems to be the only condition under which any kind of substantial moral fiber can be developed — we have tried law, compulsion and authoritarianism of various kinds, and the result is nothing to be proud of. ~ Albert Jay Nock

                  R Offline
                  R Offline
                  Rob Grainger
                  wrote on last edited by
                  #8

                  Except that I have to maintain and enhance this code. Refactoring code for elegance is not wasted time - every future maintainer will appreciate your effort.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups