Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jul 2013
    Posts
    36

    Populate a Form Field With Calculation if ID Does Not Exist In Table

    I am using 1 form for data entry and for record lookup. The source table of this form is the master table, so if a user inputs an id that exists in the master table it will fill in all fields on the form that exist in the master table. IF a user inputs an id that does not exist in the master table, then I need to use a calculation to populate the value for two fields.



    For example, if id 5 does not exist in the master table then
    Me!Total = SUM(price1+price2+price3+price4)

    if id 5 does exist in the master table then
    Me!Total = whatever the value in the table is

    Second field if id 5 does not exist in the master table then
    Me!Tax = Me!Total*.025

    if id 5 does exist in the master table then
    Me!Total = whatever the value in the table is

    How can I set this up on my form fields?

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,469
    Is ID your key field in the table? Can you give example of table structure and fields and sample data.

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    If 5 does not exist in the master table, how can you sum fields in a non-existing record?

  4. #4
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by davegri View Post
    If 5 does not exist in the master table, how can you sum fields in a non-existing record?
    If 5 does not already exist in the master table then the data has not already been input, meaning the user would be inputting the data now (which is why I would want to perform the calculations)

    Essentially a way for the form to check is this a new record being added to the table or is this an existing record that is already in the table?

  5. #5
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by Bulzie View Post
    Is ID your key field in the table? Can you give example of table structure and fields and sample data.
    ID is a field in my table but the primary key of the table is masterID.

  6. #6
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,469
    You could do the calculations after they enter values in each price field if you need to see it immediately, or in the BeforeUpdate Event on the form.

    Me!Total = SUM(nz(price1,0)+(nz(price2,0)+(nz(price3,0)+(nz(p rice4,0)

  7. #7
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by Bulzie View Post
    You could do the calculations after they enter values in each price field if you need to see it immediately, or in the BeforeUpdate Event on the form.

    Me!Total = SUM(nz(price1,0)+(nz(price2,0)+(nz(price3,0)+(nz(p rice4,0)
    The field(s) is a calculated field in the table since initially this process was only for review not for entry. If I set the AfterUpdate() Event for each of the price fields to Sum in the Total field would that interfere/throw any errors with the field being a calculated field?

  8. #8
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    Code:
    Me!Total = SUM(nz(price1,0)+(nz(price2,0)+(nz(price3,0)+(nz(price4,0)
    You don't want or need SUM, just

    Code:
    Me!Total = nz(price1,0)+nz(price2,0)+nz(price3,0)+nz(price4,0)

  9. #9
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by davegri View Post
    Code:
    Me!Total = SUM(nz(price1,0)+(nz(price2,0)+(nz(price3,0)+(nz(price4,0)
    You don't want or need SUM, just

    Code:
    Me!Total = nz(price1,0)+nz(price2,0)+nz(price3,0)+nz(price4,0)
    Will this cause any hiccups since the field Total is a calculated field in my table?

  10. #10
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,469
    Not sure i what you mean by calculated field. Do you mean they have a Default Value calculation on them in the table? The fields will get recalculated and placed into the record if ID is null.

  11. #11
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by Bulzie View Post
    Not sure i what you mean by calculated field. Do you mean they have a Default Value calculation on them in the table? The fields will get recalculated and placed into the record if ID is null.
    Ah, recalculating makes sense. So I would set the default value of Total to = nz(price1,0)+nz(price2,0)+nz(price3,0)+nz(price4,0 )

    Inserting the formula gives me an error
    Code:
    #Size!
    Could it be due to the fact that the fields I want to calculate on are empty? IE a blank form?

  12. #12
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,469
    No on Default Value, you will not have any data when the record is first created. If you want to show those Total and Tax on the form, then put the Total and Tax calculations in the AfterUpdate of each price field. Then when you save the record Total and Tax will be populated. You could check for the ID also:

    If isnull(Me.ID) Then
    me.Total = nz(price1,0)+nz(price2,0)+nz(price3,0)+nz(price4,0 )
    me.Tax = Me.Total *.025
    End IF

  13. #13
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by Bulzie View Post
    No on Default Value, you will not have any data when the record is first created. If you want to show those Total and Tax on the form, then put the Total and Tax calculations in the AfterUpdate of each price field. Then when you save the record Total and Tax will be populated. You could check for the ID also:

    If isnull(Me.ID) Then
    me.Total = nz(price1,0)+nz(price2,0)+nz(price3,0)+nz(price4,0 )
    me.Tax = Me.Total *.025
    End IF
    This is also giving me a #Size! error on the form

    The field is set to currency and 2 decimal places. Should the formula be formatted the same?

  14. #14
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,469
    So DataType on Tax field is "Number"? What is the Field Size? Should be Double I believe. Is your BE database (tables) SQL or Access?

  15. #15
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by Bulzie View Post
    So DataType on Tax field is "Number"? What is the Field Size? Should be Double I believe. Is your BE database (tables) SQL or Access?
    I caught it. The issue was one of fields had a space in it, and I did not add brackets to it was split into two fields. SO instead of my formula reading as
    Code:
    Me.Tax = NZ(price1,0)+NZ([sale markup],0)
    it was altered to
    Code:
    Me.Tax = NZ(price1,0)+NZ([sale] [markup],0)

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 07-02-2015, 10:49 AM
  2. Replies: 2
    Last Post: 06-17-2015, 09:08 PM
  3. Replies: 9
    Last Post: 02-02-2015, 01:03 PM
  4. Replies: 1
    Last Post: 08-01-2013, 12:23 AM
  5. Calculation Field in table vs Query calculation
    By Leonidsg in forum Database Design
    Replies: 18
    Last Post: 02-28-2013, 09:38 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