• Delete Query (97)

    Author
    Topic
    #393513

    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

    Viewing 3 reply threads
    Author
    Replies
    • #714008

      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.

      • #714014

        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! !!

      • #714024

        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! !!

    • #714009

      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.

    • #714125

      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.

      • #714545

        I haven’t tested this but I would expect it to work if you used a subquery something like this.

        DELETE tblPayments.*
        FROM tblPayments
        WHERE (((tblPayments.dtmDate)<#1/1/2003#)  and ( tblPayments.fk_RecordNumber in
        (Select tblOriginalData.fk_RecordNumber from tblOriginalData))); 
        

        edited by John to correct my mistakes with cutting and pasting

        • #717160

          John,

          Thanks! I will give it test!

          Ken

        • #717161

          John,

          Thanks! I will give it test!

          Ken

      • #714546

        I haven’t tested this but I would expect it to work if you used a subquery something like this.

        DELETE tblPayments.*
        FROM tblPayments
        WHERE (((tblPayments.dtmDate)<#1/1/2003#)  and ( tblPayments.fk_RecordNumber in
        (Select tblOriginalData.fk_RecordNumber from tblOriginalData))); 
        

        edited by John to correct my mistakes with cutting and pasting

    • #714126

      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.

    Viewing 3 reply threads
    Reply To: Delete Query (97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: