This is a bit of a cross post from another forum - but might be of use...
A lot of question on here seem to be from beginners, and some keep on re-appearing. So below some hints to help out:
Use Option Explicit
Place this in the first file that gets called in your ASP page. It will force you to declare any variables that you use and stop typo's
Make use of include files for site wide functionality
This will allow you to specify common site variables (database connection strings), and common functions ( sendMail?) in one place on your site. This can also be used for templating a site (top.asp / bottom.asp).
Use Functions and Subs to encapsulate code
This puts a level of scope on your variables and stops different things interfering with each other.
Use some kind of hungarian style notation ( str, int, lng, obj), for variables
This will help you figure out what each variable is as you use it. For example there was the merit table posting on here, what is merittable? rsMeritTable would have given everyone a much better clue.
Use Friendly variable names - Stevusuk
There isn't any reason why you would want to name a variable "x" when it could be "intPage"
Comment code, you might not remember why you did it that way in a couple of months time
Where possible try and split your code into the appropriate layer
The standard three tiers of development are data, business and presentation. Try to keep these apart in your code and everything will work much better.
Avoid arrays etc. like the plague
The RecordSet object is perfectly nice to work with, stop trying to pull data into nameless arrays. What reads better:
Rules of Thumb
1. If is seems impossible it's likely you're doing it wrong.
2. S/He who googles first is unlikely to be shot down in flames
3. Those error numbers really do mean something, try searching
4. If you're having to debug more than 30 lines of code you've not encapsulated enough
5. If you have to jump through more than 3 functions/subs to debug a problem you've encapsulated too much.
Function SendMailHTML( str_sendMailTo, str_sendMailFrom, str_sendMailSubject, str_sendMailBody, str_sendMailFileAttach )
Set objConfiguration = CreateObject( "CDO.Configuration" )
.Fields("cdoSMTPServer") = "127.0.0.1"
.Fields("cdoSMTPServerPort") = 25
.Fields("cdoSMTPAuthenticate") = 0
.Fields("cdoSendUsingMethod") = 2
Set iMsg = CreateObject( "CDO.Message" )
Set .Configuration = objConfiguration
.To = str_sendMailTo
.From = str_sendMailFrom
.Sender = str_sendMailFrom
.Subject = str_sendMailSubject
.HTMLBody = str_sendMailBody
If ( str_sendMailFileAttach <> "" ) Then Call .AddAttachment( str_sendMailFileAttach )
On Error Resume Next
If ( Err <> 0 ) Then SendMailHTML = "Unable to send a mail due to a server error"
On Error Goto 0
Set iMsg = Nothing
Any more for any more?
Last edited by Richard Conyard; 08-25-2005 at 06:15 AM.