In the past write-up (Aspect one), we talked over how to encapsulate facts access code and small business regulations in Visible FoxPro by applying lessons contained in Visible Course Libraries (.vcx) data files. Nevertheless, most Visible FoxPro packages, will need to present a signifies of facts capture, retrieval and navigation, which is why a great deal of programmers present facts navigation buttons (up coming, past, past and very first buttons). This is one particular of the good reasons why Visible FoxPro programmers have generally just extra the needed tables to the facts atmosphere of the kind or report as a result, building the facts natively offered to the kind. If Info Entry and Validate code is encapsulated in lessons, how can facts be returned to a kind and how can facts-sure varieties be designed? This write-up seeks to exhibit that you can build powerful, powerful, powerful, adaptable databases programs even however your facts access code is encapsulated in ‘data mindful classes’. This write-up explores the use of arrays as facts transports and Cursors as short-term storage destinations for varieties.
In part I of this write-up, it was demonstrated that encapsulating facts access code and small business regulations in lessons designed the code simply obtainable from various modules in your software and obviated the will need to compose the identical facts access code regularly in various software varieties in your software. Also, interaction and facts exchange in between the kind and the lessons was by signifies of houses (users) of the class. If your software kind (person interface) necessary to save a history made by the person, the houses (variables or users) of the class ended up up-to-date with values typed into controls on the kind and then the Preserve strategy of the class was named.
In the same way, when a person necessary to exhibit an existing history, the EmpID home of the class was up-to-date and the Discover strategy of the class was named to lookup for the history. When the history was found, the houses of the class (i.e. the object made from the class) would be up-to-date with the values of the history located so that the kind could then exhibit the value of each and every home in the corresponding kind controls (e.g. text packing containers).
Whilst this strategy commonly is effective and may well be powerful for operate with single records in a modest system, what takes place when you will need to return more than one particular history at a time this sort of as in the scenario explained before? Could there be an choice way to pass facts to a class or return facts from it?
What is wrong with just applying houses?
To begin with, permit us just take into consideration the drawbacks of just applying houses only as explained! Whilst performing in that way is simple and commonly is effective, it has many drawbacks this sort of as the adhering to: one) Your capacity to process more than one particular history at a time is severely curtailed since the home variables can maintain facts for only one particular history at a time. This signifies that if you wanted for case in point to populate a record box on your kind, an choice system would definitely be suitable considering the fact that you have not extra any tables to your form’s facts atmosphere (but have delegated all facts access and retrieval code to your facts mindful class), 2) If your software ended up running in a networked atmosphere, each and every home you set or retrieve would induce a round-vacation on the community (particularly if your lessons ended up compiled as automation elements and hosted remotely on a remote equipment).
How much improved it would be to be ready to pass all records to be saved at one particular time or return all records that you will need to use at one particular time! Not only does this remedy the couple of challenges itemized higher than, it diminished community load and increases facts availability…but how?
Combining the electric power of Non permanent Storage with powerful Info Transports
Arrays are facts storage containers but by applying them as Info Transports, you could build and have powerful communications in between your entrance-conclude (kind, report, and so forth) and your center-tier (facts mindful lessons). To get your facts mindful class to return facts to your kind, the class could read the needed records into an array that was passed to it as a parameter by reference. The moment the array is returned, a cursor (short-term desk) could be made to retail store the records returned so that the records could then be utilized and manipulated regionally. Your kind could includes the adhering to code in its Init celebration:
Area oE AS Object, lAnswer AS Logical, intRows AS Integer, cMsg AS Character
DIMENSION arrE(one,4) && Generate the Array
oE = CREATEOBJECT(“Staff”) && Generate an Object from your class
-* Get in touch with the GetAllRecs strategy and pass the array as a parameter
lAnswer = oE.GetAllRecs(arrE,intRows)
IF NOT lAnswer && Strategy did not full properly
cMsg = “Mistake transpired obtaining worker records!”
MESSAGEBOX(cMsg,forty eight,”Test Application”)
CANCEL && Abort the kind Opening Process…Prevent Type from Opening
-* Now build the cursor to host the facts returned
Generate CURSOR TEmployees (EmpID c(15) Unique,
EmpName c(fifty), EmpDept c(10), EmpStatus c(10)) FROM ARRAY arrE
The Dimension array generates the array that will be passed to the class to return info from the backend desk. The array is a two dimensional array the place the very first dimension signifies the number of records in the desk and the second dimension signifies the overall number of fields in the desk. The CREATEOBJECT() functionality build the object as common and returns to the reference to the object variable oE. The line lAnswer = oE.GetAllRows is the line that calls the strategy to return facts from the class, passing the array you have declared. The Generate CURSOR line is the place the short-term cursor is made for the kind and then hosted regionally on the kind. Creating the cursor early in the init celebration of the kind enables your facts to be offered for use in the kind early on
Now that the desk records you will need are hosted regionally, you could operate with the cursor just as you could any desk by introducing new records (APPEND), exploring for records in it (Track down), or even deleting records in the cursor (DELETE). Whilst these functions ended up having place in the Type (entrance-conclude), the back again-conclude desk would continue being offered for other buyers to receive facts in accurately the identical way (elevated facts availability).
Another way to use this tactic would be to bind the fields of the cursor (short-term desk) to the corresponding suitable controls on the Type for relieve of navigation (say you wanted to use Upcoming | Previous | Former | Initially buttons), you could individually bind each and every manage applying code this sort of as:
THISFORM.txtEmpID.ControlSource = TEmployees.EmpID
THISFORM.txtEmpName.ControlSource = TEmployees.EmpName
THISFORM.txtEmpDept.ControlSource = TEmployees.EmpDept
THISFORM.txtEmpStatus.ControlSource = TEmployees.EmpStatuis
To bind the Employee ID text box on your kind, we assigned a industry of the cursor to the ControlSource home of the corresponding manage to which we wanted to exhibit the info for each and every industry in our unique desk. This enables you to directly edit the contents of the cursor by typing info into a manage on your kind. You could then include a Upcoming button and then compose the adhering to code in its Click celebration:
Select TEmployees && Be certain this is the energetic cursor particularly if you have many in your kind
IF NOT EOF() && We are not however at conclude of file
IF NOT EOF() checks to make positive that we are not at the conclude of file following which a SKIP moves us one particular history forward. You could also include a Former button and then include the adhering to code to its Click even:
IF NOT BOF() && It it not however Stop of file
IFNOT BOF() checks to make sure that it is not starting-of-file following which a SKIP -one moves backward one particular history. THISFORM.Refresh ensures that the controls on the kind are refreshed with the current history in the cursor. For a Previous button, the code to be extra to its Click celebration could be like the adhering to:
The GO BOTT command moves us to the past history in the desk. If you made the decision to include a Initially button, you could include the adhering to code to its click on celebration:
The GO Top history moves us to the very first history in the desk. Soon after building variations to the cached records, you would have to resubmit the records in your cursor back again to your class so the variations could then be saved. The click on celebration of this sort of a Preserve button could have the adhering to code:
Copy TO ARRAY arrE
lAnswer = THISFORM.Employees1.SaveRecords(arrE)
IF NOT lAnswer
MESSAGEBOX(“Mistake transpired conserving records!’,forty eight,”Test Program’
The SaveRecords strategy of the Staff class could then have code this sort of as the adhering to:
Area intCnt As Integrer, lFileInUse As Logical
Retail outlet TO intCnt
lFileInUse = .T.
lFileInUse = .F.
USE Staff IN
FOR intCnt = one TO intRows
Track down FOR ALLTRIM(Staff.EmpID) = ALLTRIM(arrE(intCnt,one))
IF NOT Located() && Record does not exist…create it
Replace Staff.EmpID WITH arrE(intCnt,one)
-* Preserve variations to the rest of the history
Replace Staff.EmpName WITH arrE(intCnt,2)
Replace Staff.EmpDept WITH arrE(intCnt,3)
Replace Staff.EmpStatus WITH arrE(intCnt,4)
IF NOT lFileInUSe
USE IN Staff
RETURN .T. && Convey to person you accomplished process properly
In the code higher than, the For…EndFor loop ensures that each and every row of the array is processed. It is a counter. For each and every row of the array, the software checks in the desk to see of the history exists! If it does, variations to the history are saved. If it does not (IF NOT Located()), an APPEND BLANK generates a new blank history in the desk and the new history saved to the desk by means of the Replace statements that abide by.
What if the person had deleted a sequence of records on the kind that ought to also be deleted on the back again conclude desk? A very similar system of distributing the record of deleted records could do the work really perfectly! A Delete button on the kind could have the adhering to code in its Click even to delete the currently shown history:
You could then include an Apply Deletes button to eliminate all deleted records at at the time by introducing the adhering to code to the Click even of the Apply Deletes button:
Area intRows AS Integer
DIMENSION arrE(one,4) && Generate the array
Select TEmployees && Make the Staff cursor the energetic operate location
Copy TO ARRAY arrE FOR DELETED() = .T. && Copy all recs marked for deletion
Rely ALL FOR DELETED() = .T. TO intRows && Rely all records marked for deletion
lAnswer = THISFORM.Employees1.ApplyDeletes(arrE,intRows) && Get in touch with Course strategy to delete
In the code higher than, the Copy To Array command populates the array arrE only with the records marked for deletion. The DELETED() functionality returns true for any records marked for deletion. In buy to return the overall number of records marked for deletion, the COUNT…TO command is also utilized, skilled also with the DELETED() functionality to make sure that only records marked for deletion are taken into account in the Rely command. The ApplyDeletes strategy of the Staff class is then named, with arrE and intRows passed as parameters to it. The ApplyDeletes strategy of the Staff class could have the adhering to code:
Area cMsg AS Character, lFileInUse AS Logical, intCnt AS Integer
IF Employed(“Staff”) && File is currently open up
lFileInUse = .T.
USE Staff IN
lFileInUse = .F.
FOR intCnt = one TO intRows
Track down FOR ALLTRIM(Staff.EmpID) = ALLTRIM(arrE(intCnt,one))
IF Located() && Record is found
DELETE && Mark it for deletion in the back again conclude desk
IF NOT lFileInUse && If we opened the desk, close it too
USE IN Staff
RETURN .T. && Convey to calling application that you concluded properly
I the code higher than, the Staff desk us opened. The For…EndFor glance ensures that each and every aspect of the array of deleted records is searched and matched from the Staff desk in the back again-conclude databases. If the history is located, a DELETE command marks the history for deletion in the real desk. The Return .T. ensures that the calling software is familiar with that the process accomplished properly.
So much about varieties, what about reviews?
Visible FoxPro Stories also kind a part of a full Visible FoxPro software so that buyers of your programs can attract out necessary info for challenging duplicate. Stories are typically designed in the Stories Designer and like varieties, reviews too typically have a facts atmosphere in which you would have to place the tables and queries that have the facts utilized on the report. If we advocate a coverage of pooling all facts access by means of facts mindful lessons and not tightly binding reviews and varieties to the databases, how can we receive facts for reviews?
Generally, we are needed to prepare the facts needed for a report right before running the report. This would suggest introducing tales or sights to the form’s facts-atmosphere (what we are advocating from) or applying a DO
In the model we are advocating in this write-up, we could make use of the electric power and richness of the Visible FoxPro programming language to make sure that we declare an array, pass the array to a strategy of the facts-mindful class alongside with the criteria needed and then use the contents of the array returned to build a cursor or desk to be utilized regionally! This all could be achieved in the Init celebration of the facts atmosphere just as we would have carried out if we had utilized a DO
This write-up demonstrates that you can build windows customer programs with Visible FoxPro and raise facts availability and software versatility by applying just about every-working day software characteristics that you currently know – arrays and cursors to build the most powerful programs. Of course, when you offer with programs in which the person interface is disconnected from the back again-conclude databases this sort of as these we have been demonstrating in these sequence of article content, you will have to compose code to manage any facts conflicts that may well come up (e.g. some other person has just transformed a history you want to save or a history that you have also amended) and so on. Nevertheless, the point is designed in the write-up that applying Info Aware lessons as the mattress-rock of your application’s advancement enables you to different the facts access tier from your person services (varieties and reviews) and as a result increases facts availability.
So considerably, the article content in this sequence (both of those the part I and this unique write-up) have assumed that you are building Visible FoxPro Windows Customers (applying Visible FoxPro Kinds) to access facts stored in a Visible FoxPro databases. But what if the software databases you are needed to access is stored in a file structure other than the native Visible FoxPro structure this sort of as perhaps Oracle, Advantage Databases Server or Ms SQL Server? Aspect III of this write-up sequence to occur shortly will discover how you can access powerful Server Databases from your Info-Aware lessons!