Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
azim
Starting Member
29 Posts |
Posted - 2002-02-17 : 06:50:17
|
| i use adodc1i want code to how i can prevent data from savingtable consistis of 4 feildsstudentIdDegreesubjectyearif the subject and studentid and year are equal existingrecord prevent savingelse save dataazim |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2002-02-17 : 07:14:40
|
| HiYou can do this in a few ways.One, put it all in a stored procedure and perhaps a transaction as well. Test for the ID, if it doesn't match, then do the insert, otherwise don't insert.Two, use a trigger. A trigger will run when you try to insert data, have it check for a duplicate and rollback the insert if it is there.Three, use some constraints so that it will throw errors of you try to insert duplicate data.They are the basic methods, for specific detail look up the words I have put in bold in Books Online.Hope that helpsDamian |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-02-17 : 08:51:10
|
| Easiest way is probably to use the constraint (or unique index) on studentID, Subject, Year. This will throw a database error so if this is something that you expect to happen (i.e. the application doesn't try to stop it) you should probably do a check in the stored procedure as well to send back a return code.You should probably have this constraint even if the code does prevent it.Put the index fields in the order that will be most useful for queries.==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
azim
Starting Member
29 Posts |
Posted - 2002-02-19 : 06:12:55
|
| pleas answeri put this codebut also return errorcode:--------------------------------------------------------------------------------Private Sub cmdSave_Click()On Error GoTo ERRORMASGAdodc4.RecordSource = "select * from DEGREES where studentid like '" & Val(Text1.Text) & "' AND SUBJECT LIKE '" & Val(Text5.Text) & "' AND YEAR LIKE '" & Val(Text9.Text) & "'"Adodc4.RefreshDim Rs As ADODB.RecordsetSet Rs = Adodc4.RecordsetAdodc4.Recordset.MoveLastIf Adodc4.Recordset.RecordCount >= 1 ThenMsgBox "this degrees already recorded"ElseAdodc4.Recordset.UpdateBatchAdodc4.RefreshAdodc4.Recordset.AddNewMsgBox "degrees recorded", vbApplicationModal + vbOKOnlyText11.Text = ""End IfExit SubERRORMASG:MsgBox "check input data"End Subazim |
 |
|
|
|
|
|