Solved

When to Dim variables - before or after 'On Error'

Posted on 2003-12-08
5
277 Views
Last Modified: 2010-05-01
Within a procedure/function or property:  Is it best to Dim variables before or after the 'On Error' statement?

For example:

Public Sub func()
   Dim sBuffer As String

   On Error Goto EH
   :
   :
   Exit Sub
EH:
   :

End Sub

or

Public Sub func()
   On Error Goto EH

   Dim sBuffer As String
   :
   :
   Exit Sub

EH:
   :
End Sub

Thanks.
0
Comment
Question by:halfondj
5 Comments
 
LVL 4

Assisted Solution

by:TomLaw1999
TomLaw1999 earned 62 total points
Comment Utility
Convention seems to be to dim varibles first.
0
 
LVL 4

Assisted Solution

by:learning_t0_pr0gram
learning_t0_pr0gram earned 62 total points
Comment Utility
I always dim the variables first...
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 62 total points
Comment Utility
I always put my On Error at the top since it is only one line and it makes it very clear that Error trapping is occuring within the Function/Sub.
0
 

Accepted Solution

by:
DaPen earned 64 total points
Comment Utility
There are times when having the "On Error" command running with in your code can prevent errors from being passed properly (ActiveX Controls).  Also, sometimes you have to specifically place an "On Error" to a specific handler around a piece of code while the rest of the sub routine uses a different (generic) handler.  An example of this might be when the generic handler runs it always calls the specific handler code but under particular situations you only need to run the specific handler code.  All this being said, the best place to put an "On Error" statement is before the code that is going to use it.  "DIM" statements are not normally considered executable code and therefore placing an "On Error" before them would be of no use.
0
 

Author Comment

by:halfondj
Comment Utility
Thanks for all the answers.  Since there doesn't seem to be a wrong answer, I split the points.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now