PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → What is the best way to seek with 2 elements in a 3 elements composite key.
What is the best way to seek with 2 elements in a 3 elements composite key.
Iniciado por guest, 31,mar. 2015 14:36 - 9 respuestas
Publicado el 31,marzo 2015 - 14:36
Hello,

I want to use a seek on a composite key. But I want to use only the first element of 2 elements.

It is also possible that I seek like this [elem1,elem2] on a key with 3 elements.

When a execute a line like this

HreadseekFirst(aDataFile,MyTrippleCompositeKey,[elem1,elem2])

There is no error but HFound() gives a false.

The thirth element guarantees me that I found the wanted record.

What is the best way to seek with 2 elements in a 3 elements composite key.

Thanks.

Willy Hermans
Publicado el 31,marzo 2015 - 14:54
Hi Willy

I don't know about the best way, but this is what I would do:
hfilter(aDataFile,MyTrippleCompositeKey,[elem1,elem2,hminval],[elem1,elem2,hmaxval])
hreadfirst(aDataFile,MyTrippleCompositeKey)
while not hout(aDataFile)
Process here
hreadnext(aDataFile,MyTrippleCompositeKey)//As there may be several answers
end

Best regards
Publicado el 31,marzo 2015 - 14:55
Hi,
why you don't add another composite key for the 2 searched elements?
If Hreadseekfirst returns true, then you can check if aDataFile.elem3 is ok.
Publicado el 31,marzo 2015 - 15:10
Hi Willy,

You do it correctly. There are no errors.

Do not forget that you seek on a key with 3 elements and you only have the two firsts. This means that you will never get a perfect match so the function will always return False (i.e. HFound() returns False). (Searching a generic key is always a perfect-match search, so to say.)

In this case, simply check if not HOut() and check if the record that has been read (because this is the case) has the two desired values in you key, then from that point you can loop the records until the two first parts of the key are changing.

Check the help if you wish, your case is described in the bullet point list in the 3rd paragraph, bullet 2: <a class="ExternalLink" rel="nofollow" target="_blank" href="http://doc.windev.com/en-US/index.awp?3044036">http://doc.windev.com/en-US/index.awp&hellip;</a>

Best regards,
Alexandre Leclerc
Publicado el 31,marzo 2015 - 15:28
Hi,

Many thanks for the answers.

Now a suggestion to the WinDev developers. I hope they are also reading this forum.

My old clipper language had already an extra option. I do'nt remember it exactly but it was someting like this.

HSetSeek(Soft/Hard)
With a soft seek the search was working from the left position to the length of the search parameter.

Example
HSetseek(soft)
Seek("123")
The value found is "12345"
Found = true

HSetseek(Hard)
Seek("123")
The value found is "12345"
Found = FALSE

This could minimize the lines of code.

Greetings Willy
Publicado el 31,marzo 2015 - 15:42
Hi Willy,

If you want to make a suggestion, send it to the Free Technical Support.

I agree with your point. But you can also make it in one line of code (just a little bit longer that your simple example, which would be nice to see, indeed):

IF HReadSeekFirst(File,Key,["1","2"]) _OR_ (HOut()=False _AND_ File.Key1="1" _AND_ File.Key2="2") THEN
END

The other way, much simpler:
HReadSeekFirst(File,Key,["1","2"])
WHILE HOut()=False _AND_ File.Key1="1" _AND_ File.Key2="2")
// Process data here
HReakSeekNext()
END

Best regards,
Alexandre Leclerc

PS. In fact we rarely use these commands because we are using HFSQL C/S and we usually use queries for this kind of task because this is way faster (especially on slow networks).
Publicado el 31,marzo 2015 - 16:03
OK,

again many thanks.

Conclusion :

1) In coding a function like HReadSeekFirstSoft(DataFile,Key,Parameter) should be very welcome

2) It is better to use a query that could be empty or returns the correct record to compare.

Greetings,

Willy
Publicado el 01,abril 2015 - 02:01
Hi

You could use this syntax as well


FOR ALL xxx WHERE compisitekey = [1,2]

End

regards

Allard
Publicado el 01,abril 2015 - 08:53
Did u try use HBuildKeyValue(). I am using it when have situation like yours and it works for me.
Publicado el 02,abril 2015 - 11:37
... or use a query ...
IMHO the way to go in the 21st century...

Just my 2 cents,

Peter H.