By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,644 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,644 IT Pros & Developers. It's quick & easy.

primary key in vb with database sql?

100+
P: 103
can anyone please tell the code for pk constraint of id with a sql database in vb prgm?
i have written the code as"
code(vb):

rs1.Open "select * from empdetail", conn, adOpenStatic, adLockOptimistic
rs1.MoveFirst
While Not rs1.EOF
If rs1!id = txtid.Text Then
MsgBox ("Duplicate Entry")
Exit Sub
Else

txtname.SetFocus
conn.Execute "insert into empdetail(id,name,whours,rate,otrate) values ('" & txtid & "','" & txtname & "','" & txtwhr & "','" & txtrate & "','" & txtorate & "')"
MsgBox "INSERTED NEW RECORDS", vbOKOnly, "ADDING NEW RECORDS"
addcol
End If
Wend
rs.close
set rs = nothing
end sub


ERROR:operation cannot be done qwhen object ids open

THANKS IN ADVANCE
Dec 15 '07 #1
Share this Question
Share on Google+
1 Reply


QVeen72
Expert 100+
P: 1,445
Hi,

To Check for PK, open a Specific Record..
Not Necessary to Open Whole Table, and Loop..

Expand|Select|Wrap|Line Numbers
  1.  
  2. rs1.Open "select * from empdetail Where ID='" & Trim(txtID.Text) & "'", conn, adOpenStatic, adLockOptimistic
  3. IF RS1.EOF Then
  4.     conn.Execute "insert into empdetail(id,name,whours,rate,otrate) values ('" & txtid & "','" & txtname & "','" & txtwhr & "','" & txtrate & "','" & txtorate & "')"
  5. MsgBox "INSERTED NEW RECORDS", vbOKOnly, "ADDING NEW RECORDS"
  6. Else
  7.   ' ID Found In DB (Duplicate)
  8.    MsgBox "Duplicate Entry"
  9. End If
  10.  
  11. RS1.Close
  12. Set RS1 = Nothing
  13.  
Regards
Veena
Dec 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.