473,399 Members | 3,401 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,399 software developers and data experts.

SQL statement error fixed, now wrong data appending

Hey guys....

I put an error-handling in my page and have it posted at the complete
end of the code, see below(when people were putting in 's I was
getting the delimiter errors). Great, I understand that now and it
seems to be fixed but the data I'm pulling from the HTML fields is not
being appended correctly do my Access DB....The field in the DB now
reads " ' ". I understand why it does that (my function) but what I
need it to read is whatever data was entered into my text area named
"comments". I tried to change the line of code in the SQL statement to
read DoubleUpQuotes(comments) but that didn't work....Any help? The
page processes, which is a step in the right direction but fixing one
thing has broken another....

All of the text input boxes need to have this error-handling
capability...

<%
Mode = request.form("mode")
Name = request.form("name") -DROP DOWN BOX
Shift = request.form("shift") -RADIO BUTTON
Wave = request.form("wave") -TEXT INPUT BOX
Carton = request.form("carton") -TEXT INPUT BOX
Location = request.form("location") -TEXT INPUT BOX
License = request.form("license") -TEXT INPUT BOX
Sku = request.form("sku") -TEXT INPUT BOX
Qty = request.form("quantity") -TEXT INPUT BOX
Reason = request.form("reason") -DROP DOWN BOX
Comments = request.form("comments") -TEXT INPUT BOX

if mode = "Send" then

'************************************************* ****************************
'* DATABASE APPENDING
*
'************************************************* ****************************
'create db connection
Set dbconn = Server.CreateObject("ADODB.Connection")

'open db in a DSN-less method
dbconn.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE="&
Server.MapPath("/jax/wh/Online_Forms/Secured_Archives/search_files/shortage.mdb")

'SQL statement to be run
stateSQL = "INSERT INTO shortage (name, shift, wave, carton, location,
license, sku, qty, reason, comments)" &_
"VALUES ('" & name & "','" & shift & "','" & wave & "','" & carton
& "','" & location & "','" & license & "','" & sku & "','" &_
qty & "','" & reason & "','" & DoubleUpQuotes(strFormElement) &
"')"

'display results of statement on screen for testing purposes
Response.Write(stateSQL)

'remind the SQL statement who it works for
dbconn.Execute(stateSQL)

'smack around the db connection until it lets go
dbconn.Close

'kill the connection with extreme prejudice
set dbconn = nothing

'************************************************* ****************************
'* FILE APPENDING
*
'************************************************* ****************************
' Create a text file
shortage = Server.MapPath("/jax/wh/Online_Forms/Secured_Archives/Archive_TextFiles/Shortage_File.txt")
Set fileobject = Server.CreateObject("Scripting.FileSystemObject")
Set textfile = fileobject.OpenTextFile(shortage, 8)

' Append to the newly created text file
textfile.writeline "<BR>"
textfile.writeline "<BR>"
textfile.writeline "<b>Name</b> :" & Request.form("name")
textfile.writeline "<BR>"
textfile.writeline "<b>Shift</b> :" & Request.form("shift")
textfile.writeline "<BR>"
textfile.writeline "<b>Wave</b> :" & Request.form("wave")
textfile.writeline "<BR>"
textfile.writeline "<b>Carton</b> :" & Request.form("carton")
textfile.writeline "<BR>"
textfile.writeline "<b>Location</b> :" & Request.form("location")
textfile.writeline "<BR>"
textfile.writeline "<b>License</b> :" & Request.form("license")
textfile.writeline "<BR>"
textfile.writeline "<b>SKU</b> :" & Request.form("sku")
textfile.writeline "<BR>"
textfile.writeline "<b>Quantity</b> :" & Request.form("quantity")
textfile.writeline "<BR>"
textfile.writeline "<b>Reason</b> :" & Request.form("reason")
textfile.writeline "<BR>"
textfile.writeline "<b>Comments</b> :" & Request.form("comments")
textfile.writeline "<BR>"
textfile.writeline "<b>Date/Time Received</b> :" &
FormatDateTime(Now,4)
textfile.writeline "<BR>"
textfile.writeline "<HR>"

' Close the file and set object to nothing
textfile.close
Set fileobject = nothing

'************************************************* *
'* EMAIL STATEMENT *
'************************************************* *

Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "SMTP1" ' Specify a valid SMTP server
Mail.From = "someone@somewhere" ' Specify sender's address
Mail.FromName = "Test Warehouse Shortage" ' Specify sender's name
Mail.AddAddress "someoneelse@somewhere" 'email address that will
receive form submission
Mail.IsHTML = True
Mail.Subject = "Test Warehouse Shortage"

Dim mail_body

mail_body = "Name: " & request.form("name") & "<br>" &_
"Shift: " & request.form("shift") & "<br>" &_
"Wave: " & request.form("wave") & "<br>" &_
"Carton: " & request.form("carton") & "<br>" &_
"Location: " & request.form("location") & "<br>"
&_
"License: " & request.form("license") & "<br>" &_
"SKU: " & request.form("sku") & "<br>" &_
"Quantity: " & request.form("quantity") & "<br>"
&_
"Reason: " & request.form("reason") & "<br>" &_
"Comments: " & request.form("comments") & "<br>"

mail.body = mail_body & "Submitted IP Address: " &
request.servervariables("REMOTE_ADDR")

On Error Resume Next
Mail.Send
If Err <> 0 Then
Response.Write "Error encountered: " & Err.Description
End If

End if
%>
<html>
HTML FORM DATA IS IN HERE.
</html>
<%
Function DoubleUpQuotes(strText)
DoubleUpQuotes = "'" & Replace(strText, "'", "''") & "'"
End Function
%>
Jul 19 '05 #1
1 2094
I suggest that you get in the habit of using variables instead of pulling
from the Request.Form collection each time you need a value. In addition to
that, make yourself a "textIn" function for any text-based data that you're
inserting into a database. The former suggestion can generate additional
lines of code, but it will make things simpler, and possilby [unnoticeably]
faster. Example:

<%
Dim sFirstname, sLastname
sFirstname = Request.Form("firstName")
sLastname = Request.Form("lastName")

'''Say that the firstName and lastName columns in your Access database
'''are text type and have a maximum length of 50.

sSQL = "INSERT INTO [something] ([firstName],[lastName]) VALUES (" &
TextIn(sFirstname,50) & "," & TextIn(sLastname,50) & ")"

''' This function will put ' delimeters on
''' text data for SQL input, double up the '
''' as needed, and be trim away any data that
''' exceeds the limit you specify.
Function TextIn(TheText,MaxLength)
Dim sResult
sResult = TheText
If MaxLength > 0 Then
If Len(sResult) > MaxLength Then sResult = Left(sResult, MaxLength)
End If
sResult = Replace(sResult, "'", "''")
sResult = "'" & sResult & "'"
TextIn = sResult
End Function
%>
I personally feel that it is good practice to never modify the user's actual
input. Like, if you were to do something like:
<% sLastname = Replace(sLastname, "'", "''") %>
so that you could deal with any ' characters, if you later go back to add
code to e-mail this same data to someone, that person would receive
something like this:

First name: John
Last name: O''Brien

That's just silly.

Try to simplify your code and make use of variables, subs, and functions.
Example again:
<%
'' vars with global scope
Dim sMode, sName, sShift, sWave ', etc.
Call AssignValues()
Call InsertData(sName, sShift, sWave)
Call WriteToTextFile(sName, sShift, sWave)
'Call SendEmail(arguments here)
Sub AssignValues()
Mode = Request.Form("mode")
Name = Request.Form("name")
Shift = Request.Form("shift")
Wave = Request.Form("wave")
End Sub

Sub InsertData(name, shift, wave)
Dim dbconn, sSQL

sSQL = "INSERT INTO shortage (name, shift, wave) VALUES (" &
TextIn(name,50) & "," & TextIn(shift,10) & "," TextIn(wave,255) & ")"

Set dbconn = Server.CreateObject("ADODB.Connection")
dbconn.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE="&
Server.MapPath("/jax/wh/Online_Forms/Secured_Archives/search_files/shortage.mdb")
dbconn.Execute sSQL,,129
dbconn.Close : Set dbconn = Nothing
End Sub
Sub WriteToTextFile(name,shift,wave)
Dim sContents
Dim shortage, fileobject, textfile

sContents = ""
sContents = sContents & "<BR>"
sContents = sContents & "<BR>"
sContents = sContents & "<b>Name</b> :" & name
sContents = sContents & "<BR>"
sContents = sContents & "<b>Shift</b> :" & shift
sContents = sContents & "<BR>"
sContents = sContents & "<b>Wave</b> :" & wave
'''etc.
shortage =
Server.MapPath("/jax/wh/Online_Forms/Secured_Archives/Archive_TextFiles/Shortage_File.txt")
Set fileobject = Server.CreateObject("Scripting.FileSystemObject")
Set textfile = fileobject.OpenTextFile(shortage, 8)
textfile.Write sContents
textfile.Close : Set textfile = Nothing
Set fileobject = Nothing
End Sub

'''Sub SendEmail(arguments here)
'''End Sub

Ray at work

"dmiller23462" <dm**********@yahoo.com> wrote in message
news:59**************************@posting.google.c om...
Hey guys....

I put an error-handling in my page and have it posted at the complete
end of the code, see below(when people were putting in 's I was
getting the delimiter errors). Great, I understand that now and it
seems to be fixed but the data I'm pulling from the HTML fields is not
being appended correctly do my Access DB....The field in the DB now
reads " ' ". I understand why it does that (my function) but what I
need it to read is whatever data was entered into my text area named
"comments". I tried to change the line of code in the SQL statement to
read DoubleUpQuotes(comments) but that didn't work....Any help? The
page processes, which is a step in the right direction but fixing one
thing has broken another....

All of the text input boxes need to have this error-handling
capability...

<%
Mode = request.form("mode")
Name = request.form("name") -DROP DOWN BOX
Shift = request.form("shift") -RADIO BUTTON
Wave = request.form("wave") -TEXT INPUT BOX
Carton = request.form("carton") -TEXT INPUT BOX
Location = request.form("location") -TEXT INPUT BOX
License = request.form("license") -TEXT INPUT BOX
Sku = request.form("sku") -TEXT INPUT BOX
Qty = request.form("quantity") -TEXT INPUT BOX
Reason = request.form("reason") -DROP DOWN BOX
Comments = request.form("comments") -TEXT INPUT BOX

if mode = "Send" then

'************************************************* ****************************
'* DATABASE APPENDING
*
'************************************************* ****************************
'create db connection
Set dbconn = Server.CreateObject("ADODB.Connection")

'open db in a DSN-less method
dbconn.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE="&
Server.MapPath("/jax/wh/Online_Forms/Secured_Archives/search_files/shortage.mdb")

'SQL statement to be run
stateSQL = "INSERT INTO shortage (name, shift, wave, carton, location,
license, sku, qty, reason, comments)" &_
"VALUES ('" & name & "','" & shift & "','" & wave & "','" & carton
& "','" & location & "','" & license & "','" & sku & "','" &_
qty & "','" & reason & "','" & DoubleUpQuotes(strFormElement) &
"')"

'display results of statement on screen for testing purposes
Response.Write(stateSQL)

'remind the SQL statement who it works for
dbconn.Execute(stateSQL)

'smack around the db connection until it lets go
dbconn.Close

'kill the connection with extreme prejudice
set dbconn = nothing

'************************************************* ****************************
'* FILE APPENDING
*
'************************************************* ****************************
' Create a text file
shortage =
Server.MapPath("/jax/wh/Online_Forms/Secured_Archives/Archive_TextFiles/Shortage_File.txt")
Set fileobject = Server.CreateObject("Scripting.FileSystemObject")
Set textfile = fileobject.OpenTextFile(shortage, 8)

' Append to the newly created text file
textfile.writeline "<BR>"
textfile.writeline "<BR>"
textfile.writeline "<b>Name</b> :" & Request.form("name")
textfile.writeline "<BR>"
textfile.writeline "<b>Shift</b> :" & Request.form("shift")
textfile.writeline "<BR>"
textfile.writeline "<b>Wave</b> :" & Request.form("wave")
textfile.writeline "<BR>"
textfile.writeline "<b>Carton</b> :" & Request.form("carton")
textfile.writeline "<BR>"
textfile.writeline "<b>Location</b> :" & Request.form("location")
textfile.writeline "<BR>"
textfile.writeline "<b>License</b> :" & Request.form("license")
textfile.writeline "<BR>"
textfile.writeline "<b>SKU</b> :" & Request.form("sku")
textfile.writeline "<BR>"
textfile.writeline "<b>Quantity</b> :" & Request.form("quantity")
textfile.writeline "<BR>"
textfile.writeline "<b>Reason</b> :" & Request.form("reason")
textfile.writeline "<BR>"
textfile.writeline "<b>Comments</b> :" & Request.form("comments")
textfile.writeline "<BR>"
textfile.writeline "<b>Date/Time Received</b> :" &
FormatDateTime(Now,4)
textfile.writeline "<BR>"
textfile.writeline "<HR>"

' Close the file and set object to nothing
textfile.close
Set fileobject = nothing

'************************************************* *
'* EMAIL STATEMENT *
'************************************************* *

Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "SMTP1" ' Specify a valid SMTP server
Mail.From = "someone@somewhere" ' Specify sender's address
Mail.FromName = "Test Warehouse Shortage" ' Specify sender's name
Mail.AddAddress "someoneelse@somewhere" 'email address that will
receive form submission
Mail.IsHTML = True
Mail.Subject = "Test Warehouse Shortage"

Dim mail_body

mail_body = "Name: " & request.form("name") & "<br>" &_
"Shift: " & request.form("shift") & "<br>" &_
"Wave: " & request.form("wave") & "<br>" &_
"Carton: " & request.form("carton") & "<br>" &_
"Location: " & request.form("location") & "<br>"
&_
"License: " & request.form("license") & "<br>" &_
"SKU: " & request.form("sku") & "<br>" &_
"Quantity: " & request.form("quantity") & "<br>"
&_
"Reason: " & request.form("reason") & "<br>" &_
"Comments: " & request.form("comments") & "<br>"

mail.body = mail_body & "Submitted IP Address: " &
request.servervariables("REMOTE_ADDR")

On Error Resume Next
Mail.Send
If Err <> 0 Then
Response.Write "Error encountered: " & Err.Description
End If

End if
%>
<html>
HTML FORM DATA IS IN HERE.
</html>
<%
Function DoubleUpQuotes(strText)
DoubleUpQuotes = "'" & Replace(strText, "'", "''") & "'"
End Function
%>

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
4
by: Simom Thorpe | last post by:
Hi, I'm trying to insert a line into a MS access DB using ASP on IIS 5. This is the line: con.execute "INSERT INTO newProds(title,desc,catcode) VALUES ('Champagne Muff Scarf','','AC304B')"...
8
by: Drew | last post by:
I have an app that I am building that takes user input on 1 page then inserts/updates it on the next page. There are a total of 20 pages, with data from page1 being modified by page2 and so on. I...
3
by: Klemens | last post by:
Hi, I get an implicit -101 on an alter table statement on changing the length of one column ALTER TABLE XTRADE.LIEFERANT ALTER COLUMN ZENTRALE_NUMMER SET DATA TYPE VARCHAR(13) DB21034E The...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
5
by: Arthur Mnev | last post by:
This is probably beaten to death subject... Does anyone have a good idea of what penalties are for using Fixed statement in c#. On one side it allows for much greater flexibility with casts and...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
8
by: Brian Basquille | last post by:
Hello all, Bit of a change of pace now. As opposed to the typical questions regarding my Air Hockey game, am also working on a Photo Album which uses an Access Database to store information...
118
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.