Submitting data to a database and incrementing the primary key. (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


Moody_99 -> Submitting data to a database and incrementing the primary key. (3/15/2001 20:54:00)

PLEASE HELP ME SOMEONE? I AM STUCK HERE. I HAVE TRIED TO WORK THIS OUT FOR MONTHS. UNTIL I DISCOVERED OUTFRONT.NET.

I am submitting data from a form (using Frontpage 2000)to a database in Access 2000.

The ODBC connection is working as far as I know.

The table that is updated has a primary key (of a text data type using the format - AR-118, AR-119). Also in the database is a module that automatically increments the primary key, whenever a new record is entered. The module used is copied from the microsoft's knowledgebase and can be found at http://support.microsoft.com/support/kb/articles/Q209/8/30.ASP?LN=EN-US&SD=gn&FR=0&qry=how%20do%20you%20increment%20a%20numeric%20string&rnk=1&src=DHCS_MSPSS_gn_SRCH&SPR=ACC2000
. In order for this module to work a record should be inputted to start the primary key such as AR-110.

Every time a user submits data from the form, it should be submitted to the table. It should also be given a primary key such AR-111. When a second user submits data to the same table, the primary key should automatically be incremented by 1 such as AR-112.

The problem is that when data is submitted to database I am given the following error message:
Cannot update the database
Error Description: [Microsoft][ODBC Microsoft Access Driver]Error in row

Error Number: -2147217873

Error Source: Microsoft OLE DB Provider for ODBC Drivers.

Note: The pages used and the database are hosted by a hosting company. If I need to make changes of any kind I need to contact them.


IF I change the primary key data type to autonumber, the primary key is updated and all data is successfully submitted to teh data base. There is nothing wrong with the other fields such as telephone number, date and time, they are working fine.

I can't use the autonumber data type, because I have three tables and their Primary key needs to be unique such as AR- 111, DP-111, TR-111.

PLEASE HELP ME SOMEONE I AM IN DESPERATE NEED OF THE ANSWER.





Elecia -> RE: Submitting data to a database and incrementing the primary key. (3/15/2001 20:58:00)

I would suggest that you instead of using the module to find your max that you modify the function as shown below and put it in your asp page. You will need to query the database first and then loop through your records using the function. Then when your user saves the record you just specify in your code that the value of your primary key field is = to the value of FindMax.

<%Function FindMax()
Dim rsVal,mx,FindMax

rs.MoveFirst

rsVal = rs.Fields.getvalue("BookID")
' Set mx equal to the numeric portion of the field.
mx = Right(rsVal, Len(rsVal) - 3)

' Loop to make sure you have the maximum number.
Do While rs.EOF=False
rsVal = rs.Fields.getvalue("BookID")
If Right(rsVal, Len(rsVal) - 3) > mx Then
mx = Right(rsVal, Len(rsVal) - 3)
End If
rs.MoveNext
Loop

' Increment the maximum value by one and
' combine the text with the maximum number.
FindMax = "BO-" & (mx + 1)

rs.Close

Set rs = Nothing

End Function %>

EE





Elecia -> RE: Submitting data to a database and incrementing the primary key. (3/15/2001 20:00:00)

Geezzzzzz You must be desperate you have posts all over the place.




Moody_99 -> RE: Submitting data to a database and incrementing the primary key. (3/15/2001 20:04:00)

Hi Elicia
thanks for your reply.

Thank God I have found someone who unstood me.

But now there is another problem. I don't understand what you mean.

MY asp (page with the form) is located at:
http://www.am-executive.co.uk/Departures.asp

Please look take a look at this page.

The module is located in the MS Access Database connected to the MS Access Form which is created from the MS Access Table connected to this asp page. To start the module. I manually input a record and set the Primamry Key to: AR-110

There is no ID field (primary key) on the form. It is automatically generated (In Access 200) once the user enters all the information and clicks "Submit".Triggered by the module. All the user data should be put into a new record with a primary key of: AR-111.

I am very confused and have many questions that need answers.

So Where do I put the modified fuction in ASP?

How do I query the Database first. There are no queries in the Access database?


How do I loop through the records using the function?

When the user saves the record. How do I specify in my code that the value of my primary key field is = to the value of FindMax?






Elecia -> RE: Submitting data to a database and incrementing the primary key. (3/16/2001 20:38:00)

Ok here is the sample. Note that you may need to create a folder directly under wwwroot called common and put a copy of adovbs.inc in it. If you search your hard drive you should be able to find this file and make a copy of it. Then place this line of code in between the head tags of your page.
<!-- #include virtual="common\adovbs.inc" -->

If you don't get any ADO errors then you don't need to worry about this file.


<%@ Language=VBScript %>
<SCRIPT id=DebugDirectives runat=server language=javascript>
// Set these to true to enable debugging or tracing
@set @debug=false
@set @trace=false
</SCRIPT>
<FORM name=thisForm METHOD=post>
<HTML>
<HEAD>
<META name=VI60_defaultClientScript content=VBScript>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%If request("text1")="" then
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=\fpdb\am.mdb;"
objConn.Open

Dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open "SELECT * FROM Arrivals", objConn,adOpenStatic,adLockOptimistic
RS.MoveFirst%>

<%
Dim rsVal,mx,FindMax

rs.MoveFirst

rsVal = rs.Fields("ID").value
' Set mx equal to the numeric portion of the field.
mx = Right(rsVal, Len(rsVal) - 3)

' Loop to make sure you have the maximum number.
Do While rs.EOF=False
rsVal = rs.Fields("ID").value
If Right(rsVal, Len(rsVal) - 3) > mx Then
mx = Right(rsVal, Len(rsVal) - 3)
End If
rs.MoveNext
Loop

' Increment the maximum value by one and
' combine the text with the maximum number.
FindMax = "AR-" & (mx + 1)
rs.Close

Set rs = Nothing
%>
<form>

<P>Title <INPUT id=text13 name=text1></P>
<P>First Name<INPUT id=text2 name=text2></P>
<P>Last Name<INPUT id=text3 name=text3></P>
<P>Organization<INPUT id=text4 name=text4></P>
<P>Postal Code<INPUT id=text5 name=text5></P>
<P>ID<INPUT id=text6 name=text6 Value=<%=FindMax%>></P>

<INPUT type=submit value="Submit"action="asp page1.asp">
</form>
<%
Else
Dim Conn
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=\fpdb\am.mdb;"
Conn.Open

Dim myRS
Set myRS = Server.CreateObject("ADODB.Recordset")
myRS.Open "SELECT * FROM Arrivals", Conn,adOpenStatic,adLockOptimistic
myRS.MoveFirst
myRS.AddNew
myRS("ID")= Request("Text6")
myRS("AR_Title") = Request("Text1")
myRS("AR_FirstName")= Request("Text2")
myRS("AR_LastName")= Request("Text3")
myRS("AR_Organization") = Request("Text4")
myRS("AR_PostalCode") = Request("Text5")
myRS.update
Response.Write "Thank you! Your request was received."
myRS.Close
set myrs = nothing%>
<a href="asp page1.asp">Click here to enter another new record</a>
<%End if%>

</BODY>
<% ' VI 6.0 Scripting Object Model Enabled %>
<% EndPageProcessing() %>
</FORM>
</HTML>


[This message has been edited by Elecia (edited 03-16-2001).]

[This message has been edited by Elecia (edited 03-16-2001).]





Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.0625