473,408 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Coding Best Practise

171 100+
Hi Guys,
I have lots of issues with my SQL or other long VBA code when I get an error I can't pinpoint the exact problem immediatley. What I have read on A C# forum is that Parrameters should be used so a problem can be immediatley identified in the code. Now I understand this is VBA & SQL but could I get some suggetions on how I can do coding so I meet the following 2 objectives:

1. Be able to find errors easily myself or have Access identify the error on its own.
2. If A professional programmer wants to continue Edit/Improve/Understand my Database (Apart from reading my 'Comments) they can easily understand the code.
Mar 13 '08 #1
9 1382
ADezii
8,834 Expert 8TB
Hi Guys,
I have lots of issues with my SQL or other long VBA code when I get an error I can't pinpoint the exact problem immediatley. What I have read on A C# forum is that Parrameters should be used so a problem can be immediatley identified in the code. Now I understand this is VBA & SQL but could I get some suggetions on how I can do coding so I meet the following 2 objectives:

1. Be able to find errors easily myself or have Access identify the error on its own.
2. If A professional programmer wants to continue Edit/Improve/Understand my Database (Apart from reading my 'Comments) they can easily understand the code.
It is too broad a Topic to make any specific recommendations on. Are you referring to Syntax, Compile-time, Run-time, or Logic Errors? Several chapters, if not a book itself, can be written on how to detect and deal with Errors of all sorts. Any good reference book on Access/VBA should have a chapter or two that deals specifically with Error Handling and Debugging. I would suggest that this is the best approach to take on the subject.
Mar 13 '08 #2
iheartvba
171 100+
It is too broad a Topic to make any specific recommendations on. Are you referring to Syntax, Compile-time, Run-time, or Logic Errors? Several chapters, if not a book itself, can be written on how to detect and deal with Errors of all sorts. Any good reference book on Access/VBA should have a chapter or two that deals specifically with Error Handling and Debugging. I would suggest that this is the best approach to take on the subject.
Thanks Mate, Thats a great starting point. Cheers
Mar 13 '08 #3
marcf
17
As a tip regarding readability of code I always ask people to do the following:

1) Come up with meaningful variable names.

2) Variable names should contain some kind of reference to their declaration ie for strings Stmyvar, integers, Intmyvar and so on.

3) When adding to your code dont just chuck stuff in at random places try to make your code do one thing at a time.

4) While jumping into subroutines to perform repiticous tasks can cut down on the length of your code sometimes having multiple subroutines branching off just to cut down on 5-10 lines of code can seriously impact readability. If you are set on doing that then I would suggest you keep a separate document that lists each function and subroutine along with their parameters and a summary of what each one does. I find it very handy to do this as if you havent worked on an app in a while you can work alot faster rather than trying to remember what each one does.

If you do those things and above all put decent comments in your code will be very readable and easy for someone else to update.

Error handling is another issue all together, my error handlers are as verbose as possible, I use a set global variables that are set in each function/event that contain useful information to be displayed (along with the error message) in the error handler, exactly what information you set is up to you and depends on your application.

Anyway thats my two pence worth


Marc
Mar 13 '08 #4
iheartvba
171 100+
When I first started coding I thought ... Error handling, I don't need to do that Access already provides me with such usefull errors like, INVALID SQL Statement SELECT... and even if it doesn't if my program has errors, I will get a phone call from the user. Now I think...hmmm this is why an experienced programmer gets paid so much more than an unexperienced programmer. You have given great advice in the post, if only I had understood it and adhered to it sooner... anyways never too late to start.
Nov 26 '08 #5
puppydogbuddy
1,923 Expert 1GB
And last, but not least....a great reference source on the subject:

Error Handling and Debugging Tips and Techniques for Microsoft Access, VBA, and Visual Basic (VB6)
Nov 26 '08 #6
rsmccli
52
@puppydogbuddy
That is a very good rundown on error handling. Thanks for that.
Nov 26 '08 #7
ADezii
8,834 Expert 8TB
I usually like to deploy a simple, but effective Global Error Logger in my Projects. The following Public Sub-Routine will record the Date/Time, Error Number and Description, and Procedure where the Error originated, to a file named Errors.txt. This File will reside in the same Folder as the *.mdb.
Expand|Select|Wrap|Line Numbers
  1. Public Sub LogErrors(dteDate As Date, lngErrNum As Long, strErrDesc As String, strProcedure As String)
  2. Open CurrentProject.Path & "\ErrLog.txt" For Append As #1
  3. Print #1, "****************************************************************************"
  4. Print #1, "Date/Time: " & Now() & vbCrLf & "Number: " & lngErrNum & vbCrLf & _
  5.           "Description: " & strErrDesc & vbCrLf & "Procedure: " & strProcedure
  6. Print #1, "****************************************************************************"
  7.  
  8. Close #1
  9. End Sub
Sample Errors.txt:
Expand|Select|Wrap|Line Numbers
  1. ****************************************************************************
  2. Date/Time: 10/26/2008 1:34:52 PM
  3. Number: 6
  4. Description: Overflow
  5. Procedure: frmPFDPension_cmdConvertSick_Click()
  6. ****************************************************************************
  7. ****************************************************************************
  8. Date/Time: 11/26/2008 8:35:48 AM
  9. Number: 16
  10. Description: Expression too complex
  11. Procedure: frmPFDPension_cmdCalculatePension_Click()
  12. ****************************************************************************
Nov 26 '08 #8
missinglinq
3,532 Expert 2GB
A big problem we see here all the time is in the naming of objects. Retaining the names assigned by Access when an object is created is a surefire path to confusion! A day after creating Text0 or Command2 you won’t remember what they are or what they do. Another programmer following in your footsteps certainly won’t.

Another problem, as I see it, is in what I call the “miniaturization” of code. In the early days, when 20 mb hard drives were the norm, there was a genuine need to have code as compact as possible. Programmers actually had contests to see who could accomplish a task with the least amount of code. But those days are long behind us! We now have the luxury of making code not only functional but legible as well.

OBDoctor.Visible = Me.IsPregnant

is nice and concise, but it’s also rather cryptic.

If Me.IsPregnant = True Then
OBDoctor.Visible = True
Else
OBDoctor.Visible= False
End If

lets you know exactly what is going on. Clarity is always preferable over brevity, in my mind; this is particularly true when offering advice to newbies.

Another example:

CheckIt = IIf(TestMe > 1000, "Large", "Small")

is a concise, legible way of saying

If TestMe > 1000 Then
CheckIt = “Large”
Else
CheckIt = “Small”
End If

But

=IIf(IsNull([arrival date]),"In Progress",IIf(IsNull([completion date]),"In Progress",IIf([arrival date]=[completion date],"Deadline Obtained",IIf(DateDiff("d",[arrival date],[completion date])<=5,"Deadline Obtained",IIf(DateDiff("d",[arrival date],[completion date])>5,"Deadline Missed","false")))))]

is a nightmare to scan, and even worse to debug when things go awry!

And, of course, commenting code, which most of us are loath to do, is still a necessary evil. Even clearly written code can become a muddle six months down the road.


Linq ;0)>
Nov 26 '08 #9
ADezii
8,834 Expert 8TB
Clarity is always preferable over brevity, in my mind; this is particularly true when offering advice to newbies.
Excellent words of wisdom by Linq!
Nov 26 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Aadam | last post by:
Does Microsoft have a best practices for tracking errors? (like in a database, what info, etc)
8
by: s.subbarayan | last post by:
Dear all, In one of our projects in a document about C coding standard it is stated as "Always check a pointer is NULL before calling free. Always set a free'd pointer to NULL to try to protect...
10
by: Barry Morris | last post by:
Hi I am a php newbie although I have been a programmer for years, this can be dangerous because all the languages I know use = as equal comparison so it took me a long time to debug if ($STRING...
3
by: cbrown | last post by:
I am rebuilding an existing application that relies on an SQL DB. The app is a scheduling/employee management program. My question pertains to best practices in dotnet and database. I use a 3...
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
4
by: =?Utf-8?B?VzFsZDBuZTc0?= | last post by:
When one architects a new project one of the first steps in the decision is to decide on the layers. (In my opinion anyway) One architecture that I have used before is to go solid OO and create...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
3
by: Peter D. | last post by:
I have been programming PHP for a while now and always seem to run into the same problem when working on more than trivial apps. Most of my coding is for personal projects anyway so it really isn't...
41
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post by:
Hi, I am after suggestions on the best practice declaring and destroying objects. some example code: Private Sub MySub Dim frmMyForm As MyForm Try
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.