Results 1 to 5 of 5

Thread: IF statement in Msaccess query

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    53

    IF statement in Msaccess query

    Hi,

    Can anybody tell what is the correct syntax for a query in msacess when using the If statement. I want to convert the following statement to a query.


    If Rs![Delivered] < rs![NewDelivery] Then
    rs![Delivered] =NewDelivery
    End If

    There is a table that contains the Delivered field. I want to check to see if the Delivered field is less then the NewDelivery if so replace the Delivered date with the NewDelivery.

    I have type the following on the query, but I get a syntax error.

    = If [Delivered] < NewDelivery Then[Delivered] = NewDelivery
    End If

    I have look in to the Iff statement but is not what I want to want to replace, it seems that the Iff statement returns true of false it does not let me replace the value on the table.

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    53

    Smile Re: IF statement in Msaccess query

    I could not get it to work with the if Statement but the following does the SQl something.

    UPDATE Delivered SET Delivered.ShipDate = [Delivered]
    WHERE (((Delivered.ShipDate)>[Delivered]));

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: IF statement in Msaccess query

    IIF does not return True/False unless you explicitly tell it to - as you specify what it returns.

    However, in this case there is no need for any kind of If, all you need to do is add to (or alter) the Where clause.

    This is the equivalent of your original code snippet (except it alters all records in the table):
    Code:
    UPDATE Delivered SET Delivered.ShipDate = [Delivered]
    WHERE [Delivered] < Delivered.NewDelivery;

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    53

    Thumbs up Re: IF statement in Msaccess query

    So if wanted to altered a specific record, such a name the following code should work.

    UPDATE Delivered SET Delivered.ShipDate = [Delivered]
    WHERE [name] = 'Al Bundy' and [Delivered] < Delivered.NewDelivery;

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: IF statement in Msaccess query

    That's right... but if there is more than one record with the name 'Al Bundy', it will alter all of those.

Posting Permissions

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



Click Here to Expand Forum to Full Width