+ Reply to Thread
Results 1 to 6 of 6

Thread: vba error handling

  1. #1
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    vba error handling

    hi!

    have code that's something like:

    For a = (3 + r - 1) To (21 + r - 1)
    
    cntry = Cells(a, 3).Value
    On Error GoTo bob
    rst.Open "something something"
       temp = rst!subs
       
             
       calc = final * temp
       
       Cells(a, 25).Value = calc
       
    
    bob:  rst.Close
       Next a

    works for a bit and they says, invalid use of null... why is this happening, why does this not trigger the error handler?

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: vba error handling

    I'd suggest a quick read through of: http://www.cpearson.com/excel/ErrorHandling.htm

    trust me - it's worth it - understanding error handling is important.

  3. #3
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: vba error handling

    Ok, read through that. as a matter of fact i'd seen it before but re-read to see if i missed something, i'm afraid i still can't see why it does not work... it seems to operate correctly for the first error but not the second... very odd

  4. #4
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: vba error handling

    To quote from the link:

    Quote Originally Posted by Chip Pearson
    Resume is the only way, aside from exiting the procedure, to get out of an error handling block.
    Do not use the Goto statement to direct code execution out of an error handling block.
    Doing so will cause strange problems with the error handlers.
    I think that pretty much covers it, no ?

    Once you invoke the handler unless you Exit or use a Resume Statement you remain within the Handler... it follows that if a subsequent error occurs then there is no error handler (you're already in it).
    Last edited by DonkeyOte; 10-22-2010 at 05:59 AM. Reason: added final sentence

  5. #5
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: vba error handling

    aaaaah i see, i was confused about remaining inside the handler..

    so i should have my error handler outside of the loop and then use a resume label to return to the loop at the desired point. i shall go work on this


    thanks for your assistance

    Jonathan

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: vba error handling

    Even better to just test for rst.eof before you attempt to read the record. If true, there's no record to read.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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