Coming from Excel to VFP9 and need help understanding math functions on cells

I have set up a simple data tracking form that breaks out the associated costs of a vehicle going to Auction ( dealer costs, transportation fees and so on) for my employer. It works great, and I can enter data into the database successfully, and my Report will print it out nicely sorted by Store. I would now like to create cells on my data entry form that calculate and display various math procedures on the data being entered. For instance, a cell that displays the Sum of all the fees and costs, and then a Result field that displays the amount we sold the car for minus the Sum total of the fees. Boy, do I need some help getting my mind around how to do this in VFP 9! Could someone please outline the general steps I need to do to make this happen It seems like I have to define variables, write expressions using them, and somehow plug them into the forms or reports... am I even close Any and all help sincerely appreciated! thanks!


Answer this question

Coming from Excel to VFP9 and need help understanding math functions on cells

  • atnoslen

    After you make a name for the method then RIGHT CLICK on the form and choose PROPERTIES. 

    Now click on the METHODS Tab

    Move all the way down to the bottom and the name of your Method should be there.

    Double click on the area to the right of the name, a code window will pop up.

    Enter the code into this area and Save it when you are done (control key + W) saves it.



  • Htr

    I feel like I’m making some progress in VFP, and have jumped in to some simple stabs at performing math functions on form fields and have them appear inside text boxes. Thanks to all who have replied, I figured out how to set up custom methods, Link the reporting text box via the control source, made custom properties and so on. I’m struggling a bit with syntax and the rather cryptic MS help files, but each step is a step forward. Here’s my very first custom method that in theory adds these two values together…< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

    WITH thisform
    .newprop2.value=.cost1.value + .cost21.value
    .refresh()
    ENDWITH

     … of course it doesn’t work yet, as newprop2 is apparently an ‘unknown member’ unless I click the assign and/or access boxes in the method and property editor. At that point I am informed newprop2 is not a function, procedure or program. Hmmmmm…. Okay. I bet Object Oriented Programming probably is likely to be a lot more fun once I figure this one out a bit more, right J I’ll keep plugging away… any pointers why the newprop2 property is failing would be wonderful. Thanks again  



  • Mr G

    I am in the same business.  This can be a difficult thing to master until you try a few times then a lightbulb will go on.  OOP is very cool when you get the hang of it.

    Here is a sample method you can use parts of to help you understand.

    This sample has a form that has fields on it with the following NAMES (in the properties window that is)
    PRICE
    TAX
    LICENSEFEES
    TOTALPRICE

    METHOD created called CalcTax is below as a sample (not complete but you get the idea I hope)

    **Method CalcTax

    WITH thisform
        .lnNoTradeTaxAmount.value=.Price.value * (.Tax.value/100)

        .lnTradeTaxAmount.value=((.Price.value - .saledisc.value) - .tradein.value)       *       (.Tax.value/100)
       
        .lnTotalPrice.value = .pricebeforetax.Value * .lnTradeTaxAmount.value + .LicenseFees.value + .DeliveryFees.value

       ** now refresh the controls on the form
        .refresh()
    ENDWITH


    I call this method from the VALID of each control that can change the total amount.  In the PRICE Valid I place this code:
    thisform.CalcTax()

    As soon as I press enter all the totals are updated for the new amounts.



  • soskman

    UPDATE: First, thank you both for replying. I think I'm making good headway... I was able to follow through the first three steps in Tamar's reply w/o problems. In step 4, I don't see where I should enter the custom code. I press FORM>NEW METHOD and the box pops open, and I can assign a name and a user friendly description; but that's it. Under FORM>EDIT PROPERTY/METHOD; I see the list of everything I added along with their descriptions but I don't see where to add the code. I suspect I'm missing something really obvious here... I looked under the MemberData editor and can see an area for User defined attributes; is this where I should be Thanks again :)

  • JulieAnne

    The first thing you need to do is try to get away from spreadsheet-type thinking. Among other things, there really is no concept of a "cell" in VFP.

    It sounds like what you want to do is put some textboxes on a form that will show some calculations as values change. As you suspected, this will involve writing some code. I recommend doing it like this:

    1) Add a custom method to your form with a name like Calculate. You do this by choosing Form | New Method from the menu, and then entering the name you want for it. Click Add to actually add the method.

    2) Add custom properties to your form to hold the values you want to calculate, one property for each value. To do so, choose Form | New Property and then type in the name for each, clicking Add to keep it.

    3) In the Property Sheet, set the initial value for your custom properties to something appropriate. (I suspect 0 is a good choice.)

    4) In your custom method, put the code that does the actual calculations, storing the results in your custom properties. Remember that to refer to a property, you need to specify the relevant object, so you'll have code like:

    This.nTotal = <whatever it takes to compute the total>

    If you tell us more about the set-up, we can help you work out the actual code.

    5) Now you have to hook the code up to things in the form. First, add a textbox for each computed value you want to show. For each, set its ControlSource property to one of your custom properties.

    6) The final step is getting the textboxes updated as things change. There are a couple of choices here. The simplest is to add a button (with a Caption like Calculate) and in the button's Click event method, call the custom method you set up:

        ThisForm.Calculate()
        ThisForm.Refresh()

    The downside to this approach is that the textboxes only get updated when you click the button. If you want to have them updated every time something changes, you have to do a little more work. What this is depends on a) what version of VFP you're using and b) how the form is set up, that is, what kinds of controls you're using. It's not clear to me whether you're trying to show totals for a number of records displayed in a grid or for a series of records being added and saved. Tell me more and I'll try to explain more.

    Hope this gets you started.

    Tamar

  • jojobar

    Thanks for the reply! I added the code and it's working nicely but for one remaining problem. When I run the executable, the textboxes that show the calculation results have to be manually clicked-into before they become active and display their content. Is there a way to have them automatically be active when the form first opens

  • BalajiB

    Thank you so much! I'll give it a shot and post back to let you know how I made out :)

  • Sajal

    Worked like a charm! Thank you very, very much!

  • Sundown Kid

    What is the tab order on the form  

    In the INIT of the form you should fire the function also, this will make sure the textbox will have the information filled in when it finally shows.


  • Michael Nebel

    Okay, now I did it... I sucessfully added all the computational areas on to the form and they all wok very nicely. I rebuilt the app without any errors. I generated the exe file and it also compiled without errors. The problem is that now when I click on the new exe file, the hourglass comes up momentarily, then disappears. This exe is running from inside the same PC that I'm working on, and older exe's still work. This makes me think I somehow turned off something that makes the exe open the windows and begin execution. Any ideas Thanks!
    :)



  • Robert_Jr

    If NewProp2 is a property you added, then it's a scalar value, not an object (unless you assign it an object). So you refer to it as just NewProp2, not NewProp2.Value.

    Tamar

  • rjr

    No, you did not turn anything off, just neglected to place a very important command: READ EVENTS.

    VFP needs a wait state to wait for user input. During development the IDE itself does it (stays 'alive' waiting for you to do something). In an EXE, you do not use the IDE but the runtimes. They need a wait state.

    Do it like this:
    Create a main program (Main.Prg) that instantiates your main menu, main form (DO Form xxx), etc., then issue a READ EVENTS statement.

    The form is displayed and stays awaiting user input.

    To end, place a CLEAR EVENTS in the Exit button or Exit Option of you main menu. This would bring the program out of the event loop back into Main.Prg at the line immediately after READ EVENTS. That is where you do the final cleanup before QUIT.

    See also the help file for:
    ON SHUTDOWN


  • Coming from Excel to VFP9 and need help understanding math functions on cells