I.T. Discussion Community!
-Collapse +Expand
Paradox
Search Paradox Group:

Advanced
-Collapse +Expand Paradox To/From
To/FromCODEGuides
-Collapse +Expand Paradox Store
PRESTWOODSTORE

Prestwood eMagazine

August Edition
Subscribe now! It's Free!
Enter your email:

   ► KBDesktop Data...Paradox & Ob...ObjectPAL Co...OPAL: Commands   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Paradox OPAL: Commands:
Cliff's Corner: Improving the Code
 
Posted 16 years ago on 3/31/2003 and updated 8/5/2009
Take Away:

Cliff's Corner by Cliff Suttle: Feb/Mar/Apr edition.

 Tags: Paradox , ObjectPAL , cliff , suttle

KB100225

[Updated 2/23/2008, code formatting fixed]  

Back to the Invoice

Hi again. Sorry I missed the last issue. I've been a little busy lately. But in the words of General MacArthur, "I shall return." And so, here I am. Today we are going to be revamping the invoice system that we have been creating together in the last three Cliff's Corner articles. If you didn't get a chance to read them, you may wish to do so now. Otherwise, this article alone, probably won't make much sense.

In the last article I talked about how the look up forms (Customer.fsl, Items.fsl, and PO.fsl) could be improved with a few changes to the code. Lets make these changes, because they will really help the performance of our system. At this point, please download the new files DOC100225.ZIP. These files were written in Paradox 10, but should work in 9 or even 8 without any problems (Although I didn't get a chance to test the form in the lower versions, it should work.). All the example forms from this article are in the zip file.

First, I added a "Cancel" button to each of the three look up forms. These buttons were easy to add, since they only have one line of code in them. The "Push Button" method of the cancel button contains the line:

formReturn(false)

We are also going to change a line of code in the "Done Button" to:

formReturn(true)

This will tell the calling form (invoice.fsl) if the user wants to update the invoice data or not.

Next, we will update the invoice form code, in the "Keyphysical" method at the form level. Our new code will look like this:

method keyPhysical(var eventInfo KeyEvent) 
var
  fm form ; declare a form variable to call form 
endVar
if eventInfo.isPreFilter() then  ;// This code executes for each object on the form
 if eventInfo.vChar() = "VK_F5" then ; was F5 Pressed by the user   DisableDefault ; prevent F5 from doing anything else
  switch
  case active.fieldName = "Customer Number": ; is the cursor on the Customer Number
    fm.open("Customer") ; call customer form
    fm.customer_number.edit()
   if not customer_number.isBlank() then
     ; locate number in the table is data exists
     if not fm.customer_number.locate("Customer Number",customer_number) then
      fm.customer_number.insertRecord()
      fm.customer_number = customer_number
     endIf
    endIf
  case active.fieldName = "Purchase Order Number":
 ; is the cursor on the PO Number
    fm.open("PO") ; call PO form
    fm.purchase_order_number.edit()
   if not purchase_order_number.isBlank() then     ; locate number in the table is data exists
     if not fm.purchase_order_number.locate("Purchase Order Number",  purchase_order_number) then
      fm.purchase_order_number.insertRecord()
      fm.purcahse_order_number = purchase_order_number
     endIf
    endIf
  case active.fieldName = "Item Number": ; is the cusor on the Item Number
    fm.open("Items") ; call items form
    fm.item_number.edit()
   if not item_number.isBlank() then     ; locate number in the table is data exists
     if not fm.item_number.locate("Item Number",item_number) then
      fm.item_number.insertRecord()
      fm.item_number = item_number
     endIf
    endIf
  otherwise:
    beep() ; sound error alarm and give user message
    msgStop("Warning","F5 not allowed on this field.")
    return
   endSwitch
  if fm.wait() then ; wait for use to complete form
    ; a True value has been returned
    ; we need to add the data to the field
    ; check to see that the form is in edit mode
     if not isEdit() then
     edit()
    endIf
    switch
    case active.fieldName = "Customer Number": ; is the cursor on the Customer #
     customer_number = fm.customer_number
    case active.fieldName = "Purchase Order Number": ; is the cursor on the PO #
     purchase_order_number = fm.purchase_order_number
    case active.fieldName = "Item Number": ; is the cusor on the Item #
     item_number = fm.item_number
    endSwitch
   endIf
  fm.close() ; shut the form down  endIf  else  ;// This code executes only for the form
endIf
endMethod

I know at first this seems like a lot of code, but it really isn't as intimidated as it looks. Let's break down the code in order to see what I did. First off, this is a great time to discuss identifiers. You will probably notice, for instance, that "Custom Number" appears in many places and in several different formats. The different formats identify different "Holders" of data in Paradox. There are three separate uses of Customer Number in this code, they are:

Customer_Number - this points to the value of Customer Number on the Invoice.fsl form (or default form) attached to the Customer_Number field object. Therefore, since the object attaches to the data field in the table Invoice.db table, we are affecting the actual invoice data. This reference is to the object name on the form. Objects can have any name you give them, but by default, Paradox assigns the name of the object to the field name with the blanks replaced by underscores.

Fm.Customer_Number - once we open up the form, Customer.fsl using the FM form variable, we can refer to objects on that form with syntax "FM.object_name". Hence this use of Customer Number accesses the Customer Number on the Customer.fsl look up form. Thereby, we are accessing the data in the Customer.db table with this format.

"Customer Number" - the field name in quotes. This is the actual field name and is used in the Locate command in order to give the ACTUAL name of the field in the table. Note that blanks are allowed in this context.

Armed with this knowledge, the code now makes a lot more sense. After we identify which field is being accessed with code like:

case active.fieldName = "Customer Number":

Then we open up the appropriate form:

fm.open("Customer")

Place the form in edit mode:

fm.edit()

And, if there is already data in the Invoice.fsl field object, locate the current Customer Number in the Customer.DB table with the code:

fm.customer_number.locate("Customer Number",customer_number)

Notice, that this code is placed in an "IF" statement, that checks to see if the data was "NOT" found. If the customer number does not exist, a new record is inserted and the Customer Number from the Invoice form is automatically filed in with the code:

fm.customer_number.insertRecord() fm.customer_number = customer.number

This sequence is then repeated for the other two look up fields.

Lastly, if the user clicks on the done button (returning a true value), I place the data from the look up form into the invoice form with the code:

customer_number = fm.customer_number

Try and see if you can dissect this code yourself and really understand it.

Next, lets turn our attention to the Line Item table object on our form. Although the built in table object functionality is useful as is, it would be nice if the form calculated the Sale Tax and Item Total on it's own. With a little code, we can set this up.

In the "ChangeValue" method of the Item_Price field object in our table object, we are going to add the following code:

method changeValue(var eventInfo ValueEvent)
  ; check to see that all information to do the calculation has been filled in
  if not item_price.isBlank() and   not item_quantity.isBlank() then
  ; calculate sales tax
   sales_tax = (item_price * item_quantity) * 0.06 ; or whatever your state rate is
  ; calculate total
   item_total = (item_price * item_quantity) + sales_tax
  endIf
endMethod

This code was copied to the ChangeValue method of the Item_Quantity object and our line calculation system is done. The comments in the code pretty much tell you everything you need to know about this method.

Lastly, I added a field object to the bottom of the form and defined it as:

Sum(Invoice Item.Item Total)

Which will automatically total all the line items and give us a grand total. The final form now looks like this:

We now have a pretty good working invoice form. Of course, we could always make it better. But at this point, I'm afraid that maybe I'm beating a dead horse. So, I need your help. What would you like to see me talk about next time? Do you want another Cliff's Corner improving this invoice form more, or would you like a different topic? If you want a new topic, give me your ideas and I'll see what I can do.

Thanks for reading. See you next issue.

Cliff


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1181A1
Enter key:
Article Contributed By CliffSuttle:

Mr. Suttle is the President of Antler Software Technologies and is one of the leading authorities on relational data bases in the mid-west. He has been working with Paradox since version 1.0 for DOS (circa 1987).

Visit Profile

 KB Article #100225 Counter
10756
Since 4/2/2008
Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech