Results 1 to 15 of 15
  1. #1
    cptdave1996 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    16

    Setting default with an IIF Function

    Would it be wise to set a default for a field in a table with and IIF Function?
    I have a DB where we have two terminals and one terminal we want to set a goal for miles ran 6600 and the other at 8800.


    I had 8800 set as the default but now boss wants it like this.
    if this is ok to do, would anyone know how to write it. I am still new to access.
    I have uploaded a copy of the DB.
    Attached Files Attached Files

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    no you cant, tho you build your own default table.
    I have a table tConfig for this.
    yours would have the settings
    TERM, DFT
    terminal1, 6600
    terminal2, 8800

    In the form entry use a combo box that pulls both values,
    in the AFTERUPDATE event, the miles would be set from the 2nd value in the comboBox.
    txtMiles = cboTerm.column(1)

    (column counts start at 0)

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I don't believe you can use formulas in the Default property of a Table field.
    If you wanted to do this, you would have to control all data entry via a Form, and have the Form use the formula to populate the field with the correct value as the record is added. Or if you were importing the data, you might need to run an Update Query afterwards that updates the value according to the value.

    However, I question whether any of this is necessary. If the value is calculated based on values in other fields, why store it all? A general rule of thumb is that anything that can be calculated from other values should NOT be stored as a table field. There is no need to - if it can be calculated at any time!

  4. #4
    cptdave1996 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    16
    It is not a calculation, it will be a set standard so that we can see if they drove the required miles.
    Thanks for the input...

  5. #5
    cptdave1996 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    16
    ok, thanks ranman256. might be a little over my head but I will give it a shot.
    Thanks again!

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It is not a calculation, it will be a set standard so that we can see if they drove the required miles.
    Are you sure? Where is this terminal information stored?

    What you are describing actually is a simple calculation and not a "default" (a "default", by definition, does not vary).
    The calculation would look something like:
    Code:
    =IIF([Terminal]="Terminal1",6600,8800)
    or
    Code:
    =IIF([Terminal]="Terminal1",6600,IIF([Terminal]="Terminal2",8800,""))

  7. #7
    cptdave1996 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    16
    ok, i see what you are saying. The terminal is stored in a table called ModEmployeeList and Driver Performance.
    I think what i am looking for is your second example.
    Will give it a try later today. I am really busy now and can't get to it...
    thanks!

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Another approach to take is to create a separate table, like Ranman suggested. For the same reasons I described, you wouldn't really need to use an AfterUpdate event to update a field, since you really probably do not want to store the calculation. However, then instead of having a calculation, you simply link to this table to get the value.

    The advantage to doing it that way is if you ever need to change those values down the road, you are simply updating a few table records, instead of hunting through calculated fields on queries, forms, reports, etc to see where you used it. So it makes it a little easier to maintain, which is great, especially if you want to let a non-technical person maintain it.

  9. #9
    cptdave1996 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    16
    I was thinking about that but i wasn't really sure how to put it in my queries and report.
    again, I will work on it this weekend with these suggestions...

  10. #10
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    In your query, simply add this new table, and join it to the other table on the "Terminal" field, and then return the value.

  11. #11
    cptdave1996 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    16
    Great thanks!

  12. #12
    cptdave1996 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    16
    Quote Originally Posted by JoeM View Post
    In your query, simply add this new table, and join it to the other table on the "Terminal" field, and then return the value.
    Ok I added a new table named it Terminals and have two fields, one for each term and in the fields, I put 8800 for one term and then 6600 for the other.
    I am not sure how to link the tables though.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    So you save the terminal identifier in Driver Performance table. The associated miles can be displayed on form by several methods.

    1. combobox to select terminal has a column for the miles field, the miles can be viewed in the dropdown, also an expression in textbox can reference the column by index, index begins with 0: =[cbxTerminal].[Column](1) or the combobox RowSource can concatentate the fields so both values will show in the box and a separate textbox is not needed:

    SELECT TerminalID, TerminalName & " : " & Miles As TermMiles FROM Terminals;

    2. include Terminals table in the form RecordSource by OUTER joining on the PK/FK fields, bind textbox to the miles field but don't allow edits, set Locked Yes.

    3. DLookup()


    Advise not to use spaces and special characters/punctuation (underscore is exception) in naming convention. Better would be Truck_Num or TruckNum.
    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.

  14. #14
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I am not sure how to link the tables though.
    Access is a relational database program (the "relational" being how tables are related to each other). Creating queries between related objects is one of the most powerful reasons to use Access, and is a task should be in every Access users skill set.

    If you Google "Access create query between two tables", you will find many, many links and videos on the topic (I like videos myself, as I tend to be a visual learner). Any beginner book in Access will cover this topic also.

    Here is one link of a video that shows how to do it to get you started: https://www.youtube.com/watch?v=yqabnPqyNrk
    There are many others out there.

  15. #15
    cptdave1996 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    16
    Quote Originally Posted by June7 View Post
    So you save the terminal identifier in Driver Performance table. The associated miles can be displayed on form by several methods.

    1. combobox to select terminal has a column for the miles field, the miles can be viewed in the dropdown, also an expression in textbox can reference the column by index, index begins with 0: =[cbxTerminal].[Column](1) or the combobox RowSource can concatentate the fields so both values will show in the box and a separate textbox is not needed:

    SELECT TerminalID, TerminalName & " : " & Miles As TermMiles FROM Terminals;

    2. include Terminals table in the form RecordSource by OUTER joining on the PK/FK fields, bind textbox to the miles field but don't allow edits, set Locked Yes.

    3. DLookup()


    Advise not to use spaces and special characters/punctuation (underscore is exception) in naming convention. Better would be Truck_Num or TruckNum.
    Hi June7,

    Thanks.
    I am going to change my form so that I can just either put in 8800 or 6600 for the goal miles. I had someone help me build this and not sure how he got the queries to work for my report so I don't want to mess that up.

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

Similar Threads

  1. Default Database Setting
    By Perceptus in forum Access
    Replies: 3
    Last Post: 06-30-2015, 02:11 PM
  2. Setting default
    By Thompyt in forum Programming
    Replies: 10
    Last Post: 11-10-2014, 11:49 AM
  3. Setting default value to zero in form box?
    By dekhelia in forum Forms
    Replies: 6
    Last Post: 10-17-2013, 08:53 AM
  4. Automaticly Setting Default Value
    By swicklund in forum Forms
    Replies: 5
    Last Post: 06-17-2013, 08:08 AM
  5. Setting default vaulue?
    By djclntn in forum Forms
    Replies: 17
    Last Post: 04-12-2012, 11:20 PM

Tags for this Thread

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