newbie select last item entered to a DB question

newbie select last item entered to a DB question

am 20.01.2006 10:46:21 von Paul Malbon

Hi all,

Newbie post here so apologies in advance....

I have a 'Call log' form that enters details of a job into an access
database (works a treat) and then redirects to a page that will enable you
to print those details onto a call sheet. In the database, the first field
is an ID field which is an auto number used as a Call number for the call
sheet. My question is, Is there a way of retrieving the call number for the
item just added to the DB before redirecting the page so that I can add the
call number to the call sheet?

Thanks in advance
Paul

Re: newbie select last item entered to a DB question

am 20.01.2006 12:50:17 von reb01501

Paul Malbon wrote:
> Hi all,
>
> Newbie post here so apologies in advance....
>
> I have a 'Call log' form that enters details of a job into an access
> database (works a treat) and then redirects to a page that will
> enable you to print those details onto a call sheet. In the database,
> the first field is an ID field which is an auto number used as a Call
> number for the call sheet. My question is, Is there a way of
> retrieving the call number for the item just added to the DB before
> redirecting the page so that I can add the call number to the call
> sheet?
This article has several methods.
http://www.aspfaq.com/show.asp?id=2174

I would opt for the "SELECT @@IDENTITY" option.

If you are using a recordset to add the data, then you should consider using
DML (data modification language - INSERT, UPDATE and DELETE statments)
instead. Recordsets, while offering benefits (such as handling concurrency
issues) when used for data modifications in desktop applications, have
little to no value in a server-based environment such as ASP. In fact,
especially with an Access backend, they can kill your application's
scalability.

If you are using dynamic sql, you should become aware of the security issues
involved in its use:
http://mvp.unixwiz.net/techtips/sql-injection.html
http://www.sqlsecurity.com/DesktopDefault.aspx?tabid=23

Here are some more posts to read:

http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&sel m=e6lLVvOcDHA.1204%40TK2MSFTNGP12.phx.gbl

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1& selm=eHYxOyvaDHA.4020%40tk2msftngp13.phx.gbl

Using Command object to parameterize CommandText:
http://groups-beta.google.com/group/microsoft.public.inetser ver.asp.db/msg/72e36562fee7804e

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: newbie select last item entered to a DB question

am 20.01.2006 14:24:14 von Paul Malbon

This is a multi-part message in MIME format.

------=_NextPart_000_0009_01C61DC4.CEC28D50
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Thanks for your help Bob.

I'm having a bit of trouble implementing your suggestion however, and =
get a return of 0 which is the incorrect value.

MY Insert statement is as follows, the values are taken from a recordset
Insert into Tab_call_log (company_id, Company, FName, SurName, =
contact_number, date_logged, logged_by, cstatus, call_details, =
call_severity) Values('PM051205001', 'AJT Smart', 'Adam', 'Taylor', =
'0129933 446789', '20/01/2006 13:04:03', 'Employee', '0', 'fgdg', =
'Severity');

Now I assumed that the select @@identity statement should go on the same =
insert line (i may be well off the mark here, I am new...) when the sql =
statement runs the error displays '(0x80040E14) Characters found after =
end of SQL statement.'=20

I then created a new recordset and sql statement, and added the select =
@@ statement as a second SQL string, so the whole thing now looks like =
this...
%>
conn =3D "PROVIDER=3DMicrosoft.Jet.OLEDB.4.0;DATA =
SOURCE=3Dc:\inetpub\wwwroot\database\lancer.mdb"
Set rs =3D Server.CreateObject("ADODB.Recordset")
%>

<%
SQL =3D "Insert into Tab_call_log (company_id, Company, FName, SurName, =
contact_number, date_logged, logged_by, cstatus, call_details, =
call_severity)" & " Values('" & cust_id & "', '" & Company & "', '" & =
FName & "', '" & surName & "', '" & telephone & "', '" & date_logged & =
"', '" & logged_by & "', '" & cstatus & "', '" & call_dets & "', '" & =
call_severity & "');"

SQL4 =3D "SELECT @@identity as call_num;"

rs.open SQL, conn, 2, 1
rs3.open SQL1, conn, 2, 1
%>

This returns the value of call_num to be 0.

I apologies for being stupid in advance, and would really appreciate =
your insight on this one.

Thanks again

Paul

"Bob Barrows [MVP]" wrote in message =
news:%23oIdsebHGHA.532@TK2MSFTNGP15.phx.gbl...
> Paul Malbon wrote:
>> Hi all,
>>
>> Newbie post here so apologies in advance....
>>
>> I have a 'Call log' form that enters details of a job into an access
>> database (works a treat) and then redirects to a page that will
>> enable you to print those details onto a call sheet. In the database,
>> the first field is an ID field which is an auto number used as a Call
>> number for the call sheet. My question is, Is there a way of
>> retrieving the call number for the item just added to the DB before
>> redirecting the page so that I can add the call number to the call
>> sheet?
> This article has several methods.
> http://www.aspfaq.com/show.asp?id=3D2174
>=20
> I would opt for the "SELECT @@IDENTITY" option.
>=20
> If you are using a recordset to add the data, then you should consider =
using=20
> DML (data modification language - INSERT, UPDATE and DELETE statments) =

> instead. Recordsets, while offering benefits (such as handling =
concurrency=20
> issues) when used for data modifications in desktop applications, have =

> little to no value in a server-based environment such as ASP. In fact, =

> especially with an Access backend, they can kill your application's=20
> scalability.
>=20
> If you are using dynamic sql, you should become aware of the security =
issues=20
> involved in its use:
> http://mvp.unixwiz.net/techtips/sql-injection.html
> http://www.sqlsecurity.com/DesktopDefault.aspx?tabid=3D23
>=20
> Here are some more posts to read:
>=20
> =
http://www.google.com/groups?hl=3Den&lr=3D&ie=3DUTF-8&oe=3DU TF-8&selm=3De=
6lLVvOcDHA.1204%40TK2MSFTNGP12.phx.gbl
>=20
> =
http://groups.google.com/groups?hl=3Den&lr=3D&ie=3DUTF-8&c2c off=3D1&selm=3D=
eHYxOyvaDHA.4020%40tk2msftngp13.phx.gbl
>=20
> Using Command object to parameterize CommandText:
> =
http://groups-beta.google.com/group/microsoft.public.inetser ver.asp.db/ms=
g/72e36562fee7804e
>=20
> Bob Barrows
> --=20
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"=20
>=20
>
------=_NextPart_000_0009_01C61DC4.CEC28D50
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable



charset=3Diso-8859-1">




Thanks for your help Bob.

 

I'm having a bit of trouble =
implementing your=20
suggestion however, and get a return of 0 which is the incorrect=20
value.

 

MY Insert statement is as follows, the =
values are=20
taken from a recordset

Insert into Tab_call_log (company_id, =
Company,=20
FName, SurName, contact_number, date_logged, logged_by, cstatus, =
call_details,=20
call_severity) Values('PM051205001', 'AJT Smart', 'Adam', 'Taylor', =
'0129933=20
446789', '20/01/2006 13:04:03', 'Employee', '0', 'fgdg',=20
'Severity');

 

Now I assumed that the select =
@@identity statement=20
should go on the same insert line (i may be well off the mark here, I am =
new...)=20
when the sql statement runs the error displays '(0x80040E14) Characters =
found=20
after end of SQL statement.'

 

I then created a new recordset and sql =
statement,=20
and added the select @@ statement as a second SQL string, so the whole =
thing now=20
looks like this...

%>

conn =
"PROVIDER=3DMicrosoft.Jet.OLEDB.4.0;DATA=20
SOURCE=3Dc:\inetpub\wwwroot\database\lancer.mdb"
Set rs =
Server.CreateObject("ADODB.Recordset")

%>

 

<%

SQL =3D "Insert into =
Tab_call_log=20
(company_id, Company, FName, SurName, contact_number, date_logged, =
logged_by,=20
cstatus, call_details, call_severity)" & " Values('" & cust_id =
& "',=20
'" & Company & "', '" & FName & "', '" & surName =
& "',=20
'" & telephone & "', '" & date_logged & "', '" & =
logged_by=20
& "', '" & cstatus & "', '" & call_dets & "', '" =
&=20
call_severity & "');"

 


SQL4 =3D "SELECT =
@@identity as=20
call_num;"

 

rs.open SQL, conn, 2, =
1

rs3.open SQL1, conn, 2, =

1

%>

 

This returns the value of call_num to =
be=20
0.

 

I apologies for being stupid in =
advance, and would=20
really appreciate your insight on this one.

 

Thanks again

 

Paul

 

"Bob Barrows [MVP]" < href=3D"mailto:reb01501@NOyahoo.SPAMcom"> size=3D2>reb01501@NOyahoo.SPAMcom size=3D2>> wrote in=20
message
href=3D"news:%23oIdsebHGHA.532@TK2MSFTNGP15.phx.gbl"> face=3DArial =
size=3D2>news:%23oIdsebHGHA.532@TK2MSFTNGP15.phx.gbl
face=3DArial size=3D2>...
> =
Paul Malbon=20
wrote:
>> Hi all,
>>
>> Newbie post here so =
apologies=20
in advance....
>>
>> I have a 'Call log' form that =
enters=20
details of a job into an access
>> database (works a treat) and =
then=20
redirects to a page that will
>> enable you to print those =
details onto=20
a call sheet. In the database,
>> the first field is an ID =
field which=20
is an auto number used as a Call
>> number for the call sheet. =
My=20
question is, Is there a way of
>> retrieving the call number =
for the=20
item just added to the DB before
>> redirecting the page so =
that I can=20
add the call number to the call
>> sheet?
> This article =
has=20
several methods.
>
href=3D"http://www.aspfaq.com/show.asp?id=3D2174"> size=3D2>http://www.aspfaq.com/show.asp?id=3D2174
face=3DArial=20
size=3D2>>
> I would opt for the "SELECT @@IDENTITY" =
option.
>=20

> If you are using a recordset to add the data, then you should =
consider=20
using
> DML (data modification language - INSERT, UPDATE and =
DELETE=20
statments)
> instead. Recordsets, while offering benefits (such =
as=20
handling concurrency
> issues) when used for data modifications =
in=20
desktop applications, have
> little to no value in a server-based =

environment such as ASP. In fact,
> especially with an Access =
backend,=20
they can kill your application's
> scalability.
>
> =
If you=20
are using dynamic sql, you should become aware of the security issues =

>=20
involved in its use:
>
href=3D"http://mvp.unixwiz.net/techtips/sql-injection.html"> face=3DArial=20
size=3D2>http://mvp.unixwiz.net/techtips/sql-injection.html
> face=3DArial size=3D2>> href=3D"http://www.sqlsecurity.com/DesktopDefault.aspx?tabid =3D23"> face=3DArial=20
size=3D2>http://www.sqlsecurity.com/DesktopDefault.aspx?tabi d=3D23
=

face=3DArial size=3D2>>
> Here are some more posts to =
read:
>=20

> href=3D"http://www.google.com/groups?hl=3Den&lr=3D&i e=3DUTF-8&=
;oe=3DUTF-8&selm=3De6lLVvOcDHA.1204%40TK2MSFTNGP12.phx.g bl"> face=3DArial=20
size=3D2>http://www.google.com/groups?hl=3Den&lr=3D& ie=3DUTF-8&am=
p;oe=3DUTF-8&selm=3De6lLVvOcDHA.1204%40TK2MSFTNGP12.phx. gbl >
face=3DArial size=3D2>>
> href=3D"http://groups.google.com/groups?hl=3Den&lr=3D&am p;ie=3DUTF-8&=
amp;c2coff=3D1&selm=3DeHYxOyvaDHA.4020%40tk2msftngp13.ph x.gbl">
face=3DArial=20
size=3D2>http://groups.google.com/groups?hl=3Den&lr=3D&a mp;ie=3DUTF-8=
&c2coff=3D1&selm=3DeHYxOyvaDHA.4020%40tk2msftngp13.p hx.gbl
=

face=3DArial size=3D2>>
> Using Command object to parameterize =

CommandText:
> href=3D"http://groups-beta.google.com/group/microsoft.public .inetserver.a=
sp.db/msg/72e36562fee7804e"> face=3DArial=20
size=3D2>http://groups-beta.google.com/group/microsoft.publi c.inetserver.=
asp.db/msg/72e36562fee7804e
face=3DArial size=3D2>>
> Bob Barrows
> --
> =
Microsoft MVP -=20
ASP/ASP.NET
> Please reply to the newsgroup. This email account is =
my spam=20
trap so I
> don't check it very often. If you must reply off-line, =
then=20
remove the
> "NO SPAM"
>
>

------=_NextPart_000_0009_01C61DC4.CEC28D50--

Re: newbie select last item entered to a DB question

am 20.01.2006 14:58:45 von reb01501

Paul Malbon wrote:
> Thanks for your help Bob.
>
> I'm having a bit of trouble implementing your suggestion however, and
> get a return of 0 which is the incorrect value.
>
> MY Insert statement is as follows, the values are taken from a
> recordset
> Insert into Tab_call_log (company_id, Company, FName, SurName,

Is company_id the autonumber field? if so, it should NOT be included in this
statement.

> contact_number, date_logged, logged_by, cstatus, call_details,
> call_severity) Values('PM051205001', 'AJT Smart', 'Adam', 'Taylor',
> '0129933 446789', '20/01/2006 13:04:03', 'Employee', '0', 'fgdg',
> 'Severity');
>
> Now I assumed that the select @@identity statement should go on the
> same insert line (i may be well off the mark here, I am new...)

No. Jet does not support batch execution. You can only execute one query at
a time.
I'm pretty sure this was covered in the KB article cited in the aspfaq
article ...
http://support.microsoft.com/default.aspx/kb/232144

Oh, i just looked at Aaron's article and I see where you got this idea. I'm
going to have to contact him about correcting this code snippet

> when
> the sql statement runs the error displays '(0x80040E14) Characters
> found after end of SQL statement.'

>
> I then created a new recordset
New recordset? you should not have used one in the first place. Never create
a recordset to run a query that does not return records.

> and sql statement, and added the
> select @@ statement as a second SQL string, so the whole thing now
> looks like this...
> %>
> conn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
> SOURCE=c:\inetpub\wwwroot\database\lancer.mdb"
> Set rs = Server.CreateObject("ADODB.Recordset")
> %>
>
> <%
> SQL = "Insert into Tab_call_log (company_id, Company, FName, SurName,

OK, I'm going to assume that company_id is not the autonumber field.

> contact_number, date_logged, logged_by, cstatus, call_details,
> call_severity)" & " Values('" & cust_id & "', '" & Company & "', '" &
> FName & "', '" & surName & "', '" & telephone & "', '" & date_logged
> & "', '" & logged_by & "', '" & cstatus & "', '" & call_dets & "', '"
> & call_severity & "');"
>
> SQL4 = "SELECT @@identity as call_num;"

No need for the column alias. See below:
>
> rs.open SQL, conn, 2, 1

:-)
Bad. An insert statement returns no records. There is no need for a
recordset.

Simply do:
conn.execute SQL,,129 '129 = adCmdText + adExecuteNoRecords

followed by
set rs=conn.execute("select @@identity",,1) '1=adCmdText
newnumber= rs(0)
rs.close: set rs=nothing

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: newbie select last item entered to a DB question

am 20.01.2006 16:46:27 von Paul Malbon

mmmmmmm.... I now get "(0x800A01A8) Object required: 'conn'"

Conn is declared and set to
conn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=c:\inetpub\wwwroot\database\lancer.mdb"

which is the connection I use for all of my data manipulation. What I have I
done now? Any ideas?

Sorry and thanks again

Paul

>
> Simply do:
> conn.execute SQL,,129 '129 = adCmdText + adExecuteNoRecords
>
> followed by
> set rs=conn.execute("select @@identity",,1) '1=adCmdText
> newnumber= rs(0)
> rs.close: set rs=nothing
>
> HTH,
> Bob Barrows
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>

Re: newbie select last item entered to a DB question

am 20.01.2006 17:53:05 von reb01501

Paul Malbon wrote:
> mmmmmmm.... I now get "(0x800A01A8) Object required: 'conn'"
>
> Conn is declared and set to
> conn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
> SOURCE=c:\inetpub\wwwroot\database\lancer.mdb"

conn should be set like this:

set conn=createobject("adodb.connection")
conn.open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"DATA SOURCE=c:\inetpub\wwwroot\database\lancer.mdb"

Always create an explicit connection object. Never open recordsets using a
string. This practice can lead to memory leaks that may cause web server
failure.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: newbie select last item entered to a DB question

am 20.01.2006 18:25:18 von Paul Malbon

BINGO!!!

Got it, and it works!! Thanks very much Bob, sorry for being such a drain on
you!!

Paul
"Bob Barrows [MVP]" wrote in message
news:eK7Q9HeHGHA.2036@TK2MSFTNGP14.phx.gbl...
> Paul Malbon wrote:
>> mmmmmmm.... I now get "(0x800A01A8) Object required: 'conn'"
>>
>> Conn is declared and set to
>> conn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
>> SOURCE=c:\inetpub\wwwroot\database\lancer.mdb"
>
> conn should be set like this:
>
> set conn=createobject("adodb.connection")
> conn.open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
> "DATA SOURCE=c:\inetpub\wwwroot\database\lancer.mdb"
>
> Always create an explicit connection object. Never open recordsets using a
> string. This practice can lead to memory leaks that may cause web server
> failure.
>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>