Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update a table if recordset exists????

Status
Not open for further replies.

donniea21

MIS
Feb 16, 2001
75
US
I have a multi page project which inserts data into tables in db....is there a way to make it so a page will search for existing recordset..if there is one....it will do an update if user changes info..if there isn't one than it will do an insert...i can see it as in if/else statement but can't figure out the syntax...thanks
 
ok, say I have a database with two fields name and password where all the names must be unique in a table called names. So...

set rs = Server.CreateObject("ADODB.Recordset")
set conn = Server.CreateObject("ADODB.Connection")
...open your connection to your database
name = request.form("name")
password = request.form("password")

SQL = "SELECT * FROM names where name='" & name & "'"
rs.open SQL, conn

'If it didn't find a name, INSERT, otherwise, UPDATE
if rs.eof then
rs.close
SQL = "INSERT INTO names (name,password) VALUES ('" & name & "','" & password & "')"
conn.Execute(SQL)
else
rs.close
SQL = "UPDATE names SET name='" & name & "', password='" & password & "' WHERE name='" & name & "'"
conn.Execute(SQL)
end if
set rs = nothing
conn.close
set conn = nothing

There you go!

Harold Blackorby
hblackorby@scoreinteractive.com
St. Louis, MO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top