tags:

views:

2931

answers:

4

Hi,

I am using in the first part of my program

on error go to start

suppose in my second part i am again using

on error resume next

this second error trap will not get activated as teh first one will be still active.Is there any way to de activate the first error after it is used. Hope I am clear

Set objexcel = CreateObject("excel.Application")
                     objexcel.Visible = True
                     On Error GoTo Openwb
                     wbExists = False
                     Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
                     Set objSht = wbexcel.Worksheets("Sheet1")
                     objSht.Activate
                     wbExists = True
Openwb:

                     On Error GoTo 0
                     If Not wbExists Then
                     objexcel.Workbooks.Add
                     Set wbexcel = objexcel.ActiveWorkbook
                     Set objSht = wbexcel.Worksheets("Sheet1")

                     End If

                     On Error GoTo 0

Set db = DBEngine.opendatabase("C:\book.mdb")
Set rs = db.OpenRecordset("records")

Set rs2 = CreateObject("ADODB.Recordset")
rs2.ActiveConnection = CurrentProject.Connection


For Each tdf In CurrentDb.TableDefs

   If Left(tdf.Name, 4) <> "MSys" Then
        rs.MoveFirst
        strsql = "SELECT * From [" & tdf.Name & "] WHERE s=15 "

        Do While Not rs.EOF
            On Error Resume Next

            rs2.Open strsql

ON execution of last statement i wantto ignore the error and move on to next table but error handling does not seem to work

thanks

A: 

Hi,

try

On Error Goto 0

For further help look here: http://msdn.microsoft.com/en-us/library/bb258159.aspx

Regards, divo

0xA3
that doesnt seem to work
tksy
Could you post your source code here? Maybe you are using the on error statements incorrectly.
0xA3
+1  A: 

You need to clear the error. Try putting this code in:

If Err.Number > 0 Then
    Err.Clear
End If

You can also use Err.Number to handle specific error cases.

Jason Z
This doesnt seem to wwork either.THis code works if the first part of excel is put in a seperate sub but i want it like this as of now.
tksy
I don't think it's ever a good idea to just discard all errors. What if you discard an error you're not expecting?
David-W-Fenton
David, good point. That is why I said you can use Err.Number to handle specific error cases. Rather than doing a generic clear all, just clear if the error is one you were expecting.
Jason Z
+3  A: 

It is nearly always better to avoid errors, rather than handling them. For example:

Set objexcel = CreateObject("excel.Application")
objexcel.Visible = True

'On Error GoTo Openwb '
'wbExists = False '

If Dir("C:\REPORT3.xls") = "" Then
    objexcel.Workbooks.Add
    Set wbexcel = objexcel.ActiveWorkbook
    Set objSht = wbexcel.Worksheets("Sheet1")
Else
    Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
    Set objSht = wbexcel.Worksheets("Sheet1")
End If

objSht.Activate
'wbExists = True '
Remou
Thanks that did the job. But as a doubt isnt it possible anyway to cancel out an error handling after it is used before proceeding further in the program.
tksy
Quibble: in VBA it's more efficient when testing for a zero-length string to usee vbNullString instead of "" because that Access constant already has its memory allocated. It doesn't matter in this constant, but it's good to get in the habit so you always use vbNullString inside loops.
David-W-Fenton
@tksy : "On Error GoTo 0" does exactly that. It returns VBA to its usual error handling. It doesn't mean "On Error GoTo Start" which i think your question implies.
Mark Nold
A: 

On error goto 0 give hand to visual basic for error treatment (in general message box)

On error goto label will redirect your code to label:

On error resume next will ignore the error and continue

Resume next redirect the code to the next line after the error is raised

it means that combinations of instructions such as

On Error goto 0
...
On Error goto 0

do not make sense

And if you want to redirect an "on error" instruction you'll have to do it this way:

Do While Not rs.EOF

    On Error Resume Next
    rs2.Open strsql
    On error Goto 0

    rs2.moveNext

Loop

If you want to redirect an error to a label (for treatment or whatever) and then go back to the code where the error occured, you have to write something like:

On error goto label
...
...
On error goto 0
exit sub (or function)

label:
....
resume next
end function

But I really advise you to be more rigorous on your error management. You should first be able to do something like that:

Set objexcel = CreateObject("excel.Application")
objexcel.Visible = True

On Error GoTo error_Treatment
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
Set objSht = wbexcel.Worksheets("Sheet1")
objSht.Activate
wbExists = True
On error GoTo 0

Set db = DBEngine.opendatabase("C:\book.mdb")
Set rs = db.OpenRecordset("records")

Set rs2 = CreateObject("ADODB.Recordset")
rs2.ActiveConnection = CurrentProject.Connection

For Each tdf In CurrentDb.TableDefs
    ....
    'there are a number of potential errors here in your code'
    'you should make sure that rs2 is closed before reopening it with a new instruction'
    'etc.'
Next tdf

Exit sub

error_treatment:
SELECT Case err.number
   Case **** '(the err.number raised when the file is not found)'
       objexcel.Workbooks.Add
       Set wbexcel = objexcel.ActiveWorkbook
       Set objSht = wbexcel.Worksheets("Sheet1")
       Resume next 'go back to the code'
   Case **** '(the recordset cannot be opened)'
       ....
       ....
       Resume next 'go back to the code'
   Case **** '(whatever other error to treat)'
       ....
       ....
       Resume next 'go back to the code'
   Case Else
       debug.print err.number, err.description '(check if .description is a property of the error object)'
       'your error will be displayed in the immediate windows of VBA.' 
       'You can understand it and correct your code until it runs'
End select
End sub

The next step will be to anticipate the errors in your code so that the err object will not be raised. You can for example write a generic function like this one:

Public function fileExists (myFileName) as Boolean

You can then take advantage of this function in your code by testing the existence of your xls file:

if fileExists("C:\REPORT3.xls") Then
    Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
Else
   objexcel.Workbooks.Add
   Set wbexcel = objexcel.ActiveWorkbook
Endif        
Set objSht = wbexcel.Worksheets("Sheet1")
objSht.Activate

You do not need your wbExist variable anymore...

In the same way, you should anticipate the case where your recordset has no records. Writing down rs.MoveFirst before testing it could raise an error. You should then write

If rs.EOF and rs.BOF then
Else
    rs.moveFirst
    Do while not rs.EOF
         rs.moveNext
    Loop
Endif
Philippe Grondier