[ Pobierz całość w formacie PDF ] .After entering the data, clicking the ApplyRange button would put the request into effect.So far, you have learned how to filter the data from a table so that you view only a particular range of records.The steps involvedare threefold:1.Call SetRangeStart and specify the beginning value in the range of records you want to see.2.Call SetRangeEnd and specify the ending value in the range of records you want to see.file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch09.htm (17 of 36) [10/10/2000 1:13:07 AM]Ch 9 -- Using TTable and TDataSet3.Call ApplyRange in order to view the results of your request.BCB also provides a shorthand method calling the SetRangeStart, SetRangeEnd, and ApplyRange methods.Thistechnique is featured in both the Ranger and Ranger2 programs.The conceit behind the Ranger2 program is to place a Windows 95track bar control at the top of the form, and to enable the user to move the thumb on the track bar in order to select a range of valuesto view:void __fastcall TForm1::TBarChange(TObject *Sender){int i = TBar->Position * 1000;int j = i + 1000;DMod->SetRange(i, j);}TBarChange is called whenever the thumb on the track bar moves.The valid range of values on the track bar are 1 to 10.Thecode multiplies current position on the track bar times 1,000 to get the start of the range, and adds 1,000 to this value to get the endrange.The theory here is that the track bar measures the range in increments of 1,000.The TBarChange method calls the SetRange method of TDMod:void TDMod::SetRange(int AStart, int AFinish){tblCustomer->SetRange(OPENARRAY(TVarRec, (AStart)), OPENARRAY(TVarRec, (AFinish)));}This TTable SetRange method takes two OpenArray templates, the first to cover the starting range, the second to cover theending range.The idea here is that the table might be indexed on multiple fields, and you can use an OpenArray to specify thevalues associated with more than one field, if needed.Filtering with the OnFilterRecord EventThe OnFilterRecord event enables you to set up filters on fields even if they are not keyed.You can use this event in twodifferent ways.The first technique involves setting the TTable Filtered property to True.When you do this, you will see only the recordsthat are designated by the formula defined in the OnFilterRecord event.For instance, if you had a State field in your datasetand the OnFilterRecord event said to accept only records from New Hampshire, you would see only the records from NewHampshire when Filtered was set to True.The second technique enables you to search for records even when Filtered is set to False.For instance, if you set up anOnFilterRecord event that accepted only records from New Hampshire, you could call Table->FindFirst to find the firstof these records, and Table->FindNext to find the next one, and so on.There are also FindPrior and FindLast propertiesthat you can use with the OnFilterRecord event.The key point to remember is that as long as the OnFilterRecord event isimplemented correctly, you can use FindFirst, and so on, even when Filtered is not set to True.An example of the OnFilterRecord event is shown in the Filter program found on this book's CD-ROM.The rest of this sectiondescribes how to create that program from scratch.To see the OnFilterRecord event in a live program, start by dragging the Country table off the Database Explorer onto ablank form or data module from a new project.(It sometimes helps to close the Database Explorer after the drop operation, ratherthan trying to switch between the two tools by changing their focus.) Drop down a panel and set up the Align property for thepanel and for the TDBGrid as explained in the previous examples from this chapter.Place a TCheckBox object on the panel and set its caption to Filtered.Associate the following method with the OnClickevent for the checkbox:void __fastcall TForm1::FFilterClick(TObject *Sender){file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch09.htm (18 of 36) [10/10/2000 1:13:07 AM]Ch 9 -- Using TTable and TDataSetFCountry->Filtered = FFilter->Checked;}This code ensures that the table will be filtered whenever the checkbox is checked.Use the Fields Editor for the Table1 object to create field objects for all the fields in the database.Drag the Continent field offthe Fields Editor onto the form, as shown in Figure 9.8.FIGURE 9.8.The main form for the Filter program includes a grid, a panel, a checkbox, a TDBEdit control, and a button.Turn to the Events page for the TTable object, and associate the following code with the OnFilterRecord event:void __fastcall TForm1::FCountryFilterRecord(TDataSet *DataSet, Boolean &Accept){Accept = tblCountry->FieldByName("Continent")->AsString == "South America";}This code states that the OnFilterRecord event will accept all records where the Continent field of the Country tablecontains the word "South America".The Continent field will have either the value North America or SouthAmerica in it.If you click the checkbox to turn the filter on, you will see only the records from South America.In short, the filterwill automatically accept only those records whose Continent field matches the value "South America".If you wanted, you could change the code so that it always filtered on the value currently in the Continent field:void __fastcall TForm1::FCountryFilterRecord(TDataSet *DataSet, Boolean &Accept){Accept = tblCountry->FieldByName("Continent")->AsString == DBEdit1->Text;}For this code to work, you must drop down a DBEdit control, and hook it up to the Continent field of the Country table.It's important to note that the Accept field of the OnFilterRecord event is a Boolean value.This means that you can set upany kind of a Boolean statement in order to set the value of this field.For instance, in addition to the = operator, you could also usethe following operators: , >, or FindNext();}The other functions work exactly the same way.This is a Boolean function that will return False if the search fails.Rememberthat these methods work even if the Filtered property of the TTable object is set to False.Using the Refresh FunctionAs you already know, any table that you open is always subject to change.In short, you should regard a table as a fluid, rather thanas a static, entity.Even if you are the only person using a particular table and even if you are not working in a networkedenvironment, there is always the possibility that the program you are running may have two different ways of changing a piece ofdata.As a result, you should always be aware of the need to update, or refresh, your current view of a table.Furthermore, BCB willnot always update data after you perform an action in the background.As a result, you may need to update data by performing arefresh, particularly after certain kinds of delete operations.file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch09.htm (19 of 36) [10/10/2000 1:13:07 AM]Ch 9 -- Using TTable and TDataSetThe Refresh function is related to the Open function because it retrieves the data, or some portion of the data, associated with agiven table.For instance, when you open a table, BCB retrieves data directly from a database file
[ Pobierz całość w formacie PDF ]
zanotowane.pldoc.pisz.plpdf.pisz.plhanula1950.keep.pl
|