Solved How to use "NOT EXISTS" in MS Access? (1 Viewer)

mobarak ahmed

Member
Local time
Today, 17:07
Joined
May 28, 2021
Messages
95
Dears ,

i hope you are doing well

i use the form ' Salaries_Emp ' to record the attendance of employees to table ' Salaries_Emp '

i need when i chose the department from ' Combo_Dep '

show only Employee Name in ' Combo_Emp_Name ' that dose not recorded yet

any help will be appreciated
 

Attachments

  • Salaries 03-11-2022 - Copy.zip
    549.6 KB · Views: 88

ebs17

Well-known member
Local time
Today, 17:07
Joined
Feb 7, 2020
Messages
1,949
I cannot open the Demo, my Access version is too old.
General:
SQL:
SELECT
   A.*
FROM
   A
WHERE
   NOT EXISTS
      (
         SELECT
            NULL
         FROM
            B
         WHERE
            B.Key = A.Key
      )
Produces the same result as ...
SQL:
SELECT
   A.*
FROM
   A
      LEFT JOIN B
      ON A.Key = B.Key
WHERE
   B.Key IS NULL
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:07
Joined
Feb 19, 2002
Messages
43,331
Using Access SQL, it is more efficient to use a left join. Jet/ACE do not optimize subqueries well so you should only use subqueries when there is no alternative. In this case, the solution is a simple left join with a where clause looking for Null in the right-side PK.
 

ebs17

Well-known member
Local time
Today, 17:07
Joined
Feb 7, 2020
Messages
1,949
What needs to be optimized about this simple subquery?

If only one key is used for linking, the query is just as fast as the alternative shown. With more than one key it becomes more problematic. In return, the query can be updated without restrictions, which is also sometimes desirable.

Regardless of a specific use: You should master both variants. In this sense I understood the question.
 

mobarak ahmed

Member
Local time
Today, 17:07
Joined
May 28, 2021
Messages
95
I cannot open the Demo, my Access version is too old.
General:
SQL:
SELECT
   A.*
FROM
   A
WHERE
   NOT EXISTS
      (
         SELECT
            NULL
         FROM
            B
         WHERE
            B.Key = A.Key
      )
Produces the same result as ...
SQL:
SELECT
   A.*
FROM
   A
      LEFT JOIN B
      ON A.Key = B.Key
WHERE
   B.Key IS NULL
Sorry but I couldn’t get it i need example or with the attached file
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:07
Joined
Feb 19, 2002
Messages
43,331
What needs to be optimized about this simple subquery?
I'm pretty sure we had this conversation in a different thread so I don't want to go into it again.
There seems to be some website out there that recommends subqueries even when simple left joins will work.

@mobarak ahmed I don't understand what will be controlling the selection. the Saleries_Emp seems like a "master" table rather than a "transactions" table so there would not be a different source table that would control what records had already been added. If you were talking about adding timekeeping records for a period, then you would use a left join to the transaction table to find only employees whose time has not yet been recorded. But I don't understand the context of what you are showing.
 

isladogs

MVP / VIP
Local time
Today, 16:07
Joined
Jan 14, 2017
Messages
18,246
In case it helps. have a look at my two part article on synchronising data.
The second part includes the use of NOT EXISTS with a subquery as one of several approaches.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:07
Joined
May 7, 2009
Messages
19,247
i noticed you don't have Payroll Period in your Salaries_emp table, so i added the field.
now, see your employee combobox if it will work for you.
 

Attachments

  • Salaries 03-11-2022 - Copy.zip
    590 KB · Views: 107

mobarak ahmed

Member
Local time
Today, 17:07
Joined
May 28, 2021
Messages
95
So not prepared to do any work yourself, just keep asking someone for solutions? :(
I’m not a professional access user i am an accountant trying to simplify my work
So i try to do my best and when i face hard problem ( for me ) i ask for help
Sorry if anyone fell annoyed by me
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:07
Joined
Sep 21, 2011
Messages
14,338
Neither was I. I used it in my employment, pretty much as you are doing now.

I got more satisfaction by doing most of the work myself.
I asked here for help when I needed it, and I received it here and was grateful for that help.

However I also wanted to learn as I went. For one it is quicker than waiting for hopefully a solution, and two, again that satisfaction of working it out yourself.
 

mobarak ahmed

Member
Local time
Today, 17:07
Joined
May 28, 2021
Messages
95
Neither was I. I used it in my employment, pretty much as you are doing now.

I got more satisfaction by doing most of the work myself.
I asked here for help when I needed it, and I received it here and was grateful for that help.

However I also wanted to learn as I went. For one it is quicker than waiting for hopefully a solution, and two, again that satisfaction of working it out yourself.
I have tried to solve it my self and searched a lot before asking for help here however it would be simple for someone else
I want to say I have tried the hard before the easy one
 

Users who are viewing this thread

Top Bottom