[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Append - with Condition

Posted on 2011-10-23
26
Medium Priority
?
447 Views
Last Modified: 2013-11-27
I need to Append but not all records.
If there is a match between the Projects table and Import-CSM then DO NOT APPEND.
I have the below, but I am not sure of the language for a NOT LIKE criteria

Basically, Append if the criteria is NOT LIKE.  
The ProjectNo and Activity code are text

obviously the below is wrong (it is in plain English)
 
[Projects].[ProjectNo] NOT LIKE [Import-CSM].[Activity Code]

here is the APPEND SQL:
INSERT INTO Projects ( ProjectNo, [Project Name] )
SELECT [Import-CSM].[Activity Code], [Import-CSM].[Activity Description]
FROM [Import-CSM]
WHERE (([Projects].[ProjectNo]<>[Import-CSM].[Activity Code]));

Please see the pic
 AppendNotLIke
0
Comment
Question by:pdvsa
  • 11
  • 6
  • 3
  • +2
26 Comments
 

Author Comment

by:pdvsa
ID: 37015346
I have NOT EQUAL to in the picture though.  When I run it, a msgbox appears asking for Projects.ProjectNo (I guess because Access cant find the control but it is spelled correctly).  Little difficult to explain.  Please ask if have questions.
0
 

Author Comment

by:pdvsa
ID: 37015347
I need NOT LIKE language
0
 

Author Comment

by:pdvsa
ID: 37015376
The reason I dont want to Append if on that certain NOT LIKE criteria is because if there is a match then that mean that project is already in the db.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 75
ID: 37015400
Well, first you need the Projects table on the query grid.  And are Project and  Activity Code the same data type?

mx
0
 
LVL 75
ID: 37015402
OR ... sounds like you really need this (example tables and names):

SELECT Table1.*
FROM Table1
WHERE (((Table1.FIELD1) Not In (SELECT Table2.FIELD2 FROM Table2;)));

Table1 would be IMPORT-CSM
Table2 would be Projects
Field 1 would be ProjectNo
and Field2 would be Activity Code

mx
0
 

Author Comment

by:pdvsa
ID: 37015406
Mx:  If I am Appending to the Projects table then I thought I did not need it visible in the query grid...

like so in the picture.  I am appending to it so it is not visible...I select Projects after clicking the Append button.  Maybe I am missinging something.


untitled.JPG
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37015565
Make a backup and try revising your original SQL like this:

INSERT INTO Projects ( ProjectNo, [Project Name] )
SELECT [Import-CSM].[Activity Code], [Import-CSM].[Activity Description]
FROM [Import-CSM] LEFT JOIN Projects ON [Projects].[ProjectNo] = [Import-CSM].[Activity Code]
WHERE [Projects].[ProjectNo] IS NULL

0
 
LVL 75
ID: 37015595
Sorry ... I missed the APPEND ...
0
 

Author Comment

by:pdvsa
ID: 37015830
OK I did not know I could drag a table in.  

I would need a Not Like instead of the Is Null on project no.
Can you help with that?
I also think I need the same on the [Import-CSM].[Reference Number])

Thank you so much..

Here is how I have it now...just like your previous post but I added tblLetterOfCredit and the criteria on Import-CSM].[Reference Number]

INSERT INTO Projects ( ProjectNo, [Project Name] )
SELECT [Import-CSM].[Activity Code], [Import-CSM].[Activity Description]
FROM ([Import-CSM] LEFT JOIN Projects ON [Import-CSM].[Activity Code] = Projects.[ProjectNo]) INNER JOIN tblLetterOfCredit ON [Import-CSM].[Reference Number] = tblLetterOfCredit.LCNo
WHERE (((Projects.ProjectNo) Is Null)) OR ((([Import-CSM].[Reference Number])<>[LCNo]));


0
 
LVL 77

Expert Comment

by:peter57r
ID: 37016275
If what you are trying to do is add  new records into Projects then do what Miriam said.

You DON'T want 'not like'.
 EVERY import record is 'not like' at least one record in Projects and so wil get appended.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37017008
Can you post some sample data with desired results?

Do you (as an example) you want to import a record (say [Activity Code] = 'abc') ONLY if there are no records in Projects where ProjectNo is along the lines of 123abc, abc123,xyzabc123 or anything else containing 'abc'?

If that is what you need, you might be able to do something like this (which might be quite slow):

INSERT INTO Projects ( ProjectNo, [Project Name] )
SELECT [Activity Code], [Activity Description]
FROM [Import-CSM]
WHERE DCount("*","Projects","[ProjectNo] LIKE '*" & [Activity Description] & "*'") = 0

Or is it the other way around (ie: you only want to import records where NO PART of [Activity Description] is contained in any [Project Code]?  As Pete said, I don't think this is doable.  

Anyhow, post a little more detail so that we can either get this working with the original two tables or rule it out as impossible before bringing another table in to the mix.




0
 
LVL 9

Expert Comment

by:keyu
ID: 37017050
Hi,

Try like this.

INSERT INTO Projects ( ProjectNo, [Project Name] ) (select  p.ProjectNo, p.[Project Name]  from projects p innerjoin Import-CSM Im on (p.[ProjectNo]<>Im.[Activity Code]));

Hope this Helps..

Thanks,
0
 

Author Comment

by:pdvsa
ID: 37018108
Keyu:  seems to be a syntax error in there (starting at the (select part of it
INSERT INTO Projects ( ProjectNo, [Project Name] ) (select  p.ProjectNo, p.[Project Name]  from projects p innerjoin Import-CSM Im on (p.[ProjectNo]<>Im.[Activity Code]));

Mbizup:
<Or is it the other way around (ie: you only want to import records where NO PART of [Activity Description] is contained in any [Project Code]?  As Pete said, I don't think this is doable.  

yes, this is what I am looking for.  

IDEA:
Maybe I can have the NOT LIKE code outside of the APPEND qry?
(meaning possibly having 2 separate APPEND qry's...
1) if NOT LIKE the APPEND
2) If LIKE then dont do anything


I dont know...just an idea.  I think that Pete and I talked about this before
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_27197019.html
but I dont know if a Dcount could be applied here or not.  

0
 
LVL 77

Expert Comment

by:peter57r
ID: 37018218
I'm lost.
Some sample data values might help (me, at least).
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37018312
My understanding is that this is reprsentative (and I believe it is impossible)....


Project (we don't want to import records where [Activity Code] contains these strings)
-------
abc
def



Activity Code
--------
abc123
345def
123xyz345   '<--- this would be the only record imported

0
 

Author Comment

by:pdvsa
ID: 37019012
mbizup:  yes, this is true.  
0
 
LVL 9

Expert Comment

by:keyu
ID: 37022664
Hi,

here correct index of the query:

INSERT INTO Projects ( ProjectNo, [Project Name] ) (select  p.ProjectNo, p.[Project Name]  from projects p where (p.[ProjectNo] not in (select m.[Activity Code] from [Import-CSM] m)))

Thnks,
0
 
LVL 77

Expert Comment

by:peter57r
ID: 37022867
Try this as a Select query (check all the names first)..


Select * from Projects as Q where q.ProjectNo Not in
(Select Distinct P.ProjectNo from Projects as P , Import_csm as I
where  instr(I.[Activity code] , P.Projectno)>0)
0
 

Author Comment

by:pdvsa
ID: 37024148
Keyu:  
seems as though there is a syntax error after running.  
It highlights at the bold part:
INSERT INTO Projects (ProjectNo, [Project Name] ) (select  p.ProjectNo, p.[Project Name]  from projects p where (p.[ProjectNo] not in (select m.[Activity Code] from [Import-CSM] m)))

Peter:  seems like I have a syntax as well....see picture please

thank you for the help...I know it is difficult if cant see project.  

(I do have the db here if you want to take a peek...it is pared down and only contains the necessary)
https://skydrive.live.com/redir.aspx?cid=501a06dcdb8cc24b&resid=501A06DCDB8CC24B!1908


untitled.JPG
0
 
LVL 77

Expert Comment

by:peter57r
ID: 37024263
I don't get an error running my query in your database.
0
 

Author Comment

by:pdvsa
ID: 37024680
Peter:  did you change the name of the table

from
Import_csm

to
Import-csm

after I made the change it gives me the error.
0
 
LVL 77

Expert Comment

by:peter57r
ID: 37025423
yes I changed the table name.

I'm attaching my version.  Just run query1.
db-Import2k7.accdb
0
 

Author Comment

by:pdvsa
ID: 37026053
OK ...no syntax now.  

However, I do have an issue.  
When I run query1 it returns 24 records.
It should return many more.

REason:  if there is no match between import-csm.Activity Code and Projects.ProjectNo then import the ones from Import-csm that dont have a match (meaning NOT LIKE i guess) on Projects.ProjectNo  

this means that I am importing the ones that are not already in the db contained under the Projects table.  Another way of saying is I am adding what is in table import-csm that is not already contained in table projects. if I did then there would be duplicates.

I hope it makes better sense now.  
What is next?  Let me know what you decide.  

----------here is the SQL of query1
SELECT *
FROM Projects AS Q
WHERE (((Q.ProjectNo) Not In (Select Distinct P.ProjectNo from Projects as P , [Import-csm] as I
where  instr(I.[Activity code] , P.Projectno)>0)));


I attached an excel spreadsheet...
Matches-Peter.xls
0
 
LVL 77

Accepted Solution

by:
peter57r earned 2000 total points
ID: 37029911
OK.
I had it the wrong way round.

Have a look at this version.
Query1 should show the data to be imported, based on what I now understand.

db-Import2k7a.accdb
0
 

Author Closing Comment

by:pdvsa
ID: 37050910
that was it.  thank you
0
 
LVL 9

Expert Comment

by:keyu
ID: 37069188
you have not followed exact syntex writen by me..its working fine with me.

anyways thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question