Access VBA/Prevent duplicate values -


how can prevent duplicate values not insert table. have created code insert, update , delete , want display msgbox there duplicate value , cancel it. thanks. below have code:

private sub command12_click()      if me.emid.tag & "" = ""          if (isnull(me.emid) or (me.emid = "") or isnull(me.emfirst) or (me.emfirst = "") or isnull(me.emlast) or (me.emlast = ""))             me.emid.bordercolor = vbred             me.emfirst.bordercolor = vbred             me.emlast.bordercolor = vbred             msgbox "please fill required fields", vbinformation, "information"             exit sub         end if               currentdb.execute "insert tblemployees(emid, first, last, gender, phone, mobphone, city, state, zip, adress, email, comment)" & _             "values ('" & me.emid & "', '" & me.emfirst & "', '" & me.emlast & "', '" & me.emgender & "', '" & me.emphone & "', '" & me.emmob & "', '" & me.emcity & "', '" & me.emstate & "', '" & me.emzip & "', '" & me.emadress & "', '" & me.ememail & "', '" & me.emcomment & "')"             msgbox "record added", vbinformation, "information"              else                 currentdb.execute "update tblemployees " & _                 "set emid =" & me.emid & _                 ", first ='" & me.emfirst & "'" & _                 ", last = '" & me.emlast & "'" & _                 ", gender ='" & me.emgender & "'" & _                 ", phone = '" & me.emphone & "'" & _                 ", mobphone ='" & me.emmob & "'" & _                 ", city ='" & me.emcity & "'" & _                 ", state ='" & me.emstate & "'" & _                 ", zip ='" & me.emzip & "'" & _                 ", adress ='" & me.emadress & "'" & _                 ", email ='" & me.ememail & "'" & _                 ", comment ='" & me.emcomment & "'" & _                 "where emid =" & me.emid.tag                 msgbox "updated!", vbinformation, "information"     end if  me.tblemployees_subform.form.requery  end sub 

you can change sql use if exists condition , insert if records not exists.

your sql may like:

if not exists ( select ...... )

    begin         insert tblemployees ......<insert since employee not exists>     end 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -