Append query with criteria

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robin a
    New Member
    • Feb 2012
    • 30

    Append query with criteria

    Hi,
    I have an append query that has to add a record to a table [Join_tbl] if the field [ReqUNID] does not exist (there are other fields added as well with their own criteria) OR If the [ReqUID] already exists but another field [TaskStatus] has changed, then overwrite the [TaskStatus] field only. This is the code I have that only adds a record if the [ReqUID] doesn't exist.
    Code:
    INSERT INTO Join_tbl
       ( ReqUNID, 
          TaskTeam, 
          TaskAssignee, 
          taskStatus )
    SELECT dbo_vwAssignments.ReqUNID, 
       dbo_vwAssignments.TaskTeam, 
       dbo_vwAssignments.TaskAssignee, 
       dbo_vwAssignments.taskStatus
    FROM dbo_vwAssignments
    WHERE (
       ((dbo_vwAssignments.TaskTeam)
          ="chp-vm")
        AND ((dbo_vwAssignments.taskStatus)
               ="acknowledged" 
             Or (dbo_vwAssignments.taskStatus)
                  ="new" 
             Or (dbo_vwAssignments.taskStatus)
                  ="pending" 
             Or (dbo_vwAssignments.taskStatus) Is Null));
    Thank you in advance for your help
    Last edited by zmbd; Dec 14 '12, 06:08 PM. Reason: [Z{Stepped SQL for easier read}{missed a step:)}]
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You will need two querys. An append query and an update query. I don't think you can do it with just one query in Access.

    Comment

    • robin a
      New Member
      • Feb 2012
      • 30

      #3
      thank you, i did get to that point after i asked for help. You are quick, (Rabbit). I built the queries but for some reason my update query makes the data in the field to be updated blank. This is my update query:
      Code:
      UPDATE Temp_Join_tbl
         INNER JOIN Join_tbl 
            ON Temp_Join_tbl.ReqUNID 
               = Join_tbl.ReqUNID 
      SET Join_tbl.ReqUNID 
            = [Temp_Join_tbl].[ReqUNID], 
         Join_tbl.Status 
            = [Temp_Join_tbl].[Status]
      WHERE (
         ((Join_tbl.ReqUNID)
               =[Join_tbl].[ReqUNID])
      );
      I'm trying to update the Join_tbl.[status] with the value in Temp_Join_tbl.[Status] WHERE Temp_Join_tbl.R eqUNID = Join_tbl.ReqUNI D SET Join_tbl.ReqUNI D

      Any suggestion?
      Last edited by zmbd; Dec 14 '12, 06:03 PM. Reason: [Z{Stepped SQL for easier read}{added set of code tags}]

      Comment

      • robin a
        New Member
        • Feb 2012
        • 30

        #4
        sorry, i since removed the ReqUID field from the query and it works fine, but I don't understand why.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32645

          #5
          Originally posted by Rabbit
          Rabbit:
          You will need two querys. An append query and an update query. I don't think you can do it with just one query in Access.
          Until quite recently I would have agreed with that. It turns out however, that an UPDATE query will act as an APPEND query in Access (Jet) when no match existing record is found.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32645

            #6
            Originally posted by Robin
            Robin:
            sorry, i since removed the ReqUID field from the query and it works fine, but I don't understand why.
            If you were trying to set an AutoNumber value from another table then it would fail. AutoNumber values can only ever be set explicitly within an APPEND action. When you removed the code to set that the query could work.

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              Post #3 Codeblock-Line 10: The WHERE clause with the SQL refers to the same table and same field; However, this is not as indicated in the sentence following wherein that statement refers to the fields between the two tables.

              If the [ReqUID] is an autonumber it should have erred, not cleared fields.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32645

                #8
                Originally posted by Z
                Z:
                If the [ReqUID] is an autonumber it should have erred, not cleared fields.
                Good spot. I didn't see (overlooked) that detail :-(

                So was the spot of the error in the OP's WHERE clause. Sharp today Z :-)

                Comment

                Working...