sql server - MS SQL 2008/Access 2002 VBA - Check DB for current record, if does not exist then enter it -
i'm trying create sql string check table notification current record criteria (two different id values). if finds record both of these values, won't enter record table. if doesn't find it, will.
i've tried using vba solve this, seems way i'm going able use sql because of constant type mismatch errors result access field types not being same sql field types (ie: integer being ok in sql, value causing overflow in access vba).
i've been trying find sort of statement let me check table see if assetid , notificationtypeid in table. if are, ignore insert , move on.
i've seen examples of other types of sql answer question, can't them work in vba.
updated code(note:'i know, assetid should long. it's int in sql, when set int in vba access, overflow message 'when try set long, there's type mismatch. string seems work in sql @ moment putting values database )
this still isn't working @ .addnew. should, reason returns invalid operation error.
dim response integer dim strsql string dim delsql string dim nottypeid string dim notdate date dim assetid string dim rcdcount integer dim integer dim rst dao.recordset dim db dao.database dim qdf dao.querydef dim rcd dao.recordset dim strselect string strgroup = returngroup 'check user credentials before showing notifications if instr(1, strgroup, "not_admins") or instr(1, strgroup, "admins") docmd.setwarnings true 'check storage location query, see if there reason notify user if dcount("*", "qry_unknownstorageloc") > 0 'getting record count query rcdcount = dcount("*", "qry_unknownstorageloc") 'this popup message box shown user when fassetrack loads 'response = msgbox("notice: " & dcount("*", "qry_unknownstorageloc") & " record(s) contain unknown storage location", vbinformation + vbokonly, "unknownstorage") strsql = "select assetid qry_unknownstorageloc" set db = currentdb set rst = db.openrecordset(strsql, dbopensnapshot) = 1 'loop through gather records notification type 'and add them notifications table rst until .eof 'set assetid value, move next record in query assetid = rst!assetid 'nottypeid id of notification type in notificationtype table nottypeid = 1 rst.movenext 'setting notification date last date record modified 'the logic being last edit triggered notification. when record 'corrected and/or acknowledged, no longer trigger notification. 'null checking ensure no errors occur if (isnull(dlookup("modifiedon", "qry_unknownstorageloc"))) notdate = 0 else notdate = dlookup("modifiedon", "qry_unknownstorageloc") end if ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' strselect = "select n.notificationtypeid, n.notificationdate, n.assetid" & vbcrlf & _ "from notifications n" & vbcrlf & _ "where n.notificationtypeid = [ptype] , n.notificationdate = [pdate] , n.assetid = [pid];" debug.print strselect set qdf = db.createquerydef(vbnullstring, strselect) qdf .parameters("ptype").value = nottypeid .parameters("pdate").value = notdate .parameters("pid").value = assetid set rs = .openrecordset(dbopendynaset, dbseechanges) end rs if .bof , .eof .addnew !notificationtypeid.value = nottypeid !notificationdate.value = notdate !assetid.value = assetid .update end if .close end = + 1 loop end 'close , clear recordset rst.close set rst = nothing end if
consider loading recordset temporary querydef
based on parameter query. if recordset empty, no matching record exists, can add record.
dim db dao.database dim qdf dao.querydef dim rs dao.recordset dim strselect string strselect = "select n.notificationtypeid, n.notificationdate, n.assetid" & vbcrlf & _ "from notification n" & vbcrlf & _ "where n.notificationtypeid = [ptype] , n.notificationdate = [pdate] , n.assetid = [pid];" 'debug.print strselect set db = currentdb set qdf = db.createquerydef(vbnullstring, strselect) qdf .parameters("ptype").value = nottypeid .parameters("pdate").value = notdate .parameters("pid").value = assetid 'set rs = .openrecordset set rs = .openrecordset(dbopendynaset, dbseechanges) end with rs if .bof , .eof .addnew !notificationtypeid.value = nottypeid !notificationdate.value = notdate !assetid.value = assetid .update end if .close end
Comments
Post a Comment