Results 1 to 2 of 2
  1. #1
    toddbailey is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2011
    Posts
    3

    How do I Insert Values w/ Not Exists sub select?

    Hi All

    I'm trying to execute this sql stmt via vb/a code

    Insert into tblRate
    (PN,LevelID,CategoryID,SubcategoryID,Hrs,LaborRate ,LaborCost,FringeRate,FringeCost,MaterialOther,[Total Direct Costs])
    select 'default',1,3,1,0,0,0,0,0,0,0 from tblRate where
    not exists (select * from tblRate where PN = 'default' and LevelID = 1 and CategoryID = 3 and SubCategoryID = 1)



    basically I want to seed a table with hardvalues if the keys (Pn, Levelid, categoryid and subcategoryid) do not exists

    this is a small part of a larger do until loop, where levelid, categoryid and subcategoryid are stored in different tables and extracted 1 row at a time to get all unique possible combinations,

    so PN will always = 'default', but level, cat and sub cat can be any integer value.

    I want to store any new id entries found in the master tables into the rate table, but only if they do not already exist.

    The sql stmt works on my test sql server box, but when I try to run it as part of a vb/a code module where the code eventually calls a DoCmd.RunSQL qstr, nothing is ever inserted, running manually I always see you are about to append 0 rows...

    If I replace the "...select 'default',1,3,1,0,0,0,0,0,0,0 from tblRate..."
    with values ('default',1,3,1,0,0,0,0,0,0,0) and eliminate the not exist sub qry the query runs,

    but I need a way to only insert the values if the keys do not exists.

    Is there a Access syntax error here or it what I'm trying to accomplish difficult in access and perhaps I need to get a row count and if 0 proceed with the insert + values code.


    Ideas/suggestions?

    thanks

  2. #2
    toddbailey is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2011
    Posts
    3
    Sorry forgot to mention Access 2010 XP i386

Please reply to this thread with any new information or opinions.

Similar Threads

  1. if exists UPDATE else INSERT
    By lloyddobler in forum Programming
    Replies: 18
    Last Post: 01-04-2012, 11:35 PM
  2. INSERT with SELECT query.
    By goodguy in forum Queries
    Replies: 8
    Last Post: 09-23-2011, 10:38 AM
  3. select and insert using two seperate tables
    By benjammin in forum Queries
    Replies: 2
    Last Post: 02-20-2011, 04:52 PM
  4. INSERT INTO ... SELECT Subquery
    By TheDeceived in forum Programming
    Replies: 1
    Last Post: 10-19-2010, 09:29 AM
  5. Replies: 0
    Last Post: 09-08-2009, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums