Subject Re: [IBO] Problem with incremental searching
Author Salvatore Besso
hello Helen,

thank you for quick reply.

This is the ANAGRAPHICS table SQL:

SELECT A.ID_ANAGRAPHIC, A.PREFIX, A.UNIT, A.CALL_SIGN,
A.OPERATOR_NAME, A.CITY, A.TX_PROVINCE,
(SELECT SHORT_NAME FROM PROVINCES WHERE ID_PROVINCE = A.TX_PROVINCE) AS TX_PROV,
A.TX_REGION, A.OWN_PROVINCE,
(SELECT SHORT_NAME FROM PROVINCES WHERE ID_PROVINCE = A.OWN_PROVINCE) AS
OWN_PROV,
A.OWN_REGION, A.CLASS,
A.OP_2_PREFIX, A.OP_2_UNIT, A.OP_2_CALL_SIGN, A.OP_2_NAME,
A.OP_3_PREFIX, A.OP_3_UNIT, A.OP_3_CALL_SIGN, A.OP_3_NAME,
A.CERTIFICATES, A.AMOUNT
FROM ANAGRAPHICS A
ORDER BY A.PREFIX, A.UNIT

This is the PROVINCES table SQL:

SELECT * FROM PROVINCES
WHERE ID_PROVINCE > 0
ORDER BY SHORT_NAME

And this is the TIB_LookupCombo code taken from the dfm (I have two combos on
the form but they do the same thing for two different province fields in the
ANAGRAPHICS table):

object TXLC: TIB_LookupCombo
Left = 16
Top = 168
Width = 64
Height = 24
Hint =
'Transmission province|Select the transmission province from the ' +
'drop down list'
HelpType = htKeyword
HelpKeyword = 'Edit anagraphic: How to select the transmission province'
AutoLabel.Kind = albTop
AutoLabel.Caption = '&TX prov.'
AutoLabel.Margin = 0
AutoLabel.Model = ModelLabel
AutoLabel.Options = [albBoldIfRequired]
DataSource = AnagDM.TXProvDS
TabOrder = 3
TabStopAutomation = True
ColLines = True
DefaultRowHeight = 18
DisplayField = 'SHORT_NAME'
GridLinks.Strings = (
'SHORT_NAME'
'LONG_NAME')
OnChange = TXLCChange
OnCloseUp = TXLCChange
ShowButton = True
end
object OwnLC: TIB_LookupCombo
Left = 120
Top = 168
Width = 64
Height = 24
Hint = 'Home province|Select the home province from the drop down list'
HelpType = htKeyword
HelpKeyword = 'Edit anagraphic: How to select the home province'
AutoLabel.Kind = albTop
AutoLabel.Caption = '&Home prov.'
AutoLabel.Margin = 0
AutoLabel.Model = ModelLabel
AutoLabel.Options = [albBoldIfRequired]
DataSource = AnagDM.OwnProvDS
TabOrder = 4
TabStopAutomation = True
ColLines = True
DefaultRowHeight = 18
DisplayField = 'SHORT_NAME'
GridLinks.Strings = (
'SHORT_NAME'
'LONG_NAME')
OnChange = OwnLCChange
OnCloseUp = OwnLCChange
ShowButton = True
end

If it may be of interest, this is the OnChange event code linked to the combo:

procedure TAnagEditForm.TXLCChange(Sender: TObject);

begin
if Visible then
AnagDM.GetDirRegion(True)
end;

procedure TAnagEditForm.OwnLCChange(Sender: TObject);

begin
if Visible then
AnagDM.GetDirRegion(False)
end;

GetDirRegion does nothing more than retrieving a region ID from a third table
that is not connected with PROVINCES not with ANAGRAPHICS and puts this ID into
ANAGRAPHICS.TX_REGION and ANAGRAPHICS.OWN_REGION:

procedure TAnagDM.GetDirRegion(TX: Boolean);

var
Province: Integer;

begin
if TX then
begin
if AnagQuery.FieldByName('TX_PROVINCE').IsNotNull then
begin
Province := AnagQuery.FieldByName('TX_PROVINCE').AsInteger;
AnagQuery.FieldByName('TX_REGION').AsInteger := GetRegionID(Province)
end
end
else begin
if AnagQuery.FieldByName('OWN_PROVINCE').IsNotNull then
begin
Province := AnagQuery.FieldByName('OWN_PROVINCE').AsInteger;
AnagQuery.FieldByName('OWN_REGION').AsInteger := GetRegionID(Province)
end
end
end;

GetDirRegion uses private method GetRegionID:

function TAnagDM.GetRegionID(IDProvince: Integer): Integer;

begin
try
ProvQuery.Prepare;
ProvQuery.Params.ByName('ID_PROVINCE').AsInteger := IDProvince;
ProvQuery.Open;
if ProvQuery.RecordCount = 1 then
Result := ProvQuery.FieldByName('ID_REGION').AsInteger
else Result := 0
finally
ProvQuery.Close;
ProvQuery.Unprepare
end
end;

ProvQuery is another query different than that connected to the combos. Anyway I
have to make further controls because there is the possibility that GetRegionID
has become useless and maybe I can drop it because when a province ID is
selected by means of the combo, the associated query already has the region ID
available (it is in the query associated with the combos).

That's all.

Thank you
Salvatore