What does this do? Does this affect the whole routine? or just the line right after the statement?
Thanks in advance
Seahag
Printable View
What does this do? Does this affect the whole routine? or just the line right after the statement?
Thanks in advance
Seahag
It affects the procedure it is in from the line of the statement on.
Thats what i thought.. but it seems to affect the whole Sub.
I forced it to do an error a few line down from the On error..
Doesn't do anything.. just ignors it..
Any Suggestions why?
That is what it does. I fit riuns into an error, it SKIPS over it and keeps going...
VB Code:
Option Explicit Sub main() ' On Error Resume Next FileCopy "g:\files\sales\rep3\1036\toolbox\toolbox.exe", _ "C:\Program Files\toolbox\toolbox.exe" Test "g:\files\sales\rep3\1036\sdtcost", _ "C:\Program Files\toolbox\sdtcost" Shell "C:\Program Files\toolbox\toolbox.exe", vbMinimizedFocus End Sub Function Test(SeverFolder As String, DestFolder As String) Dim oFileSystem Set oFileSystem = CreateObject("Scripting.FileSystemObject") oFileSystem.CopyFolder SeverFolder, DestFolder, True Set oFileSystem = Nothing End Function
The g:\ does not exist.. so i should get the second error al least..
but i dont..
On Error Resume Next is generally used in events (like the Click event, or the Form_Unload event. It doesn't trap or respond to errors, as James stated. It is a method of preventing run time errors that would blow the user back to their desktop.
Your On Error Resume Next is at the top of your routine. Therefore, it covers ALL errors in the routine. On Error Resume Next will contine if an error occurs ANYWHERE Below the On Error line. If you recoded your routine like thisQuote:
Orginally posted by SeaHag
The g:\ does not exist.. so i should get the second error al least..
but i dont..
your sub would blow up because g:\ didn't exist.VB Code:
Option Explicit Sub main() ' FileCopy "g:\files\sales\rep3\1036\toolbox\toolbox.exe", _ "C:\Program Files\toolbox\toolbox.exe" Test "g:\files\sales\rep3\1036\sdtcost", _ "C:\Program Files\toolbox\sdtcost" On Error Resume Next Shell "C:\Program Files\toolbox\toolbox.exe", vbMinimizedFocus End Sub
Quote:
Originally posted by SeaHag
VB Code:
Option Explicit Sub main() ' On Error Resume Next FileCopy "g:\files\sales\rep3\1036\toolbox\toolbox.exe", _ "C:\Program Files\toolbox\toolbox.exe" Test "g:\files\sales\rep3\1036\sdtcost", _ "C:\Program Files\toolbox\sdtcost" Shell "C:\Program Files\toolbox\toolbox.exe", vbMinimizedFocus End Sub\ Function Test(SeverFolder As String, DestFolder As String) Dim oFileSystem Set oFileSystem = CreateObject("Scripting.FileSystemObject") oFileSystem.CopyFolder SeverFolder, DestFolder, True Set oFileSystem = Nothing End Function
The g:\ does not exist.. so i should get the second error al least..
but i dont..
Instead of On error resume next, try on error goto errhandler
'Erhandler is where you can handle a specific error message in this case, you know that a Drive doesn't exists, so you could display a message telling the user of the same. Do you get the general idea?
Cool Thanks all.. Very Helpfull..
:)
Any errors after it are ignored.
Do you actually read anything anyone else posts?Quote:
Originally posted by DiGiTaIErRoR
Any errors after it are ignored.
Sometimes I do. :p
:rolleyes:
My post is less technical than the other though.
Who wouldn't undertsand it?
You?
Did anyone catch the first reply???:confused:Quote:
Any errors after it are ignored.
However I wouldnt recommend using Resume Next
Because if the error that has occured is going to affect a number of things further down your code you can still end up blowing out your application by making it freeze up when it keeps skipping lines as the errors snowball.
You would be better off trying to trap the error telling the user what went wrong and taking them back to the step they were on before the error occured.
Generally, Resume Next is okay. Resume is the one you want to avoid.Quote:
Originally posted by rudvs2
However I wouldnt recommend using Resume Next
Because if the error that has occured is going to affect a number of things further down your code you can still end up blowing out your application by making it freeze up when it keeps skipping lines as the errors snowball.
You would be better off trying to trap the error telling the user what went wrong and taking them back to the step they were on before the error occured.
However I agree you should trap the error
Trapping the error in usually perfered, but there are times when ignoring the error (with On Error Resume Next) is the only way to go.
Case in point: the form unload event where database objects, and recordset objects etc. are closed and set to nothing. There are times when the application is opened, and then closed right away, so setting these objects to nothing, in the Form_Unload event, would result in a runtime error because they were never created or opened. On Error Resume Next handles this situation quite nicely.
Not necessarily; like any error handler you can cancel it by using On Error Goto 0.Quote:
Originally posted by seoptimizer2001
It affects the procedure it is in from the line of the statement on.
I use On Error Resume Next only before Compiling it ( final release ) to prevent bug than I didn't see :)
On Error Resume Next is only good in ONE scenario: form resizing and control resizing. Other than that, I consider it to be as low and evil as Variant. :)
You right but I always block in resize like : if form1.width <2000 then form1.width=2001 ;)
I think that trapping the error is the best way to go. Then after you check the error number use a resume next from the error handler, if it is an error you expected. Otherwise you may be oblivious to a very simple and stupid error that is occuring.
When using an On Error statement (no matter if you use Resume Next or Goto LabelName) in a procedure it's valid until the procedure exits or an On Error Goto 0 statement is found.Quote:
Originally posted by SeaHag
VB Code:
Option Explicit Sub main() ' On Error Resume Next FileCopy "g:\files\sales\rep3\1036\toolbox\toolbox.exe", _ "C:\Program Files\toolbox\toolbox.exe" Test "g:\files\sales\rep3\1036\sdtcost", _ "C:\Program Files\toolbox\sdtcost" Shell "C:\Program Files\toolbox\toolbox.exe", vbMinimizedFocus End Sub Function Test(SeverFolder As String, DestFolder As String) Dim oFileSystem Set oFileSystem = CreateObject("Scripting.FileSystemObject") oFileSystem.CopyFolder SeverFolder, DestFolder, True Set oFileSystem = Nothing End Function
The g:\ does not exist.. so i should get the second error al least..
but i dont..
So even though you don't have any error checks in your Test procedure above the On Error Resume Next statement in main is still valid since it haven't reached the end of that procedure yet.
When an error occure in the Test procedure it immidiatly bails out to return to the next line in the Main procedure.
So in this case it will not reach the line where you set oFileSystem to Nothing.
Best regards
I pretty much only use On Error Resume Next when I do form resizing and I don't particularly care whether I handle errors or not. On Error Goto 0 will reset error handling to the original error handling state in the routine.
Occasionally I use On Error Resume Next with Common DIalog boxes when I have the CancelError proerty set to true and I really don't feel like handling the error, just checking for a FileName length.
I also use On Error Resume Next in ASP pages because they don't support GoTo labels. If I expect something may cause an error, I can still check the Err Object.
Thanks for you thoughts all. :)
Actually On Error Resume Next is perfect for what I am doing.
I have a program copy a set of files from the network every morning. I do this to keep everyone updated on what I am doing.
Some of the people here have mobile computers. So if the network isn't ther .Error.. error .. error ... not so good.
I hate errors..
Seems to work ok..
:)
Well, you can always use On Local Error Resume Next, that'll only affect the sub that called it :)