Board index » delphi » Locate & Variant Arrays

Locate & Variant Arrays

I'm trying to do a locate on a table, however the locate has different
parameters each time
I run it, so one time it may be made up of elements 1 and 2, another time
made up of
elements 1,3 & 12.

To combat this I thought I could dynamically create a string of search
fields and a variant
array. So the code looks something like this,

VariantArray := VarArrayCreate([0, 11], varVariant);

OtherLocateCount := 0;
If DMData.CUSTOMER <> '' Then
Begin
  ToLocateAgainst := ToLocateAgainst + ';CUSTOMER';
  VariantArray[OtherLocateCount] := Variant(DMData.CUSTOMER);
  Inc(OtherLocateCount);
End;

If DMData.LEDGER <> '' Then
Begin
  ToLocateAgainst := ToLocateAgainst + ';LEDGER';
  VariantArray[OtherLocateCount] := Variant(DMData.Ledger);
  Inc(OtherLocateCount);
End;

VarArrayRedim(VariantArray, OtherLocateCount);

This will then leave me a string which might look like,
'CUSTOMER;LEDGER'

And a variant array with two elements corresponding to CUSTOMER and LEDGER.

However when I run the locate with,

DataModule.MyTable.Locate(ToLocateAgainst, VariantArray, [loPartialKey]);

I get invalid variant type conversion.

Am I doing something obviously wrong?

 

Re:Locate & Variant Arrays


John,

I don't know if this will help you.

It's a lot easier using SQL to locate data in a Table.
i.e.

Q1  Query
XStr : string;

  Q1.Close;
  Q1.SQL.Clear;
  XStr:='select * form Tdata1.db A where A.Field1=''123'' and
A.Field2=35';
  // XStr can be built from other strings
  Q1.SQL.Add(XStr);
  Q1.Open;

  if Q1.RecordCount>0 then // found record

Anyway, it might help you to look into SQL.

Delphi 4 has a good help file LOCALSQL.hlp

Good luck,
Frank
-----------------

Quote
John Dempsey wrote:

> I'm trying to do a locate on a table, however the locate has different
> parameters each time
> I run it, so one time it may be made up of elements 1 and 2, another time
> made up of
> elements 1,3 & 12.

> To combat this I thought I could dynamically create a string of search
> fields and a variant
> array. So the code looks something like this,

> VariantArray := VarArrayCreate([0, 11], varVariant);

> OtherLocateCount := 0;
> If DMData.CUSTOMER <> '' Then
> Begin
>   ToLocateAgainst := ToLocateAgainst + ';CUSTOMER';
>   VariantArray[OtherLocateCount] := Variant(DMData.CUSTOMER);
>   Inc(OtherLocateCount);
> End;

> If DMData.LEDGER <> '' Then
> Begin
>   ToLocateAgainst := ToLocateAgainst + ';LEDGER';
>   VariantArray[OtherLocateCount] := Variant(DMData.Ledger);
>   Inc(OtherLocateCount);
> End;

> VarArrayRedim(VariantArray, OtherLocateCount);

> This will then leave me a string which might look like,
> 'CUSTOMER;LEDGER'

> And a variant array with two elements corresponding to CUSTOMER and LEDGER.

> However when I run the locate with,

> DataModule.MyTable.Locate(ToLocateAgainst, VariantArray, [loPartialKey]);

> I get invalid variant type conversion.

> Am I doing something obviously wrong?

Re:Locate & Variant Arrays


John, I don't completely follow your code because I don't know what
DMData.CUSTOMER and DMData.LEDGER are...

Assuming they are string variables to hold the locate criteria, the
one thing that I would change is the cast to variant when you assign them
to the VariantArray, i.e. use
   VariantArray[OtherLocateCount] := DMData.CUSTOMER;
(this is the way I do it in a similar function that I wrote for a program).

One other thing to check in the help files, and experiment with, is the inclusion of
loPartialKey.  I'm not sure if multiple fields can be partial and still have a
successful locate, in which case SQL is the only way to get what you want.  You can have
a TQuery in the background, then do a locate on the key values for the display
table/query.

Good luck,  Brian

Other Threads