trapping info in an error message 
Author Message
 trapping info in an error message

Quick question, hopefully a simple answer.

I'm writing an error handler for a procedure that among other things creates
a number of temporary queries and tables.  One error condition that can
occur is the creation of a query or table that already exists which
generates

    run-time error '3012':  object 'ObjectName' already exists.

In this case, I want to delete the already existing object and then resume
and recreate it from scratch.

The problem is, how can I extract the ObjectName from the error message with
my error handler so that I know WHICH query or table to delete?  This
particular procedure creates many different objects.  I can think of some
rather inelegant ways of doing this but I'm wondering if there's something
quick and dirty.  Any ideas?

Sincerely,
April



Sun, 12 May 2002 03:00:00 GMT  
 trapping info in an error message

Quote:

> Quick question, hopefully a simple answer.

> I'm writing an error handler for a procedure that among other things creates
> a number of temporary queries and tables.  One error condition that can
> occur is the creation of a query or table that already exists which
> generates

>     run-time error '3012':  object 'ObjectName' already exists.

> In this case, I want to delete the already existing object and then resume
> and recreate it from scratch.

> The problem is, how can I extract the ObjectName from the error message with
> my error handler so that I know WHICH query or table to delete?  This
> particular procedure creates many different objects.  I can think of some
> rather inelegant ways of doing this but I'm wondering if there's something
> quick and dirty.  Any ideas?

> Sincerely,
> April

If you pass the name of the object into a variable before trying to create it
each time then if there is an error you know that that variable contains the
name of the object which already exists

### START SAMPLE CODE ###

On Error GoTo ErrPara

    Dim strObjName As String
    Dim qry1 As QueryDef
    Dim db1 As Database

    Set db1 = CurrentDb()

    strObjName = "qry1"
    Set qry1 = db1.CreateQueryDef(strObjName, "SELECT * FROM [Table1];")

ExitPara:
    Exit Function

ErrPara:
    Select Case Err.Number
        Case 3012
            MsgBox "Query exists"
            DoCmd.DeleteObject acQuery, strObjName
            Resume
        Case Else
            MsgBox Err.Number & vbCrLf & vbCrLf & Err.Description
            Resume ExitPara
    End Select

### END SAMPLE CODE ###

Regards,

--

Roger E K Stout

Programmer, DMDS
EASAMS Ltd.
Tel. 01785 785339
Fax 01785 244397



Mon, 13 May 2002 03:00:00 GMT  
 trapping info in an error message
Dear Roger,

Thank-you very much for your solution.  That's kind of what I figured I'd
have to do.  Your solution confirms that.  I was kind of hoping that the Err
object had some property from which I could extract the info I needed
directly.

Again thanks for your code!

April



Quote:

> > Quick question, hopefully a simple answer.

> > I'm writing an error handler for a procedure that among other things
creates
> > a number of temporary queries and tables.  One error condition that can
> > occur is the creation of a query or table that already exists which
> > generates

> >     run-time error '3012':  object 'ObjectName' already exists.

> > In this case, I want to delete the already existing object and then
resume
> > and recreate it from scratch.

> > The problem is, how can I extract the ObjectName from the error message
with
> > my error handler so that I know WHICH query or table to delete?  This
> > particular procedure creates many different objects.  I can think of
some
> > rather inelegant ways of doing this but I'm wondering if there's
something
> > quick and dirty.  Any ideas?

> > Sincerely,
> > April

> If you pass the name of the object into a variable before trying to create
it
> each time then if there is an error you know that that variable contains
the
> name of the object which already exists

> ### START SAMPLE CODE ###

> On Error GoTo ErrPara

>     Dim strObjName As String
>     Dim qry1 As QueryDef
>     Dim db1 As Database

>     Set db1 = CurrentDb()

>     strObjName = "qry1"
>     Set qry1 = db1.CreateQueryDef(strObjName, "SELECT * FROM [Table1];")

> ExitPara:
>     Exit Function

> ErrPara:
>     Select Case Err.Number
>         Case 3012
>             MsgBox "Query exists"
>             DoCmd.DeleteObject acQuery, strObjName
>             Resume
>         Case Else
>             MsgBox Err.Number & vbCrLf & vbCrLf & Err.Description
>             Resume ExitPara
>     End Select

> ### END SAMPLE CODE ###

> Regards,

> --

> Roger E K Stout

> Programmer, DMDS
> EASAMS Ltd.
> Tel. 01785 785339
> Fax 01785 244397



Mon, 13 May 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Trapping Warning Messages, NOT Error Messages

2. Getting info when trapping errors???

3. Trap and Replace Error Messages

4. how to trap ADO update error instead of a message box

5. How do I trap spelling error message fr embedded word OLE object

6. onerror trap doesn't trap error!

7. Info trapped in Buffer

8. Info trapped in Buffer

9. Trapping file sharing errors w/o ON ERROR?

10. DAO Error Trapping & Most Common Errors

11. Deadlock errors & vb error trapping

12. on error (trapping error)

 

 
Powered by phpBB® Forum Software