Results 1 to 3 of 3

Thread: Access Forms: Calculated field updates after each entry. HELP!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Posts
    3

    Question Access Forms: Calculated field updates after each entry. HELP!!

    I am probably so annoyed that it must be easy, but I just can't figure it out!! I am trying to create a data entry form and want a field in there to automatically calculate the total of what is entered in the 13 fields that would have data entry in them. Sounds like a reasonably easy thing to be able to do, but I don't know why it isn't?? I just spent $90 on books for Access, and VB6, and neither of them can help me. I have the fields based on the table, as it did not work with the query. When I based the fields on the query, I could not add more records, but my total field in the query did work. I put in a text box field and had the expression to equal the query, but it displayed &name. I assume VB code would be the way to go, if someone could walk me through this I would be soo sooo happy!!!

    THE GOAL: To have a total field that automatically updates after each entry of data on the form.

  2. #2
    New Member
    Join Date
    Feb 2002
    Posts
    9
    Do you want the the total to change automatically when you change one of the fields? Like when you change an entry on a spreadsheet? Or do you want to click on a button and have the total update based upon the values of the fields.

    Let me know how you want to handle it and I will supply some sample code for you.

  3. #3
    New Member
    Join Date
    Feb 2002
    Posts
    9
    Create a form with one text box and one label on it
    set the textbox name to txtEntries and set the label name to
    lblEntries
    Put another lablel down in the righgt hand corner of the form
    and name it lblTotal

    Set the Index property of the textbox to 0
    and set the Index property of the label to 0
    Paste the code below into your form.

    This is what it sounds like you are looking for. Let me know

    VB Code:
    1. Option Explicit
    2. Private cTotal As Currency 'Used to hold total
    3.  
    4. Private Sub Form_Load()
    5. Dim I As Integer
    6.  
    7. lblEntries(0).Caption = "Entry Number 1" 'Name the first Label
    8. '**************Set up a Loop to create control arrrays********
    9. For I = 1 To 12
    10. Load txtEntries(I) 'Sets up a control array of 12 more Text Boxes
    11. Load lblEntries(I) 'Sets up a control array of 12 more labels
    12.  
    13. '*************Line Everything up under each under*****************
    14. txtEntries(I).Top = txtEntries(I - 1).Height + txtEntries(I - 1).Top
    15. lblEntries(I).Top = lblEntries(I - 1).Height + lblEntries(I - 1).Top
    16. txtEntries(I).Left = txtEntries(I - 1).Left
    17. lblEntries(I).Left = lblEntries(I - 1).Left
    18. txtEntries(I).Visible = True
    19. lblEntries(I).Visible = True
    20. lblEntries(I).Caption = "Entry Number " & I+1 'Changes Label Caption
    21. Next
    22.  
    23. End Sub
    24.  
    25. Private Sub txtEntries_GotFocus(Index As Integer)
    26. txtEntries(Index).SelStart = 0
    27. txtEntries(Index).SelLength = Len(txtEntries(Index))
    28. End Sub
    29.  
    30. Private Sub txtEntries_Validate(Index As Integer, Cancel As Boolean)
    31. Dim I As Integer
    32. cTotal = 0 'Reset Total
    33. For I = 0 To 12 'Loop through 13 text boxes
    34.   'If text box is empty then assign 0 to it and format it
    35.   If txtEntries(I) = "" Then
    36.   txtEntries(I) = 0
    37.   txtEntries(I) = Format(txtEntries(I), "Currency")
    38.   Else
    39.   txtEntries(I) = Format(txtEntries(I), "Currency")
    40.  
    41.  
    42.   End If
    43. Next
    44. For I = 0 To 12
    45. cTotal = cTotal + CCur(txtEntries(I)) 'Calculate Total
    46. lblTotal.Caption = Format(cTotal, "Currency") 'Place Total in Label
    47. Next
    48. End Sub
    Last edited by mgiambrone; Apr 5th, 2003 at 12:12 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
  •  



Click Here to Expand Forum to Full Width