empty values in INSERT INTO statement
- From: "TB" <tbpostbox-googlegroups@xxxxxxxxx>
- Date: Sat, 23 Apr 2005 00:52:04 +0200
I have this ASP page that inserts new records into an Access database via an
execute statement.
Taking the data from a form, I build my SQL statement like this:
<%
strSQL = "INSERT INTO mytable (Firstname, Lastname, Department, Extension)
_
& " VALUES ('" & CStr(Replace(Request.Form("Firstname"), "'", "''")) _
& "', '" & CStr(Replace(Request.Form("Lastname"), "'", "''")) _
& "', " & CInt(Replace(Request.Form("Department"), "'", "''")) _
& ", " & CInt(Replace(Request.Form("Extension"), "'", "''")) & ")"
%>
That works ok as long as ALL the fields in the form contain data. If one of
the number fields (Department and Extension) are empty, and error is
returned.
I guess it has something to do with the fact that text values are surrounded
by delimiting apostrophies while number fields are not.
So with complete data the above lines generate the following SQL statement:
INSERT INTO mytable (Firstname, Lastname, Department, Extension) VALUES
('John', 'Doe', 3, 55)
If the texts fields are empty then the statement would be:
INSERT INTO mytable (Firstname, Lastname, Department, Extension) VALUES ('',
'', 3, 55)
which is also OK.
But if the number fields are emtpy the statement become:
INSERT INTO mytable (Firstname, Lastname, Department, Extension) VALUES
('John', 'Doe', , )
and that produces an error.
How can I change the above SQL building code so as to avoid this kind of
problem? For example if I could make the word NULL appear instead of
(nothing) then SQL statement would work
Any suggestions would be highly appreciated.
Thanks
TB
.
- Follow-Ups:
- Re: empty values in INSERT INTO statement
- From: Bob Barrows [MVP]
- Re: empty values in INSERT INTO statement
- Prev by Date: Re: Upsizing issue
- Next by Date: Re: FormatNumber with db results
- Previous by thread: Inserting time and date in datetime field
- Next by thread: Re: empty values in INSERT INTO statement
- Index(es):
Relevant Pages
|