Page 1 of 2

dc_setrelation question

Posted: Tue Nov 16, 2010 8:10 am
by BruceN
I've used dc_setrelation a few times previously, always with the indexed field on parent being the link field. Like: dc_SetRelation("Calls", {||reg1->r_r_serno} )

How do I do it when the link field is NOT the indexed field?

If I have 2 files REG and CALLS, Calls is the parent and I want to look up r_name in REG based on matching calls->c_serno == reg->r_serno, but the active index in calls is c_date... what would be the proper syntax of the codeblock to do that?

thanks

Re: dc_setrelation question

Posted: Tue Nov 16, 2010 9:41 am
by skiman
Hi,

Relation only works if there is an index active with the key. Same as using a scope.

Best regards,
Chris Andries

Re: dc_setrelation question

Posted: Tue Nov 16, 2010 11:42 am
by rdonnay
DC_SetRelation() is basically a wrapper function that uses dbSetRelation().
This performs a seek based on the currently selected order of the relational database.
Why do you want to select a different index tag than the one needed for the seek?

Re: dc_setrelation question

Posted: Wed Nov 17, 2010 5:59 am
by BruceN
What I'm trying to do is view records from CALLS sorted in date order. But I need to look up values in the related file REG (linked with c_serno == r_serno) to display on the form and/or browse grid.

I could with every navigation button (next, prev, first, last) perform a dbseek() as part of the ACTION code block. I guess I could do the same with a browse. I was hoping to have a 'slicker', more automatd method of doing it. dc_setrelation seemed to offer that solution... but I guess it won't work.

Is there a better way than my thought?

As always... grasshopper humbly learn at feet of masters.

Re: dc_setrelation question

Posted: Wed Nov 17, 2010 6:21 am
by RDalzell
Within your DCBROWSE use a codeblock to perform information retrieval

Code: Select all

  DCBROWSECOL   DATA {|| WaiverDesc(WaiverMst->W_Code) }        HEADER ';Waiver Description'                   WIDTH 20  PARENT oBrowse
Then

Code: Select all

STATIC FUNCTION WaiverDesc(cWcode)

  nSelect := SELECT()
  SELECT CodeMst
  SET ORDER TO 1
  SEEK cWcode
  IF Eof()
    cWaiverDesc := cWcode
  ELSE
    cWaiverDesc := Trim(CodeMst->CmDesc)
  ENDIF
  SELECT (nSelect)

RETURN(cWaiverDesc)

Re: dc_setrelation question

Posted: Wed Nov 17, 2010 8:47 am
by rdonnay
Sometimes "automated" just doesn't cut it and a little programming is required.

Rick Dalzell's solution looks pretty "slick" to me.

Re: dc_setrelation question

Posted: Wed Nov 17, 2010 10:24 am
by skiman
Hi,

Any reason why you don't use an extra index? If the key is c_serno+dtos(date) you have your speed and within a serialnumber a sorted list by date.

I don't know if your child contains multipe records for each parent? In that case a scope would be the best solution.

I don't understand how you would perform a dbseek() if your indexkey is the date?

If the way Rick is doing it is a solution for you, you could do this in one codeblock.
{|| reg->(dbseek(calls->c_serno)),iif( reg->(eof()) , " show this" , "or this") } But of course, this way you need the key in the index, which you don't have?

Without indexkey on c_serno yu can't find your record fast. If your file is little, you can use locate, but this is working VERY slow if your file grows!

Re: dc_setrelation question

Posted: Wed Nov 17, 2010 10:36 am
by BruceN
THanks guys.. I REALLY appreciate the suggations and help!

Chris: I don't think that would work for my stuation. I want to show records for all serial numbers sorted by date... and lookup the company name in REG for whatever the serial number in the CALLS record is. As I understand it your method would only be useful to show records for a single serial number in date order - I do that elsewhere and, in fact, that is the actual index I use for it (great minds think alike - so do ours)!

The only way I could think to do it was conceptually the same as Rick's method - but I'm sure my version of the code would not be nearly as elegant (a fancier for for slick) as his. I was hoping to find some way to 'auto-track' like a setrelation(), but I guess that idea isn't feasable.

I'm going to use Rick's code and be happy that it works!

thanks again...

Re: dc_setrelation question

Posted: Thu Nov 18, 2010 9:20 am
by skiman
Hi,

I'm rather confused about the difference between Ricks code and my codeblock. I don't see it.

However the most important is that your problem is solved.

Re: dc_setrelation question

Posted: Thu Nov 18, 2010 2:03 pm
by RDalzell
It's very simple Chris, yours has an accent...