Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-12-08
5
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Assisted Solution

by:TomLaw1999
TomLaw1999 earned 248 total points
ID: 9898950
Convention seems to be to dim varibles first.
0
 
LVL 4

Assisted Solution

by:learning_t0_pr0gram
learning_t0_pr0gram earned 248 total points
ID: 9899778
I always dim the variables first...
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 248 total points
ID: 9901054
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 256 total points
ID: 9901888
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
ID: 9903737
Thanks for all the answers.  Since there doesn't seem to be a wrong answer, I split the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

721 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