DataSet Connection - how to skip a blank address line

Category

Rave - Data Connections

Question

I have a name and address style report that is working OK, but it has a lot of empty lines where the second address line goes. How do I get rid of those empty address lines.

Solution

The common answer is to use data mirroring and there is a demo on how to do that. However, there is another technique that uses the RvDataSetConnection component.

When you are working with data table(s) and want the output to be based upon some type of calculation, then you should consider using a Rave DataSet Connection. Don't let the term calculation throw you. Think of "IF somecondition THEN PrintPart ELSE PrintAll" concept. For this example, think of using a calculation would be printing out label type information from a table that has two "address" lines and wanting to suppress printing of the second address line when it is blank.

Setting up a Rave DataSet Connection to do this is just a few more steps than what you would normally do. Drop a tTable, DataSource components on your form and connect then to your tables as you normally would. Now, drop a RvDataSetConnection and set it's "DataSet" property to your tTable component. Now all you need to set up two events, OnGetCols and OnGetRows.

The "OnGetCols" event is where you define the field(s) that you wish to "add to" / "calculate for" your data table. The first three parameters ("Name", "DataType", "Width") must be given. The last two parameters ("FullName", "Description") are optional. There are several DataTypes, they are dtBlob, dtBoolean, dtCurrency, dtDate, dtDateTime, dtFloat, dtGraphic, dtInteger, dtInt64, dtMemo, dtString and dtTime. However, if you define more than one field, then the order of creation is very important and that same order must be used when writing the fields in the OnGetRows event.

The "OnGetRows" event is where you do your "calculation logic" and store the results of that calculation in the new field(s) you created. The calculation is done with regular Delphi code and will be execute on each new row. Remember that if you have defined more that one field, that you must write the data back in the same order that you defined it. You do need to pay attention to which write type method you use. It must match the data type of your field. So you would use WriteInt(value) for an Integer field and a WriteInt64(value) for an Int64 field etc.

The following sample code shows how to bring in address line 2 along with the standard city state zip items. Then combine them into one string based upon the contents of address line 2 and take that string is placed in the new calculated field. This new field can then be used in your report design just like a regular table field. So in this case, you would not use the Address2, City, State, Zip or Plus4 in your report design, but you would use the new MyAddr2 field since it contains all the other column (field) information.

Delphi Example:

Procedure TForm1.RvDataSetConnection1GetCols(Connection: TRvCustomConnection);
Begin
  // First, get the tables columns (fields)
  Connection.DoGetCols;

  // Need a field with room to combine the following table columns (fields)
  // Address2 40 - City 20 - State 2 - Zip 5 - Plus4 4
  Connection.WriteField('MyAddr2', dtString, 72, '', '');
End;



Procedure TForm1.RvDataSetConnection1GetRow(Connection: TRvCustomConnection);
Var
  MyAddr2, sAddress2, sCity, sState, sZip, sPlus4: String;
Begin
  // First, get current row values
  Connection.DoGetRow;
  
  // retrieve then "calculate" replacement column (field) MyAddr2
  sAddress2 := Uppercase(tTable.FieldByName('Address2').AsString);
  sCity := Uppercase(tTable.FieldByName('City').AsString);
  sState := Uppercase(tTable.FieldByName('State').AsString);
  sZip := Uppercase(tTable.FieldByName('Zip').AsString);
  sPlus4 := Uppercase(tTable.FieldByName('Plus4').AsString);

  MyAddr2 := '';                  // Start with a null (blank)
  If Length(sAddress2) > 0 Then Begin
    // Address2 contains information
    MyAddr2 := sAddrress2 + #13;  // end this with a carriage return
  End;
  // now combine the separate field information into one variable
  MyAddr2 := MyAddr2 + sCity + ' ' + sState + ' ' + sZip + '-' + sPlus4;
  // now take that combined variable and store it in the new column (field)
  Connection.WriteStrData('', MyAddr2);
End;