Access SQL problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Petrol
    Contributor
    • Oct 2016
    • 295

    Access SQL problem

    Can anyone tell me if there is any simple way in Access SQL of deleting all records in tblA that are specified by having a matching record in tblB?
    In other words, I want something like
    Code:
     DELETE tblA.* FROM tblA WHERE tblA.[fieldname] = tblB.[fieldname]
    - except that that doesn't comply with the syntax for a DELETE statement.

    I tried the obvious method - creating a SELECT query by putting the two tables on the design canvas, linking them by the shared field name and putting tblA.* as the only column in the design grid (this then displays the wanted records) and then converting it to a DELETE query - but the SQL that Access generates also doesn't comply with the syntax and doesn't work.
    It sounds like a simple enough task - but I can't figure it out. Surely it must be possible?
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32662

    #2
    Try :
    Code:
    DELETE [tblA].*
    FROM [tblA]
         INNER JOIN
         [tblB]
      ON [tblA].[fieldname] = [tblB].[fieldname]

    Comment

    • Petrol
      Contributor
      • Oct 2016
      • 295

      #3
      Yes, I had tried that, thanks NeoPa.
      For some reason it gives an unnumbered message "Specify the table containing the records you want to delete" - although the table clearly was specified.
      Code:
      DELETE Applicant_Transient_info.*
      FROM Applicant_Transient_info INNER JOIN temptbl_ObserversThisWalk 
         ON Applicant_Transient_info.PersonID = temptbl_ObserversThisWalk.PersonID;

      Comment

      • Petrol
        Contributor
        • Oct 2016
        • 295

        #4
        I actually want to delete corresponding records from another table, too. I tried the same SQL with this second table and got a different message - also unnumbered -"Could not delete from specified tables".
        Code:
        DELETE Walk_participation_history.*
        FROM  Walk_participation_history INNER JOIN temptbl_ObserversThisWalk
             ON Walk_participation_history.PersonID = temptbl_ObserversThisWalk.PersonID;
        (What looks like a P at the end is actually a semicolon in the SQL).

        Comment

        • Petrol
          Contributor
          • Oct 2016
          • 295

          #5
          Sorry, no, it was just that the SQL was truncated by the post).

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32662

            #6
            Unfortunately, my version of Access (2019) has no information on that subject I was able to find :-(

            Older versions certainly included information on the DELETE clause, but 2019 seems to have mislaid it while trying to rewrite it for some reason.

            I'll do some simple Googling if I can and possibly some tests if I can. I suspect the problem is unrelated to this syntax as I remember doing this successfully before, but it's a very rare need so finding my own examples hasn't been straightforward .

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32662

              #7
              Perhaps instead try :
              Code:
              DELETE [tblA]
              FROM   [tblA]
                     INNER JOIN
                     [tblB]
                ON   [tblA].[fieldname] = [tblB].[fieldname]
              This is the same as before except that it leaves out the .* part.

              Comment

              • Petrol
                Contributor
                • Oct 2016
                • 295

                #8
                Thanks, NeoPa, but please don't waste any more of your valuable time on it. I have been struggling with this one for about 2 weeks now and I think I have tried pretty well every possibility.
                However, I have thought of another way around the problem. Fortunately, when anyone enters the system I know if they will be a temporary participant. So I will change the design of the various tables to include a "temporary" flag, and when a "temporary" participant enters the system the queries that create the relevant records can set those flags. Then to delete them all I should need to do is simple DELETE queries with only table in each.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32662

                  #9
                  OK well, at least you have a way past your problem for now :-)

                  Comment

                  Working...