ASP insert statement question

  • Thread starter Thread starter Larry Rekow
  • Start date Start date
L

Larry Rekow

setup: IIS5, Access 2000 DB, Frontpage 2003

Looking for ASP INSERT statement that can:

insert a new record into a table with various values from a form
filled out by a user, and a value that increments the value of one of
the fields +1.

in other words, at the time the user decides to submit the newly
created record to the db, is there a way to have an sql query run
which looks for the higest value of the field "filenumber", adds 1 to
it and saves it as a variable, perhaps into a hidden field, which is
added to the INSERT statement mentioned earlier?

Do not wish to use an autonumber field for various reasons.

Thanks if you can help.

Larry
- - - - - - - - - - - - - - - - - -
"Forget it, Jake. It's Chinatown."
 
-----Original Message-----
setup: IIS5, Access 2000 DB, Frontpage 2003

Looking for ASP INSERT statement that can:

insert a new record into a table with various values
from a form filled out by a user, and a value that
increments the value of one of the fields +1.

Please refer to the article:

Saving Form Data in a Database
http://www.interlacken.com/winnt/tips/tipshow.aspx?tip=44
in other words, at the time the user decides to submit
the newly created record to the db, is there a way to
have an sql query run which looks for the higest value
of the field "filenumber", adds 1 to it and saves it as
a variable, perhaps into a hidden field, which is
added to the INSERT statement mentioned earlier?

Yes, but there's a major flaw in doing so.

Certainly, you can run a SQL statement like:

SELECT Max(mytable.id) AS MaxOfid FROM mytable;

and there by get the largest id value in the mytable
table. The flaw comes about in this scenario:

1. Visitor 1 gets the highest existing id value,
adds 1 and saves it in a hidden form field.
If the highest existing value is 100, the hidden
form field contains 101.

2. Visitor 2 gets the highest existing id value,
adds 1 and saves it in a hidden form field.
Again, the hidden form field contains 101.

3. Visitor 1 gets around to adding his or her record.

4. Visitor 2 gets around to adding his or her record.
A duplicate key results.
Do not wish to use an autonumber field for various
reasons.

Why not? If it's mostly because it's hard to find out
what the new autonumber value is, the article I cited
earlier should resolve that.

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
- - - - - - - - - - - - - - - - - -
"No, ma'am. We're musicians."

Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------
 
Please refer to the article:

Saving Form Data in a Database
http://www.interlacken.com/winnt/tips/tipshow.aspx?tip=44


Yes, but there's a major flaw in doing so.

Certainly, you can run a SQL statement like:

SELECT Max(mytable.id) AS MaxOfid FROM mytable;

and there by get the largest id value in the mytable
table. The flaw comes about in this scenario:

1. Visitor 1 gets the highest existing id value,
adds 1 and saves it in a hidden form field.
If the highest existing value is 100, the hidden
form field contains 101.

2. Visitor 2 gets the highest existing id value,
adds 1 and saves it in a hidden form field.
Again, the hidden form field contains 101.

3. Visitor 1 gets around to adding his or her record.

4. Visitor 2 gets around to adding his or her record.
A duplicate key results.


Why not? If it's mostly because it's hard to find out
what the new autonumber value is, the article I cited
earlier should resolve that.
+++++++++++++++++++++++++++++++++++++++++++++
Whoa! What an excellent article....thanks; it's going to the top of my
favorites in my ASP folder.

My reasons for not using the autonumber field are as follows:
the users, for whatever reason, need to re-enter these file numbers
into another d/b on another system (don't ask) and require that the
file numbers remain constant and in a logical series. deleting
records would create gaps in their file numbers (not such a bad thing)
or, worst case, repairing or compacting their d/b would re-number
their files (so I hear), and that would be a very bad thing.

After a weekend of immersing myself in a begginer's ASP book, I came
up with the following code/approach which I think will avoid the major
flaw you mentioned about two users trying to save the same file
number.

They enter the new file without *any*, and at the instant of their
INSERT submission, an SQL query is run to retrieve the higest number
of the file number field+1 and then use it's resulting value in a
variable (newnum) to add to their INSERT statement. Then the updating
page shows the user what their new number is.

<%
straccount = Request.Form("account")
strmbl = Request.Form("mbl")
strcontainer = Request.Form("container")
strvessel = Request.Form("vessel")
strpo = Request.Form("po")
strpcs = Request.Form("pcs")
strcommodity = Request.Form("commodity")
strremote_computer_name = Request.ServerVariables("REMOTE_HOST")
struser_name = Request.ServerVariables("REMOTE_USER")
strbrowser_type = Request.ServerVariables("HTTP_USER_AGENT")
Set objHamDB=Server.CreateObject("ADODB.Connection")
objHamDB.ConnectionTimeout=60
objHamDB.Open "DSN=import_entry"
Set recordSet=Server.CreateObject("ADODB.Recordset")
recordSet.Open "SELECT (max(ref)+1) as MyMax FROM Results", objHamDB
newnum=recordSet("MyMax")
sqlStatement="INSERT INTO
Results(account,mbl,container,vessel,po,pcs,commodity,ref,remote_computer_name,user_name,browser_type,[timestamp])
VALUES ('"&straccount&"','"&strmbl&"','"& strcontainer
&"','"&strvessel&"','"&strpo&"','"&strpcs&"','"&strcommodity&"',"&newnum&",'"&strremote_computer_name&"','"&struser_name&"','"&strbrowser_type&"',
now())"
Set recordSet=objHamDB.Execute(sqlStatement)
objHamDB.Close
Set objHamDB=Nothing
%>

As you can see, the user's submission form sends nothing to this
insert processing page in the way of either the file number (ref) or
the timestamp, letting this page come up with its own values at the
instant of submission and then later showing the fields submitted to
the user later in the page. I even included a link in case the user
wants to go back to an update page to re-edit this newly created
record in case they made any errors. (and needless to say, the update
page will not process any attempts to change the ref number)

This is my first attempt at trying to use ASP instead of the FP
wizards and bots, and I love the flexibility and freedom of it,but I
also realize I have a lot to learn about writing good code, especially
in the error-handling department, which will be my next project.

I have no illusions that the above code is any good or not, tho it
*seems*, after some testing, to work ok. My thinking is that even if
two users were to hit the submit button at the same time, the query
that looks for the next higest number should be quick enuff to prevent
two users from getting the exact same number. Even so, I think I'll
try to work on an error handling page if one user's submission fails
due to a duplication error which would ask the failing submitter to
retry.

Many thanks for your comments and great website.

Larry
- - - - - - - - - - - - - - - - - -
"Forget it, Jake. It's Chinatown."
 
Clarification: Compacting and repairing the database will only reset the
autonumber field in a table, if you have delete ALL records from the table
prior to compacting and repairing, otherwise the number will still increment
by 1 from the last record entered.

--

==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
WEBMASTER Resources(tm)

FrontPage Resources, Forums, WebCircle,
MS KB Quick Links, etc.
==============================================


Larry Rekow said:
Please refer to the article:

Saving Form Data in a Database
http://www.interlacken.com/winnt/tips/tipshow.aspx?tip=44


Yes, but there's a major flaw in doing so.

Certainly, you can run a SQL statement like:

SELECT Max(mytable.id) AS MaxOfid FROM mytable;

and there by get the largest id value in the mytable
table. The flaw comes about in this scenario:

1. Visitor 1 gets the highest existing id value,
adds 1 and saves it in a hidden form field.
If the highest existing value is 100, the hidden
form field contains 101.

2. Visitor 2 gets the highest existing id value,
adds 1 and saves it in a hidden form field.
Again, the hidden form field contains 101.

3. Visitor 1 gets around to adding his or her record.

4. Visitor 2 gets around to adding his or her record.
A duplicate key results.


Why not? If it's mostly because it's hard to find out
what the new autonumber value is, the article I cited
earlier should resolve that.
+++++++++++++++++++++++++++++++++++++++++++++
Whoa! What an excellent article....thanks; it's going to the top of my
favorites in my ASP folder.

My reasons for not using the autonumber field are as follows:
the users, for whatever reason, need to re-enter these file numbers
into another d/b on another system (don't ask) and require that the
file numbers remain constant and in a logical series. deleting
records would create gaps in their file numbers (not such a bad thing)
or, worst case, repairing or compacting their d/b would re-number
their files (so I hear), and that would be a very bad thing.

After a weekend of immersing myself in a begginer's ASP book, I came
up with the following code/approach which I think will avoid the major
flaw you mentioned about two users trying to save the same file
number.

They enter the new file without *any*, and at the instant of their
INSERT submission, an SQL query is run to retrieve the higest number
of the file number field+1 and then use it's resulting value in a
variable (newnum) to add to their INSERT statement. Then the updating
page shows the user what their new number is.

<%
straccount = Request.Form("account")
strmbl = Request.Form("mbl")
strcontainer = Request.Form("container")
strvessel = Request.Form("vessel")
strpo = Request.Form("po")
strpcs = Request.Form("pcs")
strcommodity = Request.Form("commodity")
strremote_computer_name = Request.ServerVariables("REMOTE_HOST")
struser_name = Request.ServerVariables("REMOTE_USER")
strbrowser_type = Request.ServerVariables("HTTP_USER_AGENT")
Set objHamDB=Server.CreateObject("ADODB.Connection")
objHamDB.ConnectionTimeout=60
objHamDB.Open "DSN=import_entry"
Set recordSet=Server.CreateObject("ADODB.Recordset")
recordSet.Open "SELECT (max(ref)+1) as MyMax FROM Results", objHamDB
newnum=recordSet("MyMax")
sqlStatement="INSERT INTO
Results(account,mbl,container,vessel,po,pcs,commodity,ref,remote_computer_na
me,user_name,browser_type,[timestamp])
VALUES ('"&straccount&"','"&strmbl&"','"& strcontainer
&"','"&strvessel&"','"&strpo&"','"&strpcs&"','"&strcommodity&"',"&newnum&",'
"&strremote_computer_name&"','"&struser_name&"','"&strbrowser_type&"',
now())"
Set recordSet=objHamDB.Execute(sqlStatement)
objHamDB.Close
Set objHamDB=Nothing
%>

As you can see, the user's submission form sends nothing to this
insert processing page in the way of either the file number (ref) or
the timestamp, letting this page come up with its own values at the
instant of submission and then later showing the fields submitted to
the user later in the page. I even included a link in case the user
wants to go back to an update page to re-edit this newly created
record in case they made any errors. (and needless to say, the update
page will not process any attempts to change the ref number)

This is my first attempt at trying to use ASP instead of the FP
wizards and bots, and I love the flexibility and freedom of it,but I
also realize I have a lot to learn about writing good code, especially
in the error-handling department, which will be my next project.

I have no illusions that the above code is any good or not, tho it
*seems*, after some testing, to work ok. My thinking is that even if
two users were to hit the submit button at the same time, the query
that looks for the next higest number should be quick enuff to prevent
two users from getting the exact same number. Even so, I think I'll
try to work on an error handling page if one user's submission fails
due to a duplication error which would ask the failing submitter to
retry.

Many thanks for your comments and great website.

Larry
- - - - - - - - - - - - - - - - - -
"Forget it, Jake. It's Chinatown."
 
Clarification: Compacting and repairing the database will only reset the
autonumber field in a table, if you have delete ALL records from the table
prior to compacting and repairing, otherwise the number will still increment
by 1 from the last record entered.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Thanks for the input. Most of my being leery about the autonumber
field came when, after running a few tests at night when no users were
connected, i would create test files. after deleting these files, i
noticed that i could not re-use the file numbers, or, if I manipulated
the ID numbers by appending or anything, sometimes the number series
would jump to a new and strange value. This along with my (perhaps
incorrect) fears about compacting led me to want to use a different
field that i felt i had more control over. (are you saying that
compacting will *never* change the earlier issued file numbers? these
numbers cannot change since users re-enter them in another d/b, etc.)
In any case, this forced me to delve into ASP 3.0, and I'm certainly
the better for it. Someone else's tag says something about 'the more
I learn, the more I *need* to learn', and that is certainly true in my
case.

Larry
- - - - - - - - - - - - - - - - - -
"Forget it, Jake. It's Chinatown."
 
Larry,

I have use the autonumber field for what you are trying accomplish, however
I do prefer not to use it, only because I want the account number be text
and a set number of digits/characters at all times in most of the projects I
work on, plus I do use the Account number to link to related content in
other tables or databases for a specific user.

But is certain cases the autonumber is a excellent option, and can be used
to link to related content in another table. The other table would stored
the autonumber in number field for the specific user.
--

==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
WEBMASTER Resources(tm)

FrontPage Resources, Forums, WebCircle,
MS KB Quick Links, etc.
==============================================
 
Larry,

I have use the autonumber field for what you are trying accomplish, however
I do prefer not to use it, only because I want the account number be text
and a set number of digits/characters at all times in most of the projects I
work on, plus I do use the Account number to link to related content in
other tables or databases for a specific user.

But is certain cases the autonumber is a excellent option, and can be used
to link to related content in another table. The other table would stored
the autonumber in number field for the specific user.
++++++++++++++++++++++++++++++++++++++++++++++++++++
thanks, i feel as you do since i need the file number to remain static
(tho i don't seem to have any problems yet with it being a number and
not text) and within a very exact number range (accounting purposes)
Actually, I also have an ID autonumber in the table in question, but i
just have a habit of letting access create it's own primary key, well,
just because, not sure if it really helps or not, but i don't think it
can hurt.

Larry
- - - - - - - - - - - - - - - - - -
"Forget it, Jake. It's Chinatown."
 
In almost all cases, I have an ID (renamed to RecID) in all tables as well.

The reason I rename ID, is there is a bug or was a bug at some point, when
trying to generate Excel file (csv) file with ID as a field name.

--

==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
WEBMASTER Resources(tm)

FrontPage Resources, Forums, WebCircle,
MS KB Quick Links, etc.
==============================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top