Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write class file

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
Can someone help me out with writing a class file. I am constatly re-using the same block of code throughout my program (with different parameters). The only thing that really differs is the SQL query. Here is the code:

procedure TfrmMain.LoadCustomers;
var
i : Integer; //variable to loop through query recordset
ADOLoadQuery : TADOQuery;
begin
//initiate empty query
ADOLoadQuery := TADOQuery.Create(nil);
//set ADOQuery connection string
ADOLoadQuery.ConnectionString := connString;
//set the SQL search string for the ADO Query
ADOLoadQuery.SQL.Text := 'Select distinct CUST_CODE, TEMP_NO ' +
'From invoice_data';

ADOLoadQuery.Open; //Open query and connection

//loop through recordset
with ADOLoadQuery.Recordset do
begin
if (RecordCount > 0) then
begin
MoveFirst; //Move to the first record
//loop through SQL query dataset and retrieve corresponding data
for i := 1 to RecordCount do
begin
//add all the customer codes available to the comboboxes on
//the 'View/Modify' form and the 'Delete' form.
//add both customer code + template no. to the combobox on the
//import form
cboExportCustTemp.Items.Add(Fields['CUST_CODE'].Value +
' TEMP NO: ' + FloatToStr(Fields['TEMP_NO'].Value));
if cboDeleteCustomerCode.Items.IndexOf(Fields['CUST_CODE'].Value) = -1 then
cboDeleteCustomerCode.Items.Add(Fields['CUST_CODE'].Value);
if cboViewCustCode.Items.IndexOf(Fields['CUST_CODE'].Value) = -1 then
cboViewCustCode.Items.Add(Fields['CUST_CODE'].Value);
MoveNext; //Move to the next record
end;
end;
end;
ADOLoadQuery.Close; //close query and connection
//select the first item in the combo boxes
cboDeleteCustomerCode.ItemIndex := 0;
cboViewCustCode.ItemIndex := 0;
cboExportCustTemp.ItemIndex := 0;
end;

Any suggestions or comments are welcome.
Thanks.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
You don't necessarily have to make it a separate "class" but you can put the code in a unit that is "used" in all of the places that you need it. It might look something like this:
Code:
function LoadList(sQuery, connString, sField1, sField2: string; var slList1, slList2, slList3: TStrings): boolean;
var
  i : Integer; //variable to loop through query recordset
  ADOLoadQuery : TADOQuery;
begin
  //initiate empty query
  ADOLoadQuery := TADOQuery.Create(nil);
  try
    try
      //set ADOQuery connection string
      ADOLoadQuery.ConnectionString := connString;
      //set the SQL search string for the ADO Query
      ADOLoadQuery.SQL.Text := sQuery;
      ADOLoadQuery.Open; //Open query and connection

      //loop through recordset
      with ADOLoadQuery.Recordset do
      begin
        if (RecordCount > 0) then
        begin
          MoveFirst; //Move to the first record
          //loop through SQL query dataset and retrieve  corresponding data
          for i := 1 to RecordCount do
          begin
            //add all the customer codes available to the comboboxes on
            //the 'View/Modify' form and the 'Delete' form.
            //add both customer code + template no. to the combobox on the
            //import form
            slList1.Add(Fields[sField1].Value +
              '   TEMP NO: ' + FloatToStr(Fields[sField2].Value));
            if slList2.IndexOf(Fields[sField1].Value) = -1 then
               slList2.Add(Fields[sField1].Value);
            if slList3.IndexOf(Fields[sField1].Value) = -1 then
               slList3.Add(Fields['CUST_CODE'].Value);
            MoveNext; //Move to the next record
           end;
         end;
       end;
      result := True;
    finally
      ADOLoadQuery.Close; //close query and connection
      FreeAndNil(ADOLoadQuery);
    end;
  except
    on e:Exception do
    begin
      result := False;
      ShowMessage(e.Message);
    end;
  end;
end;

sQuery is the SQL that you want to run.
connString is the connection string for the query.
sField1 and sField2 are the names of the fields that you want to display.
slList1, slList2 and slList3 are the Items lists from the comboboxes that you're working with.

Set the item indexes for the comboboxes after you return from this function.

-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top