Link to home
Start Free TrialLog in
Avatar of mharcais
mharcais

asked on

Update same record from different forms

Can someone please check my code so that it updates the SAME RECORD as it goes through a series of forms, each form adding more information to different fields in the SAME record.

Much appreciated.


Form 1:

<form name="form" method="post" action="add_to_database_roi1.asp">
<select name="1">
                        <option selected>Select Type of occurrence...
                        <option value="Incident">Incident
                        <option value="First Aid">First Aid
                        <option value="Medical Aid">Medical Aid
                        <option value="Environmental Spill">Environmental Spill
                        <option value="Traffic Accident">Traffic Accident
                        <option value="Fire">Fire
                        <option value="Dangerous Occurence     ">Dangerous Occurence
                        </select>

<input type="image" src="submit.gif" name="submit" value="SUBMIT">
</form>





add_to_database_roi1.asp:

<%
'Dimension variables
Dim adoCon                   'Holds the Database Connection Object
Dim rsAddComments            'Holds the recordset for the new record to be added to the database
Dim strSQL                  'Holds the SQL query for the database

'Create an ADO connection odject
Set adoCon = Server.CreateObject("ADODB.Connection")


'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("health.mdb")

'Create an ADO recordset object
Set rsAddComments = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database

strSQL = "SELECT * FROM types;"

'Set the cursor type we are using so we can navigate through the recordset
rsAddComments.CursorType = 2
'Set the lock type so that the record is locked by ADO when it is updated
rsAddComments.LockType = 3
'Open the Timepoint table using the SQL query held in the strSQL varaiable
rsAddComments.Open strSQL, adoCon
'Tell the recordset we are adding a new record to it
rsAddComments.AddNew

'Add a new record to the recordset
rsAddComments.Fields("1") = Request.Form("1")

'Write the updated recordset to the database
rsAddComments.Update

'Reset server objects
rsAddComments.Close
Set rsAddComments = Nothing
Set adoCon = Nothing

Response.Redirect "h&S_form_roi2.asp"

%>

Form 2:

<form name="form" method="post" action="add_to_database_roi2.asp">
<select name="2">
                        <option selected>Select Choice...
                        <option value="Incident">A
                        <option value="First Aid">B
                        <option value="Medical Aid">C
                        <option value="Environmental Spill">D
                        <option value="Traffic Accident">E
                        <option value="Fire">F
                        <option value="Dangerous Occurence     ">G
</select>

<input type="image" src="submit.gif" name="submit" value="SUBMIT">
</form>




add_to_database_roi2.asp:


<%
'Dimension variables
Dim adoCon                   'Holds the Database Connection Object
Dim rsAddComments            'Holds the recordset for the new record to be added to the database
Dim strSQL                  'Holds the SQL query for the database

'Create an ADO connection odject
Set adoCon = Server.CreateObject("ADODB.Connection")


'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("health.mdb")

'Create an ADO recordset object
Set rsAddComments = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database

strSQL = "SELECT * FROM types;"

'Set the cursor type we are using so we can navigate through the recordset
rsAddComments.CursorType = 2
'Set the lock type so that the record is locked by ADO when it is updated
rsAddComments.LockType = 3
'Open the Timepoint table using the SQL query held in the strSQL varaiable
rsAddComments.Open strSQL, adoCon
'Tell the recordset we are adding a new record to it
rsAddComments.AddNew

'Add a new record to the recordset
rsAddComments.Fields("2") = Request.Form("2")

'Write the updated recordset to the database
rsAddComments.Update

'Reset server objects
rsAddComments.Close
Set rsAddComments = Nothing
Set adoCon = Nothing

Response.Redirect "h&S_form_roi3.asp"

%>

Etc etc for approx 15 forms

Each form has a lot more than just one select box, I've just doen it like that for ease.

I essentially need your help to modify this existing code in order to ensure that each time it adds the fields to the access database it adds it to the record it created in the first form.

I need to get this sorted out  ASAP, so your help would be greatly appreciated.
Avatar of sybe
sybe

1. - why not put everything in a single form, it makes things a lot less complicated.
2. - if you really want to have a series of forms, then you have several options:

** at the first submission, create a new record and gets the ID of the record. Pass this ID to each next form (into a hidden formfield), so you will know which record to update.

** don't create the record after all forms are filled out. In the meantime store all values temporarily in either Session-variables or hidden formfields.

Avatar of mharcais

ASKER

It has to be broken down - there are over 150 different questions, So it is broken down to about 12 per page over 15 forms.

I don't want to risk it crashing near the end and the user having to start all over, so that is why it must update the same record each time.

I am an asp beginner, who has modified this script I got from the web, so please only reply with real, workable code to add to what I already have.
>> I don't want to risk it crashing near the end and the user having to start all over, so that is why it must update the same record each time.

the risk at "crashing" is larger when you have more forms, that is why i suggest to put it all in a single form
also the code is harder whe you have a sequence of forms.

>> I am an asp beginner, who has modified this script I got from the web, so please only reply with real, workable code to add to what I already have.

Does this mean that you just want working code without understanding how it works?? because that would mean that someone just writes the  code for you. That's ok for a question, I have no problems with that, but you would probably need to post a lot more information.

My code would basically look a lot different from what you got, because i have many standard functions that I use, and have my own way of doing things. For example I would use another connectionstring (using OLEDB), and I would use "INSERT" and "UPDATE" statements, not use the recordset.AddNew.

So in that case I would leave the question to others - don't want to want you left with some upgrades of your existing code which I think can be improved in many ways (even though it works).

ASKER CERTIFIED SOLUTION
Avatar of SquareHead
SquareHead

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial