Consulting

Results 1 to 17 of 17

Thread: How to automate calculations using vba

  1. #1
    VBAX Mentor
    Joined
    Aug 2010
    Posts
    457
    Location

    How to automate calculations using vba

    I'm trying to add two random numbers together on a form in ms access and with the click of a calculate button, the result will appear in a blank field. But when I click on the calculate button there is no result in the empty label field. This is the code I wrote.(Below) Any suggestions as to why the result isn't showing on the blank label box(field)? Is my code correct? Any solutions to resolve this would be more than welcome.


    Private Sub Command1_Click()

    'To add the values in text box 1 and text box 2

    Sum = Val(Text1.Text) + Val(Text2.Text)

    'To display the answer on label 1

    Label101.Caption = Sum

    End Sub

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    I would use an Unbound text box rather than a Label with something like this
    [VBA]
    Private Sub Command1_Click()

    'To add the values in text box 1 and text box 2 & To display the answer in the new unboundtextbox

    unboundtextboxname = Val(Text1) + Val(Text2)


    End Sub[/VBA]

    Where "unboundtextboxname" is the actual name of your text box.

  3. #3
    VBAX Mentor
    Joined
    Aug 2010
    Posts
    457
    Location
    Thank you!

  4. #4
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Actually I wouldn't have a command button - use the AfterUpdate event for the text boxes.


    [vba]
    Private Sub Text1_AfterUpdate()
    'To add the values in text box 1 and text box 2 & To display the answer in the new unboundtextbox
    unboundtextboxname = Val(nz(Text1, 0)) + Val(nz(Text2, 0))
    End Sub

    Private Sub Text2_AfterUpdate()
    'To add the values in text box 1 and text box 2 & To display the answer in the new unboundtextbox
    unboundtextboxname = Val(nz(Text1, 0)) + Val(nz(Text2, 0))
    End Sub


    [/vba]

    We are what we repeatedly do. Excellence, therefore, is not an act but a habit.
    Aristotle

  5. #5
    VBAX Mentor
    Joined
    Aug 2010
    Posts
    457
    Location
    I added the code to the cost tectbox field, discount textbox field and Total textbox field and the following error messgae popped up - 'This error occurs when an event has failed to run because Microsoft Office Access cannot evaluate the location of the logic for the event. For example, if the OnOpen property of a form is set to =[Field], this error occurs because Access expects a macro or event name to run when the event is fired'.


    Have I applied the code to the correct textboxes? Or should I apply the code to the total text box? I'm sure it's logical

    Thanks

  6. #6
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Can you post your code?

    We are what we repeatedly do. Excellence, therefore, is not an act but a habit.
    Aristotle

  7. #7
    VBAX Mentor
    Joined
    Aug 2010
    Posts
    457
    Location
    Here is the code I wrote:

    Private Sub Cost_AfterUpdate()

    unboundtextboxname = Val(Nz(CostField, 0)) + Val(Nz(Discount, 0))
    End Sub

  8. #8
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    So is the field called "Cost" or "CostField"?

    We are what we repeatedly do. Excellence, therefore, is not an act but a habit.
    Aristotle

  9. #9
    VBAX Mentor
    Joined
    Aug 2010
    Posts
    457
    Location
    Cost Field

  10. #10
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    You have to check that your code is using the correct field names, and I'm not sure from your response which is correct.

    [vba]
    Private Sub Cost_AfterUpdate()
    unboundtextboxname = Val(Nz(CostField, 0)) + Val(Nz(Discount, 0))
    End Sub
    [/vba]

    Which is right? And is there a control named "unboundtextboxname"?

    We are what we repeatedly do. Excellence, therefore, is not an act but a habit.
    Aristotle

  11. #11
    VBAX Mentor
    Joined
    Aug 2010
    Posts
    457
    Location
    Thanks, I cracked it! I used an expression with a formula...many thanks!

  12. #12
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399
    If i could make 1 minor adjustment.

    I have in the past run in to problems using code along the lines of

    [vba]Private Sub Cost_AfterUpdate()
    unboundtextboxname = Val(Nz(CostField, 0)) + Val(Nz(Discount, 0))
    End Sub [/vba]
    I now ensure that i use the me. identifier for each reference of a control on a form. This eliminated a corruption problem and cross reference issue that had been causing me severe issues for a couple weeks so i would suggest using

    [vba]Private Sub Cost_AfterUpdate()
    me.unboundtextboxname = Val(Nz(me.CostField, 0)) + Val(Nz(me.Discount, 0))
    End Sub
    [/vba]
    Just my 2 cents
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

  13. #13
    VBAX Mentor
    Joined
    Aug 2010
    Posts
    457
    Location
    I applied your theory to my total field and this messge: (#Name) appears on the total field what does this mean?


    Thanks - I'm learning a lot about performing calculations in ms access...

  14. #14
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location
    #Name error means that it has been bound to a control or formula that cannot be referenced. Either you have misspelled the name of the control or the control source doesn't exist. If the control is bound to a calculation then the calculation might have an error in it.
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  15. #15
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399
    Also in some situations I have had #name appear if the text box is not wide enough to show the whole text (For a date or similar).

    To calculate the field total are you using a VBA Procedure or a "Calculation" directly in the source field ?

    My tip was designed only for VBA code not for a script.
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

  16. #16
    VBAX Mentor
    Joined
    Aug 2010
    Posts
    457
    Location
    I'm using an expression written in a source field.

  17. #17
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399
    Then you do not need the me. clarification i only mentioned it because the examples given were for vba code in the afterupdate sub for the field. Not for an expression in the source field.

    So the me. is probably causing the problem and giving you the #name error

    Quote Originally Posted by geekgirlau
    You have to check that your code is using the correct field names, and I'm not sure from your response which is correct.

    [vba]
    Private Sub Cost_AfterUpdate()
    unboundtextboxname = Val(Nz(CostField, 0)) + Val(Nz(Discount, 0))
    End Sub
    [/vba]
    Which is right? And is there a control named "unboundtextboxname"?
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

Posting Permissions

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