+ Reply to Thread
Results 1 to 12 of 12

GoTo Blocks of code and when they activate.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    GoTo Blocks of code and when they activate.

    I have some code like this:

    Sub Macro
    
    If (expression) Then
          [Do Code]
    Else
          GoTo Err
    End If
    
    [Do more code]
    
    Err:
    [Error Handling]
    Exit Sub
    
    End Sub
    My question is... how does "Err:" work exactly...
    if my "If" statement is false and I get sent to Err, will it just complete the Err code until Exit Sub?
    if my "If" statement is True, does it ever go into the "Err" section? If not, does the code just stop before "Err" because the End Sub is directly after the Err section?

    And Finally, Is there a way to create Err as a box? like...
    Err:
    [code]
    Exit Err

    so that I can have code after it (before the end sub) that will be done if the "If" statement is True.
    If I can... if the "If" statement is False, does the code complete the "Err" section and then go BACK up to the "If" statement and run from where it left off?

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: GoTo Blocks of code and when they activate.

    General good programming habits is to avoid using goto. It creates sphaghetti code, and can generally be avoided.

    For example, in your code above, why not just have the [Error Handling] code within the Else...End If Block? If the same code is used multiple times, then it is a bit more understandable, but a separate function may be more 'clean'.

    Note also that Err is an internal variabe, which you can use to determine the type of system error. I would use something like MyErr in your code above, just to avoid potential conflicts.

    On to your specific questions:
    If false, then, yes, it will run the error handling code and then exit as you have written it.
    If true, then your error handling routine will also run since it is right after your [Do more code] block and you do not have anything before it to stop entry (such as Exit Sub).
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  3. #3
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: GoTo Blocks of code and when they activate.

    damn... u caught me! -turns around and places hands behind his back-

    the reason I want to use GoTo is because it is used multiple times... and the reason I cant use a Function instead is because im handling data in a database... so the variables are specific to that macro, and the error handling is used to close the database/record set that is defined also in multiple areas of my database.
    very good detective work sherlock

    do you know about my other set of questions? Refering to if a section can be closed with a Exit MyErr command or something?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: GoTo Blocks of code and when they activate.

    You can't have something like Exit MyErr.

    What would you be exiting exactly anyway?

    Err in your code is just a label, it doesn't define a block of code.

    As for using Goto if you need this code to run multiple times, how about looping instead?
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: GoTo Blocks of code and when they activate.

    the code isnt used multiple times, its just referenced multiple times and it would bulk up the code far more if i was to place it everywhere I need to use it.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: GoTo Blocks of code and when they activate.

    I wasn't suggesting you duplicate the code.

    Still don't see why referencing the code multiple times would require using GoTo.

    PS Why can't you use a function? Data is data, doesn't matter whether it comes from a worksheet or a database.

  7. #7
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: GoTo Blocks of code and when they activate.

    Sounds like you want to
    a) use the error handling code from multiple places
    b) be able to 'return' from the error handler back to where it was called

    That is a subroutine. I understand it may get messy with a variety of file pointers and objects, so I would suggest you either pass them as parameters or use global variables.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: GoTo Blocks of code and when they activate.

    Any chance of seeing the code and/or an explanation of what it's meant for?

  9. #9
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: GoTo Blocks of code and when they activate.

    can you use global variables in access database? i was under the impression ALL sub's were private

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: GoTo Blocks of code and when they activate.

    You could use global variables and not all subs are private.

    Access VBA is basically (exactly?) the same as Excel VBA.

    Where are you using this code?

  11. #11
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: GoTo Blocks of code and when they activate.

    in an access database, in an "Update" button

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: GoTo Blocks of code and when they activate.

    What does the code update?

    Is it executing a query that updates a table?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Code advances VERY SLOWLY and blocks after 3 columns of calculations
    By seigna in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-07-2013, 02:57 PM
  2. Using macros to identify blocks of data and summarise those blocks
    By gophbeav in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2012, 04:35 AM
  3. Need help writing a C++ DLL for VB / Excel 2003 using Code::Blocks
    By sarahman in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-22-2009, 04:15 AM
  4. [SOLVED] Goto code
    By Bob in forum Excel General
    Replies: 4
    Last Post: 07-06-2006, 12:50 PM
  5. [SOLVED] Transposing blocks of cells-What would be the code to do this?
    By Dave in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-23-2006, 12:00 PM

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.6.0 RC 1