473,466 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to break out of loop from error handler?

I have a logging routine that's supposed to silently log errors caught by
error handler code on certain functions. The problem is sometimes stuff
happens and the error handler can get caught in a loop. Is there some way
to send a break from VBA code to break out of the loop?

Here's what the error handler code looks like:

[MyForm module]
Private Function MyFunction
On Error GoTo HandleErr
[code here]
Exit_Here:
Exit Function
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr "MyForm", "MyFunction"
End Select
Resume Exit_Here
End Function

Here's what modHandler.LogErr looks like:

[modHandler module]
Public Sub LogErr(strErrFrm As strString, strErrFunc As String)
Dim db As DAO.Database
Dim strErrMsg As String
Dim strSql As String
strErrMsg = Err.Description
strSql = "INSERT INTO tblErrorLog ( ErrMsg, ErrFrm, ErrFunc ) " & _
"VALUES (strErrMsg, strErrFrm, strErrFunc);
Set db = CurrentDb
db.Execute strSql
Set db = Nothing
End Sub

If something weird happens, I don't want LogErr to get caught in a loop. I
was thinking of something like this:

Public Sub LogErr(strErrFrm As String, strErrFunc As String)
Dim db As DAO.Database
Dim strErrMsg As String
Dim strSql As String
If Nz(Forms("MyForm")!txtLastErr, Now() - 1) >= (Now() - 0.00001) Then
MsgBox "we're in a loop"
[***code to break the loop?***]
Else
Forms("MyForm")!txtLastErr = Now()
strErrMsg = Err.Description
strSql = "INSERT INTO tblErrorLog ( ErrMsg, ErrFrm, ErrFunc ) " & _
"VALUES (strErrMsg, strErrFrm, strErrFunc);
Set db = CurrentDb
db.Execute strSql
Set db = Nothing
End If
End Sub

Is there some way -- other than Application.Quit :) -- to break out of the
loop? Other options?

Thanks in advance.
Nov 13 '05 #1
3 15936
I don't see anything in your code which could generate a loop.
However, if you did something like this:

[MyForm module]
Private Function MyFunction
On Error GoTo HandleErr
dim rst as dao.recordset
rst.open ....

[code here]
Exit_Here:
rst.close
set rst=nothing
Exit Function
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr "MyForm", "MyFunction"
End Select
Resume Exit_Here
End Function

If rst somehow gets set to nothing too early, rst.close will generate an
error, and the handler will resume at Exit_Here, causing rst.close to try to
execute again, and generating another error.

I usually deal with that sort of a loop by putting a condition on the
execution:
If (rst is nothing)=false, then rst.close

Perhaps you can locate the source of your "loop" and address that...
"deko" <de**@nospam.com> wrote in message
news:ka********************@comcast.com...
I have a logging routine that's supposed to silently log errors caught by
error handler code on certain functions. The problem is sometimes stuff
happens and the error handler can get caught in a loop. Is there some way
to send a break from VBA code to break out of the loop?

Here's what the error handler code looks like:

[MyForm module]
Private Function MyFunction
On Error GoTo HandleErr
[code here]
Exit_Here:
Exit Function
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr "MyForm", "MyFunction"
End Select
Resume Exit_Here
End Function

Here's what modHandler.LogErr looks like:

[modHandler module]
Public Sub LogErr(strErrFrm As strString, strErrFunc As String)
Dim db As DAO.Database
Dim strErrMsg As String
Dim strSql As String
strErrMsg = Err.Description
strSql = "INSERT INTO tblErrorLog ( ErrMsg, ErrFrm, ErrFunc ) " & _
"VALUES (strErrMsg, strErrFrm, strErrFunc);
Set db = CurrentDb
db.Execute strSql
Set db = Nothing
End Sub

If something weird happens, I don't want LogErr to get caught in a loop. I was thinking of something like this:

Public Sub LogErr(strErrFrm As String, strErrFunc As String)
Dim db As DAO.Database
Dim strErrMsg As String
Dim strSql As String
If Nz(Forms("MyForm")!txtLastErr, Now() - 1) >= (Now() - 0.00001) Then
MsgBox "we're in a loop"
[***code to break the loop?***]
Else
Forms("MyForm")!txtLastErr = Now()
strErrMsg = Err.Description
strSql = "INSERT INTO tblErrorLog ( ErrMsg, ErrFrm, ErrFunc ) " & _ "VALUES (strErrMsg, strErrFrm, strErrFunc);
Set db = CurrentDb
db.Execute strSql
Set db = Nothing
End If
End Sub

Is there some way -- other than Application.Quit :) -- to break out of the
loop? Other options?

Thanks in advance.

Nov 13 '05 #2
Your too late by the time you get there.

You need to break the loop before the problem

For example, this will loop forever:-
Function xxx()
On Error GoTo xxx_error
Err.Raise 1300 + vbObjectError
xxx_ret:
Err.Raise 1301 + vbObjectError
Exit Function
xxx_error:
Resume xxx_ret
End Function

One way to break this would be:-
Function xxx()
On Error GoTo xxx_error
Err.Raise 1300 + vbObjectError
xxx_ret:
On Error Resume Next
Err.Raise 1301 + vbObjectError
Exit Function
xxx_error:
Resume xxx_ret
End Function

--
Terry Kreft
MVP Microsoft Access
"deko" <de**@nospam.com> wrote in message
news:ka********************@comcast.com...
I have a logging routine that's supposed to silently log errors caught by
error handler code on certain functions. The problem is sometimes stuff
happens and the error handler can get caught in a loop. Is there some way
to send a break from VBA code to break out of the loop?

Here's what the error handler code looks like:

[MyForm module]
Private Function MyFunction
On Error GoTo HandleErr
[code here]
Exit_Here:
Exit Function
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr "MyForm", "MyFunction"
End Select
Resume Exit_Here
End Function

Here's what modHandler.LogErr looks like:

[modHandler module]
Public Sub LogErr(strErrFrm As strString, strErrFunc As String)
Dim db As DAO.Database
Dim strErrMsg As String
Dim strSql As String
strErrMsg = Err.Description
strSql = "INSERT INTO tblErrorLog ( ErrMsg, ErrFrm, ErrFunc ) " & _
"VALUES (strErrMsg, strErrFrm, strErrFunc);
Set db = CurrentDb
db.Execute strSql
Set db = Nothing
End Sub

If something weird happens, I don't want LogErr to get caught in a loop. I was thinking of something like this:

Public Sub LogErr(strErrFrm As String, strErrFunc As String)
Dim db As DAO.Database
Dim strErrMsg As String
Dim strSql As String
If Nz(Forms("MyForm")!txtLastErr, Now() - 1) >= (Now() - 0.00001) Then
MsgBox "we're in a loop"
[***code to break the loop?***]
Else
Forms("MyForm")!txtLastErr = Now()
strErrMsg = Err.Description
strSql = "INSERT INTO tblErrorLog ( ErrMsg, ErrFrm, ErrFunc ) " & _ "VALUES (strErrMsg, strErrFrm, strErrFunc);
Set db = CurrentDb
db.Execute strSql
Set db = Nothing
End If
End Sub

Is there some way -- other than Application.Quit :) -- to break out of the
loop? Other options?

Thanks in advance.

Nov 13 '05 #3
> Your too late by the time you get there.

You need to break the loop before the problem


I think you're right. I was looking for a way to break any loop from any
code in a centralized error handler, but that's not going to happen without
Application.Quit.
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jay | last post by:
G'day all This registration form checks for the submit button then displays the next form from the include statement. But before it displays the next form it will check to make sure the user...
6
by: Sandman | last post by:
Ok, so I have PHP set upp to ato_prepend and auto_append files to every script I run. So if I someone surfs to /index.php, these scripts run: init.php -> set up DB connections and...
5
by: viza | last post by:
Hi! Suppose I have int i,j,k; for(i=0;i<I;++i){ /* loop 1 */ for(j=0;j<J;++j){ /* loop 2 */ for(k=0;k<K;++k){ /* loop 3 */ if(test){
5
by: Samuel R. Neff | last post by:
When you have an unhandled exception in vb.net how do you view the exception information in the debugger? In C# the debugger creates a local variable that points to the exception and you can...
10
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
4
by: raylopez99 | last post by:
See comment below. This is a simple problem but I'm a little rusty. How to break out of a event loop (here _Paint)? I've tried if/else, case, etc but not quite what I want--I keep getting the JIT...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.