|
 |
access_asp thread: logic problem
Message #1 by "Tony DiNucci" <tonydinucci@h...> on Thu, 4 Oct 2001 21:44:43
|
|
I have included a portion of a page I am having problems with.
What I want to do is insert a new record if there isnt already one
existing in the table for the selected "registration number"
and "username"............otherwise I just want to update the existing
record.
My problem is that I can get it to do one or the other but not both
I know this must be very simple but ive been staring at it for hours and
changing every thing i can think of and still can't get it to work.
I've checked all the variables are being passed so that isn't the problem.
Thanks for any help
___________________________________________________________________________
set indivRS = Server.CreateObject( "ADODB.Recordset" )
indivRS.ActiveConnection = times
sqlString = "SELECT * FROM indiv_records " &_
indivRS.Open sqlString
' update the customers use of the aircraft
If indivRS("indiv_reg_no") = reg_no And indivRS("indiv_name") = username
Then
' give the variables their values for the customers personal flights
VarIndiv_hours = indivRS("indiv_hours") + new_hours
VarIndiv_cycles = indivRS("indiv_cycles") + new_cycles
VarIndiv_engine1_cycles = indivRS("indiv_engine1_cycles") +
new_engine1_cycles
VarIndiv_engine2_cycles = indivRS("indiv_engine2_cycles") +
new_engine1_cycles
sqlString = "UPDATE indiv_records SET " &_
"indiv_hours ='" & VarIndiv_hours & "'," &_
"indiv_date ='" & VarLast_flight & "'," &_
"indiv_cycles ='" & VarIndiv_cycles & "'," &_
"indiv_engine1_cycles ='" & VarIndiv_engine1_cycles & "'," &_
"indiv_engine2_cycles ='" & VarIndiv_engine2_cycles & "'" &_
"WHERE indiv_reg_no ='" & reg_no & "'" & "AND indiv_name ='" & username
& "'"
Connection.Execute sqlString
Response.Write "<b>Record UPDATED</b>"
End If
If indivRS("indiv_reg_no") <> reg_no AND indivRS("indiv_name") <> username
Then
sqlString = "INSERT INTO indiv_records " &_
"(indiv_company, indiv_name, indiv_reg_no, indiv_date, indiv_hours,
indiv_cycles, indiv_engine1_cycles, indiv_engine2_cycles) " &_
"VALUES ( " &_
" '" & company & "'," &_
" '" & username & "'," &_
" '" & reg_no & "', " &_
" '" & date & "', " &_
" '" & new_hours & "', " &_
" '" & new_cycles & "', " &_
" '" & new_engine1_cycles & "', " &_
" '" & new_engine2_cycles & "' " &_
")"
Connection.Execute sqlString
Response.write "<b>Record INSERTED</b>"
End If
Message #2 by "Jose Bueno" <jbueno@i...> on Thu, 11 Oct 2001 20:39:20
|
|
Step 1: break out each procedure into a discrete function
Step 2: create a recordset that searches for the specific ID, and either
returns true or false on EOF. if true, then call the insert procedure,
else call the update procedure.
objrs.open ("Select ID from TABLE where ID = & " stringval & ";",DSN)
If objrs.eof = true then
Close your RS and connection
call insertrecord function
else
Close your RS and connection
call updaterecord function
end if
It is a bit clunky, but its a workable solution
|
|
 |