Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SEEK v LOCATE and why does this happen?

Status
Not open for further replies.

keepingbusy

Programmer
Apr 9, 2000
1,470
GB

I use the following to search for an item in a table:
Code:
SET EXACT OFF
SET ORDER TO PRODNUMB
SEEK(mprodnumb)
IF FOUND()
=MESSAGEBOX("etc etc....
  THISFORM.GRID1.SetFocus
  RETURN 0
ENDI
If I use the above, the item IS found in the table but does NOT highlight in a grid on a form but goes to the first record in the grid.

If I use the following, it is still found and will highlight in the grid:
Code:
LOCATE FOR PRODNUMB=mprodnumb
IF FOUND()
  =MESSAGEBOX("etc etc....
  THISFORM.GRID1.SetFocus
  RETURN 0
ENDI

I am led to believe that SEEK is quicker than LOCATE so my question is, how can I overcome the above issue which will allow me to use SEEK instead of LOCATE so the FOUND record will show as a highlighted record in the grid?

I am using VFOX Version 9

Thanks guys

Lee
 
Both the SEEK() function and the LOCATE commands change to the record found. I can't tell ou why there would be a difference in the grid behaviour, maybe that's because of the SET ORDER you do in case of SEEK() but not in case of LOCATE. Be aware that the display order also changes by that. I'd do more than Setfocus(). Add Thisform.Grid1.Refresh() before setting the focus to the grid.

Bye, Olaf.
 
Lee,

I think Olaf's was right when he said the problem is caused by the SET ORDER command. By changing the index order of the table, it is no longer in sync with the grid.

You could try setting the grid's RecordSource to blank before you run the code, and then re-setting it to the appropriate alias afterwards. I think that will do the trick.

A Refresh would do no harm either, although I suspect it won't solve this particular problem.

Or, get rid of the SET ORDER, and pass the index tag as a parameter to the SEEK().

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
On the question of whether SEEK() is faster than LOCATE:

My understanding is that there will be no appreciable difference, provided the expression in the LOCATE's FOR clause can be optimised (in this case, if there's an index on Prodnum).

But I've never tried to verify that.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 

Olaf
I'd do more than Setfocus(). Add Thisform.Grid1.Refresh() before setting the focus to the grid.
That suggestion appeared to resolve the issue. Still strange how one way works and not the other though - Thank you

Mike

I remember reading a thread somewhere on this forum that mentioned SEEK was faster than LOCATE when dealing with a table with many records.

In any case, issue resolved with the additional command line as above.

Thanks again guys for the speedy response as always.

Lee
 
SEEK will be slightly faster than LOCATE if the FOR clause can be optimized. This is because SEEK already knows which index key to use and LOCATE has to determine if there is an applicable key, switch to it, then switch back.

Craig Berntson
MCSD, Visual C# MVP,
 
Lee,

Glad to hear it's sorted.

Lee and Craig,

I see the point about LOCATE having to determine the index. Obviously that will be a relatively small overhead if the query is large, but worth keeping in mind.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
One more thing regarding LOCATE. If there is no suitable index and the query engine things an index would speed things up, it will create one on the fly. I've seen that additional overhead actually be noticable slower than using no index at all.

Craig Berntson
MCSD, Visual C# MVP,
 
If there is no suitable index and the query engine things an index would speed things up, it will create one on the fly.

I never knew that.

It sounds like a two-edged sword to me. As you say, Craig, it could end up taking longer to run the query if the query engine gets it wrong.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top