Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Delete Query (97)

    I am having difficulties creating a delete query where I have two tables, tblPayments and tblOriginalData, and want to delete records from the former, IF the record is older than 2003 AND there is a matching fk_recordnumber in the latter.

    The table structures:
    tblPayments
    pkey
    fk_recordnumber
    strType
    curAmount
    dtmDate

    tblOriginalData
    pkey
    fk_recordnumber
    curAmount
    curMed
    curPHA

    I need to delete all records from tblPayments where the date (dtmDate) is before Jan 1, 2003 AND there is a matching fk_recordnumber in tblOriginalData.

    I created a query using the wizard which returned the following SQL:

    DELETE tblPayments.*, tblPayments.dtmDate
    FROM tblPayments LEFT JOIN tblOriginalData ON tblPayments.fk_RecordNumber= tblOriginalData.fk_RecordNumber
    WHERE (((tblPayments.dtmDate)<#1/1/2003#));

    If I VIEW the query results, it shows the correct records to be deleted, but when I RUN the query I get an error message stating "Operation must be an updatable query".

    I also modified the join in the query query which resulted in the following SQL statement:

    DELETE tblPayments.*, tblPayments.dtmDate
    FROM tblPayments, tblOriginalData
    WHERE (((tblPayments.dtmDate)<#1/1/2003# And [tblOriginalData]![fk_RecordNumber]=[tblPayments]![fk_RecordNumber]));


    Still get the same error message. Is there anything obvious that I am missing?

    Thanks,

    Ken

  2. #2
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Delete Query (97)

    AFAIK you should make sure that the fields fk_recordnumber are defined as primary keys in both tables, then define the link between in the relationships window.

  3. #3
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: Delete Query (97)

    Patt,

    Thanks. I tried setting the relationship to the foreign key field (recordnumber) with no success. I even used a make table query to create a table of the records needing to be deleted, then create a delete query using the newly created table and the tblPayments table, relating to the pkey (a primary field) in both, and continue to get the same error. gggrrrrrrrrrrrrrrrrr! !!

  4. #4
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: Delete Query (97)

    Although there must be a cleaner and kewler way of deleting these records, I ended up using an update query and then a delete query. I added a field to the first table, updated the new field with string if it was to be deleted, then ran a delete query for all records that contained that string.

  5. #5
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Delete Query (97)

    I haven't tested this but I would expect it to work if you used a subquery something like this.
    <pre>DELETE tblPayments.*
    FROM tblPayments
    WHERE (((tblPayments.dtmDate)<#1/1/2003#) and ( tblPayments.fk_RecordNumber in
    (Select tblOriginalData.fk_RecordNumber from tblOriginalData)));
    </pre>

    edited by John to correct my mistakes with cutting and pasting
    Regards
    John



  6. #6
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: Delete Query (97)

    John,

    Thanks! I will give it test!

    Ken

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •