View Full Version : Form Processing

The Wizzard
01-27-2003, 06:50 AM
Is it possible to send a form to an email & to a database?

I want a copy emailed to myself, and then also added to a database so I can access it from anywhere.


01-27-2003, 08:46 AM
Well, yes. Provided that you have access to an email component.
Essentially it would go like this.

Set conn = server.createobject("Adodb.connection")
Set rs = server.createobject("Adodb.recordset")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="&server.MapPath("db/yourDatabase.mdb")&";"

sqlInsert = "INSERT INTO TableName (Field1,Field2,Field3) VALUES ('"&Request.Form("Field1")&"','"&Request.Form("Field2")&"','"&Request.Form("Field3")&"')"
rs.Open sqlInsert, conn

Set cdonts = Server.CreateObject("Cdonts.NewMail")
cdonts.subject = "Form mail"
cdonts.from = "youraddress@here.com"
Body = "This and that"
Body = Body & Request.Form("Field1") & " and " & Request.Form("Field2") & " and " & Request.Form("Field3")
cdonts.body = Body
set cdonts = Nothing

01-28-2003, 01:51 AM
Except you'd also want to close the recordset - and also protect the insert statements from single quotes. It doesn't matter in the email, though.

Function SQLFormat(byVal str)
If IsNull(str) Then str = ""
SQLFormat = Replace(str,"'","''")
End Function

Set Conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.RecordSet")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("db/yourDatabase.mdb") & ";"

sqlInsert = "INSERT INTO TableName (Field1,Field2,Field3) VALUES ('" & SQLFormat(Request.Form("Field1")) & "','" & SQLFormat(Request.Form("Field2")) & "','" & SQLFormat(Request.Form("Field3")) & "')"
rs.Open sqlInsert, Conn

Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.Subject = "Form mail"
objMail.From = "youraddress@here.com"
Message = "This and that"
Message = Message & Request.Form("Field1") & " and " & Request.Form("Field2") & " and " & Request.Form("Field3")
objMail.Body = Message
set objMail = Nothing

Set rs = Nothing
Set Conn = Nothing

P.S. I love lower-case variables in JavaScript, and camel-cased functions - but it annoys me to see stuff in VBScript typed all lower case (or in this case, even worse - randomly with no coherent pattern). ;)

Just a pet peeve, nothing personal mhtml (or is it MHTML or MHtmL?). :D

If you're gonna type everything lower-case, go all the way, lol. :) Otherwise I'd follow the standard Pascal Casing of VBScript (even though IMHO, especially learning .NET, VBScript syntax stinks).

Which is why I just got a book that looks like "War and Peace" called "Professional C# Second Edition". C# is a much cleaner language than VB.NET. ;)

01-28-2003, 12:28 PM
he he, it was 3am in the morning. I think that getting the coding right at that time was a good effort.

lol :)

Usually my coding style is all uniform.