Macros - during data entry and retrieval
Macros which are saved as 'Same Process', then played by means of F-Keys or <Command-Shift-m>/ can be used to, for example, evaluate and change the data of the current record(s) on screen. All field values can be used, or fields can be altered to contain new values.
For example, a macro called by these means at the Orders entry screen could say:
If ([ORDER ITEMS]Product Code#"")
SEARCH ([STOCK LEVELS];[STOCK LEVELS]Stock Type="P";*)
SEARCH ([STOCK LEVELS];[STOCK LEVELS]Product Code=[ORDER ITEMS]Product Code)
Alert ("The Free Stock Level is: "+String ([STOCK LEVELS]Free Level))
End if
From an Orders listing screen, an appropriate macro might be:
If (Records in set ("Userset")>0) `UserSet contains the highlighted items
USE SET ("Userset")
End if
SORT SELECTION ([ORDERS]Total Amount;>)
Note that the macro palette's [Play] button cannot be used to start ‘Same Process’ macros in this situation.
Note also that ‘Same Process’ macros can make use of the recorded options such as Key to place text into fields in an input layout, but that the keying will not be visible until the macro has stopped. This is because the macro has effectively taken over the processing of the window involved. It is more logical to directly change the values of fields, using the := operator.
The following macro relies on the Company Find Default to perform an instant credit check on the last Company searched for. Because it relies on the Find Default (ie the Company last searched for), it can be used at any time (even when you have no processes open) and is therefore attached to a Function Key:
`Credit Check macro
`First, search for Invoices made out to the Find Default Company which have money outstanding
SEARCH ([INVOICES];[INVOICES]Company Code= ◊CompCode;*)
`◊CompCode is the Find Default
` The * parameter means wait for a second part of the search
SEARCH ([INVOICES];&;[INVOICES]Total Due#0)
SEARCH ([COMPANIES];[COMPANIES]company code= ◊CompCode)
`Perform this search to display Company Name
mCompName:=[COMPANIES]Company Name
vAlert1:=(mCompName+char(13)+char(13)+"There are "+string(Records In
Selection([INVOICES]))+" due invoices.")
mSum:=Sum([Invoices]Total Due)
Alert (valert1+char(13)+"Total Value= "+String(mSum))
`The Total Due must be converted to a String or the Alert command will result in an error
Macros - before, during and after data entry
It is possible to force a macro to be run on saving a record by clicking [√] or pressing <Enter>. Such a macro is termed a ‘Data Entry macro’.
When a record is saved, Enterprise 6 will search for a macro with a specific macro Code, and will run it if found. That Code should consist of the word ‘Accept ’ (including trailing space) followed by the File Number of the File to which the record belongs. For example, such a macro for the Sales Orders File would have ‘Accept 24’ as its macro Code. Data Entry macros will be run if they exist and if the ‘Play Data Entry macros’ check box in the current user’s Personnel record is switched on.
You can also create macros that operate before creating a record, before viewing an existing record and after modifying individual fields. The macro Codes for these types of macro should have the following formats (in each case, x represents the File Number):
‘Enter x’ (for example, ‘Enter 2’ will run before a new, blank, Company record is shown on screen); ‘View x’, which will run before an existing record that you have double-clicked on is displayed on screen; and ‘Modified x/y’, where y refers to a specific field. For example, ‘Modified 25/2’ will run when the value in the Product Code field in an Order Item (ie the second field in the Order Items file) is changed by the user.
It is possible to have one such Data Entry macro for each file for each user. If you would like a Data Entry macro to be universal, leave the Person field blank.
When creating these macros, it is not necessary to know the File or Field Numbers. When the cursor is placed in the macro Code field on the macro entry screen, selecting the appropriate File from the File pop-up will cause the macro Code to be changed to ‘Accept x’ and the macro Name to ‘Run on Accepting a [x name] record’, where x is the File Number. The word ‘Accept’ can be altered to ‘Enter’ or ‘View’ as appropriate, although selecting from the Files pop-up while holding down the or <Alt> key will cause the word ‘Enter’ to be brought in automatically. Then, selecting from the Fields pop-up whilst the cursor is still in the macro Code field will alter the macro Code field to ‘Modified x/y’ and the macro Name to ‘Run when [x file]y field is Modified’.
From version 6.3.1 you can also create Load macros. These execute as soon as a record is opened. They can be set up in just the same way as Data Entry Macros.
Enter macros
Data Entry macros will be played as ‘Same Process’ macros so that they react to the values in the record that has been accepted.
If you do want to launch a new Process (for example, launching a new unrelated entry screen) then the Accept macro should contain a single line: Macro ("NewProcMacro")
All sorts of usages are possible, including exporting data to disk record by record, creating Diary Items that inform people of what is going on, or running various other macros according to the data values (their Codes could be based upon those values).
For example, you could write a macro which you would like to be run whenever you save a Sales Order for the first time. This would check that you have not forgotten to charge for carriage on Sales Orders worth less than a certain figure. Following the naming convention, such a macro must be named ‘Accept 24’, to ensure it is run when you click [√] to save an Sales Order.
Such a macro might read:
`Accept 24
`Accept button macro for Sales Orders file
`This must be a same-process macro
If (vAdd=1) `If you’ve added a record - vAdd is 0 if you are viewing `ie Only checks the 1st time an order is saved
If ([ORDERS]Total Amount<200) `Check if order under £200
FIRST RECORD([ORDER ITEMS])
while (not (end selection ([ORDER ITEMS])))`Loops through order items to see if carriage is already there
If ([ORDER ITEMS]Product Code= "NNCARRIAGE") mFlag:=1
Else
mFlag:=0
End If
NEXT RECORD([ORDER ITEMS])
end while
If (mFlag=0)
Confirm("This order is under £200"+char(13)+"Do you wish to add carriage?"; "Yes") `If not there do you wish to add
If (OK=1)`If you do create new order item
CREATE RECORD([ORDER ITEMS])
orderi No `Two Enterprise 6 procedures to attach new carriage Order Item to correct Order
orderi sublpb
[ORDER ITEMS]product code:="NNCARRIAGE"
check product(»[ORDER ITEMS]Product Code;"Product")
Orderi SubLPPC
PriceT Check
SAVE RECORD([ORDER ITEMS])
orders inlptot `Amends Order Totals
SAVE RECORD([ORDERS])
End if
End If
End if
End if
NOTE: If you are Playing a macro that accepts a record in a file for which an Accept macro has been set up, the former one will stop playing at this point - and will not restart.
A 4D function Str In List allows macros to be used as a storage mechanism for lists of data, which can then be the basis for decisions of how to proceed. For example, the following could be the ‘Accept 24’ macro:
If (vAdd=1)
If (Str In List([ORDERS]Company Code;"Those Companies"))
Alert ("You've entered an Order for one of those Companies")
End if
End if
The first If statement determines whether you’ve added a record or not. The second one checks whether the Company Code stored in the new Order exists in a list defined within a macro with the Code ‘Those Companies’. The list in the ‘Those Companies’ macro Text would in this case simply consist of Company Codes, separated by <Return>s. The listing macros will not themselves be playable - they would simply give errors.
Enter macros might be used to default values on an entry layout to settings that users most commonly require, thus saving time during entry.
For example, the macro ‘Enter 2’ (applying to the Companies file) might read:
[COMPANIES]Source:="Most Common"
If(◊User="RAN")
[COMPANIES]Comments:="This is mine!"
End if
View macros
View macros might perhaps be less frequently used, but an example might be where you want to ensure that whenever a record is viewed a value will be up to date.
For example ‘View 24’ might read:
[ORDERS]Comments:=[ORDERS]Comments+"Viewed by "+◊User+" on "+String(Current Date)+Char(13)
Modified macros
Modified macros can be used wherever, as a result of making a given field entry, you want to update the same or other field values - over and above any changes made by Enterprise 6 itself. Thus, entering the Product Code to a Sales Order will normally fill in the Cost and Sales Price, but ‘Modified 25/2’ might read:
If([ORDER ITEMS]Product Code="10@")
[ORDER ITEMS]Delivery Date:=Current Date
End if
Changing further fields by modified macro
Modified macros can be used to enter or change values of Further Fields. For example, you might like to store a Call Manager for each Service Call which automatically defaults to the Sales Person of the Company concerned. To do so, set up a Further Field in the Service Calls file to store the Call Manager information. Then create a ‘Same Process’ macro allocated to Service Calls with the macro Code ‘Modified 20/1’ (the Company Code field), that reads simply:
FurthFld Set("Service Calls";"Call Manager";[COMPANIES]Sales Person)
Since the macro is run immediately after modifying the Company Code field you can be sure that the Company record will be in memory and accessible and therefore the correct sales Person information will be brought in.
A more complex Modified macro using Further Fields can be used to create a simplified form of Price Table that brings in one of two different Sales and Cost prices according to a single criterion. In this example, the macro chooses between ‘Buy’ or ‘Exchange’ prices which are held in the Actual and Standard Price fields for each Product.
First, set up a Further Field for Orders called ‘Buy or Exchange’ in order to store the choice, and create a ‘Same Process’ macro allocated to Order Items with the macro Code ‘Modified 25/2’ (the Product Code), which reads:
If([ORDER ITEMS]Product Code#"")
If([PRODUCTS]Standard Price#0) mBorE:=FurthFld Get("Orders";"Buy or Exchange") `Find out if the Order is Buy or Exchange
If(mBorE="") `if it has not previously been specified
Confirm("Is this a Buy or Exchange Order?";"Buy";"Exchange")
If(OK=1)
mBorE:= "Buy"
Else
mBorE:= "Exchange"
End if
FurthFld Set("Orders";"Buy or Exchange";mBorE)
End if
If(mBorE="Exchange") `Only if it's Exchange do you need to change the figures
[ORDER ITEMS]Retail Price:= [PRODUCTS]Standard Price
[ORDER ITEMS]Sales Price:= [PRODUCTS]Standard Price
[ORDER ITEMS]Cost Price:= [PRODUCTS]SUPPLIERS'Standard Cost
Calc Item
[ORDER ITEMS]Product Name:= [ORDER ITEMS]Product Name+" (Exchange)" `optional
Else
[ORDER ITEMS]Product Name:= [ORDER ITEMS]Product Name+" (Buy)" `optional
End if
End if
End if
This macro could be made more powerful by making use of Further Fields attached to Products to store and retrieve multiple different prices for different purposes. The retrieving line could read something like:
[ORDER ITEMS]Retail Price:=Num(FurthFld Get("Products";"Sales Price 3"))
Activating modified macros from further fields
It is also possible to activate a Modified macro from a Further Field. Such macros should have a slightly different macro Code format to that described above, and any Further Fields which will be used to activate such macros should also have Field Names following a specific format. Such Field Names should contain a unique identifier as their first four characters.
For example, you might set up a Further Field with a Field Name of ‘0001 Date of Audit’ to facilitate the running of a Modified macro coded ‘Modified FF0001’ that will be run whenever you modify this Further Field.
However, you can write Modified macros that can be activated by more than one Further Field. For example you could replace ‘0001’ with ‘D1’ to represent ‘Date handling macro Style 1’. Further Fields activating such a macro might have as their Field Names might be ‘D1 Date of Audit’ or ‘D1 Started Date’.
When writing such Modified macros, you should always refer to the field being modified as ‘aALFFText{bd37}’, and its value is always a string (or text).
Consequently, to work with Numeric and Date Further Fields you need to use the Num and Date Functions to convert to the correct Type, and the String Function to convert back.
The following Modified macro could be the above ‘D1’ example, since it prevents a Date being entered that is earlier than the current date, and thus could be applied to any Further Field of Date Type:
mDate:=Date(aALFFText{bd37})
If(mDate0)
aALFFText{mFind}:= "Modification" `this is of course an Alpha or Text field in this case
End if
This places a numeric code referring to the Further Field to be modified in the mFind variable, which is then used to change the appropriate value in the aALFFName{} array. The text ‘Field Name to modify’ should be replaced by the Field Name of the Further Field to be modified by the macro.
It is not recommended to use expressions such as ‘aALFFText{bd37-1}’ to refer to Further Fields other than that containing the cursor. (This example refers to the Further Field immediately above the one that has been modified.) This is because the positions of each Further Field in relation to each other can be altered at any time in the Further Fields Definition screen, and some Further Fields may be omitted for some Users.
You can write values from Further Fields back to the normal fields of the record from which you accessed Further Fields, using the normal [FILE]Field references. Remember that doing so is likely to require the Modified macro to be unique to a particular Further Field.
Modified macros activated from Further Fields are run before the application of a Format (if applicable), but after the displaying of a List (if applicable).
Data entry macros - miscellaneous
The operation of Data Entry macros does not upset or interrupt the recording or playing of standard macros. Thus you could create a Modified macro attached to the Company Name field, then record a macro while you enter a Company Name. The Modified macro would work both while recording and while playing the recorded macro.
Modified macros cannot be applied to fields in subfiles. They therefore cannot be used for the Fields of Sales Invoice Items, Purchase Invoice Items, Purchase Order Items or General Ledger Transaction Items.
All Data Entry macros are loaded into Single-user or Client memory on startup in order to allow them to operate at a reasonable speed. If memory is tight, ensure that the number and length of these macros does not grow too great.
Note that the format of the macro Code for Data Entry macros described above was introduced in Modulus version 4.0.034. Previous versions used a different format. On upgrading, the macro Codes of all Data Entry macros will be updated automatically. The text ÔRun on Accepting a [filename] recordÕ will also be inserted in the macro Name field.
Published date: Fri, 09 Mar 2007 13:53:58 GMT
© 2006 - 2007 Daybook Limited, All trademarks accepted. All Rights Reserved!
Site Index