Results 1 to 3 of 3
  1. #1
    tmaleshafske is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    1

    Caculated field with if then conditions


    I am working on a database for my employer in regards to yield. Because we do a lot of high moisture corn we have to shrink the weights to dry bushels, but once we are down to a specific moisture then we no longer shrink it and just go by weight I want this to be invoked on insert and into a specific column of that particular row. I can get it to work with one portion, but not on an if statment what am I doing wrong

    Here is my code
    Code:
    If [Corn Yield]![Moisture] >= "15.5" Then [DRY BUSHELS] = [Net Weight] / 56
    ElseIf [Moisture] < "15.5" Then [DRY BUSHELS] = [Net Weight] / (56 / (1 - (([Moisture] - 15.5) * 0.013)))

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Why doesn't it work - error message, wrong results, nothing happens?

    Assume [Moisture] field is a number type, don't put the value in quote marks.

    Where is this statement - in a VBA procedure or in a query or in a textbox? If you are using in the latter two then must use an IIf function:

    IIf([Moisture] >= 15.5, [Net Weight] / 56, [Net Weight] / (56 / (1 - (([Moisture] - 15.5) * 0.013))))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I'm a little confused on where you are using this - in code or in a query.

    If you want this in a query, you need to use the IIF() version of the IF...THEN function:
    Code:
    IIF([Corn Yield]![Moisture] >= 15.5,[DRY BUSHELS] = [Net Weight] / 56,[Net Weight] / (56 / (1 - (([Moisture] - 15.5) * 0.013))))
    If used in code, you should use this form of the IF...THEN function:
    Code:
    If [Corn Yield]![Moisture] >= 15.5 Then 
       [DRY BUSHELS] = [Net Weight] / 56
    ElseIf [Moisture] < "15.5" Then 
       [DRY BUSHELS] = [Net Weight] / (56 / (1 - (([Moisture] - 15.5) * 0.013)))
    End IF
    I removed the quotes around the 15.5 in the first condition.
    "15.5" (a text string) is not equal to 15.5 (a number).

    It is confusing on your field/variable names. Some places you use "[Corn Yield]![Moisture]", other places you use "[Moisture]".
    Some names are in all caps, others are mixed case.



    Also, you shouldn't use spaces in Access object names. It just causes you headaches. See http://access.mvps.org/access/tencommandments.htm #3

    If you must use all caps, use the underscore "DRY_BUSHELS". Or use "DryBushels"

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 08-18-2012, 11:32 PM
  2. Replies: 3
    Last Post: 08-01-2012, 10:56 AM
  3. Replies: 1
    Last Post: 03-27-2012, 04:49 PM
  4. if...then conditions ???
    By em07189 in forum Access
    Replies: 6
    Last Post: 03-05-2010, 10:29 PM
  5. Query result based upon two field conditions
    By diane802 in forum Access
    Replies: 35
    Last Post: 01-08-2010, 06:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums