Results 1 to 6 of 6
  1. #1
    stumped is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    20

    Error loop when I misscode in a function?

    Hi All, when I write a function to use in an Access query more often than not I'll get it wrong at first and I get stuck in a loop and prompted by various dialogue boxes and basically I struggle to break out of the error loop. Any pointers please?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,728
    Don't start by having a major loop ie 500 iterations.
    Step through your code -- use F8 -- research it.
    Get familiar with DEBUG.Print statements

    see debugging at http://www.cpearson.com/excel/debug.htm

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    If you code is stuck in a loop, you can often use Ctrl-Break to stop it, at which point you can get into debug mode to see what the problem is.

    for me, I find that an infinite loop is often caused by a missing .movenext while processing a recordset.

    HTH

    John

  4. #4
    stumped is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    20
    Hi Both, thanks for getting back. appreciate the advice about a smaller dataset but the problem was only occurring deep within a large number of records. In this case it was working with dates and I have to write small functions to deal with the fact that the dates were in a text format and sometimes missing, null's, zero length strings, spaces etc. So it wasn't until I ran the query with a return of several thousand records that any errors in my function are found. the Microsoft VB for Applications Windows ap[pears with the error highlighted yellow, but I just want to break out of the query which i can seem to do. I can't recall exactly but i get dialogs with end buttons etc and prompted "this will stop the debugger", eventually something works but it is very scientific and I would like to understand better. My keyboard does not have a 'break', key!

  5. #5
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    One thing you can (and should) do is implement proper error handling procedures in your VBA functions. Doing that gives you complete control (well almost, anyway) over what happens when MS Access generates a run-time error. Most importantly, if done right, it prevents MS Access from going into debug mode and stopping the query. In your case, since the functions are being used to return values in a query, you might want the error handler to return a distinct "error" value. Your functions would look something like this, in skeleton form:

    Function MyFunction(...parameter list...)
    Dim...
    On Error GoTo ErrorProcedure
    ...
    ... Function code
    ...
    MyFunction = normal return value
    Exit Function
    ErrorProcedure:
    ...
    ... Error processing code
    ...
    MyFunction = Error value
    End Function

    I would not recommend putting a MsgBox in the error handling code, especially if the errors are frequent, but using debug.print to record information about the bad data values would help.

    John

  6. #6
    stumped is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    20
    Thanks John, very helpful.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error with Do While Loop
    By Icewolf0927 in forum Programming
    Replies: 2
    Last Post: 06-11-2012, 10:47 AM
  2. Error Handling Loop...Stuck Inside!
    By Soule in forum Programming
    Replies: 4
    Last Post: 02-23-2012, 07:10 PM
  3. New tables created in a loop causing error
    By shubhamgandhi in forum Programming
    Replies: 2
    Last Post: 07-20-2011, 05:16 PM
  4. Replies: 8
    Last Post: 05-16-2011, 06:01 PM
  5. Error: Loop without Do
    By eric.opperman1@gmail.com in forum Programming
    Replies: 4
    Last Post: 01-25-2011, 02:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums