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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help in using EOF for searching

Status
Not open for further replies.

zhed0708

IS-IT--Management
Jun 25, 2011
41
PH
I am searching the last record i save using the BOF() but it does not search the last record instead the first record viewed.

whats wrong in my code?

SELECT transaction
SET ORDER to id
LOCATE FOR name = ALLTRIM(thisform.txtname.Value)
IF FOUND()
IF !BOF() then
thisform.txtctcnum.Value = ctcnum
thisform.txtdateissued.Value = dateissued
endif
ENDIF


zhed
 
Zhed,

First, I don't see why you are testing for !BOF(). If the record is found (that is, if FOUND() is .T.), then the table will never be at BOF().

You can take out the test for !BOF() without changing the functionality in any way.

Next, are you saying that it always fails to find the record that you are expecting? If so, I suggest you put ALLTRIM() and UPPER() around the search terms. In other words:

Code:
LOCATE FOR ALLTRIM(UPPPER(name)) = ;
  ALLTRIM(UPPER(thisform.txtname.Value))

If this doesn't answer your question, perhaps you can clarify it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
BOF is Begin Of File, not as you may think - bottom of file.

GO BOTTOM get's you to the last record in sort order. LOCATE FOR searchcondition get's you to the record you search and you will only be at EOF() - End Of File - if there is no such record. As Mike says, testing FOUND() is sufficient and also better to understand in the context.

Also note, that EOF() position really is AFTER the last record, there is no record at EOF() position. You're at the last record in physical order of the DBF file, if RECNO() = RECOUNT() and in the last record in sort order, if SKIP 1 get's you to EOF(). The difference is, because the last record in sort order could also be in the middle of the dbf file, eg in case of ordering by a last name, as the dbf records seldom are in alphaebitcal order, but in the order of data being inserted.

Bye, Olaf.
 
since you have an index. you can do this:
instead of this:
Code:
SELECT transaction
SET ORDER to id
LOCATE FOR name = ALLTRIM(thisform.txtname.Value)    
IF FOUND()        
   IF !BOF() 
      thisform.txtctcnum.Value = ctcnum           
      thisform.txtdateissued.Value = dateissued        
   endif    
ENDIF

do this, much cleaner imo.
Code:
select Transaction
Set Order to ID
if seek(alltrim(thisform.txtName.value)
   thisform.txtctNum.value = ctcNum
   thisform.txtDateIssued.value = DateIssued
endif

Ali Koumaiha
TeknoSoft Inc.
Michigan
 
my last post was missing a closing )

Code:
  if seek(alltrim(thisform.txtName.value)[b])[/b]
   ...

Ali Koumaiha
TeknoSoft Inc.
Michigan
 
Or have you spent ANY time looking in the VFP Help for:
EOF()
or
BOF()
so that you can UNDERSTAND why you are not getting the results you expect?

JRB-Bldr
 
Your thread title says EOF but your code says BOF. I'll assume the typo is in your code because it wouldn't make sense the other way.

If you insist on using LOCATE (which is the least preferable method), make sure you study CONTINUE which works in conjunction with LOCATE.

Code:
LOCATE FOR UPPER(ALLTRIM(field)) == UPPER(ALLTRIM(value))
nRecord = Recno()
  DO WHILE NOT EOF()
    CONTINUE
    IF NOT EOF()
       nRecord = RECNO()
    ENDIF
  ENDDO
ENDIF
IF nRecord <= Reccount()
   Goto nRecord
ELSE
   ** nothing found
ENDIF

But again, THIS IS NOT THE BEST WAY TO FIND THE LAST RECORD!

You should have an index on that field:

SET ORDER TO TAG tagname DESCENDING
SEEK value && <-- here you are!
 
Dan,

I think that what Zhed was looking for was not the last record, but the last record that was saved - in other words, the record currently showing in his form.

I might be totally wrong about that of course. Maybe Zhed will clarify it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
He seems to be trying to locate the last record he saved with a specific value. That's pretty much what he says in his first sentence, anyway.

If he insists on using LOCATE, he'll need CONTINUE, and a way to determine he's CONTINUEd to EOF().
 
Yes Mike,

Code running beforehand, saving the record he now searches would also help. In fact if you save a record via INSERT SQL you are at that record and there is no need to locate it, same goes for APPEND BLANK and Replaces, same goes for GATHER, same goes for TABLEUPDATE() of a single record and from the top of my head, also if you issue TABLEUPDATE() for the whole table this ends in the same record position you had earlier.

Next question I have is, if the table called transaction is what he saves to, or if that is a table a trigger in his database is always filling with any change made, to make up a transaction log file.

Without further knowledge there's only guessing involved.

Zhed, you're not helpful, if you not respond to questions we need to have answered to know how to help.

Bye, Olaf.
 
sorry for the late reply....

here's the scenario,i save my record and these records are id which the index in my table,name,CTC number,Date issued, and etc.After i save my record i want to search it for edit and update for my CTC number and date issued.i use LOCATE FOR name = ALLTRIM(thisform.txtname.Value)to search for my name and when he viewed my name, i want also to search the last record of my CTC number and date issued. so i used these code.

IF !BOF() then
thisform.txtctcnum.Value = ctcnum
thisform.txtdateissued.Value = dateissued
endif

But it doesn't work. but the name that i was searching is working properly. He can viewed the name but the CTC number and date issued doesn't work.

in my save command is this code,


SELECT transaction
APPEND BLANK
replace dateissued WITH thisform.txtdateissued.Value
replace ctcnum WITH thisform.txtctcnum.Value
replace name WITH thisform.txtname.Value
replace address WITH thisform.combo5.Value
replace date WITH thisform.txtdate.Value
replace ornum WITH thisform.txtornum.Value
replace payment WITH thisform.txtpayment.Value
replace purpose WITH thisform.combo4.Value
this.Refresh()

i am trying to save the same name but different in CTC number and date issued.then like what i said, i want to search the last record of CTC number and date issued..

example:
in my table.there are these records:

first record
name: Allan Poe
ctc number: 11111111
date issued: 01/21/1991
second record
name: Allan Poe
ctc number: 22222222
date issued: 01/02/1992
last record
name: Allan Poe
ctc number: 33333333
date issued: 03/03/1993

then i want to search the LAST RECORD which is the CTC number is 33333333 and date issued 03/03/1993.

i hope i make my self cleared....thank you...


zhed
 
In the first place: Testing BOF() is simply wrong, stop doing that.
LOCATE always locates the first record in the order set by SET order. If you want the last record and ID is a field like an autoincrementing integer, then the last record is the one with the highest ID. Then you' dneed to SET ORDER TO ID DESCENDING to let LOCATE find the last and not the first record.

On the other side, if you want to look into the last record you saved right before looking for it, you are already there. APPEND BLANK does not only add a new space for a record at the end of the DBF file, you also pont to that record, there is no need to locate any other record, if you want that one.

Bye, Olaf.
 
sir olaf,
is that possible when my text boxes properties is not control source to my table?
after i save the record, my form releases that's why i need to search for my record.

zhed
 
If a textbox has the table as controlsource or not is not influenceing the record pointer, the record pointer is set to the saved record via APPEND BLANK, no matter if the form is bound to the table or unbound.

What I don't know is, if you want to get values from that new record, I wonder how that would help you, as you can only read back, what you entered, you set that record via

replace dateissued WITH thisform.txtdateissued.Value
replace ctcnum WITH thisform.txtctcnum.Value

Then you'd also read back those values again, that doesn't make sense.

It's likely you want to get data from a previous record, it's likely you want that bvefore you add the new record via APPEND BLANK and not afterwards.

Bye, Olaf.
 
sir olaf,...
yes sir,, i want to get the new values for my ctc number and date issued.when i remove the code
IF !BOF() then
thisform.txtctcnum.Value = ctcnum
thisform.txtdateissued.Value = dateissued
endif
for my search textbox, it will not get the last record instead the first record.

zhed
 
Then you are not exceuting this after the APPEND BLANK. At the start of the form you will always be at the first record.

I already told you you could

SELECT transaction
SET ORDER to id DESCENDING
LOCATE FOR name = ALLTRIM(thisform.txtname.Value)

to find the last record matching the entered name , but that can only be done after a name was entered and also must be done before the new record is added.

If you want the data of the newly created record, you don't do anything before

thisform.txtctcnum.Value = ctcnum
thisform.txtdateissued.Value = dateissued

You neither check BOF, nor do you locate, you are already at the new record. But you will only read back waht you stored there.

I get the idea you want to locate the last record existing in the table to a certain name to copy over some data and update it, but that would need to be done after you enter name and before you add the new record, wouldn't it?

Bye, Olaf.

Bye, Olaf.
 
sir olaf...
your suggestion works fine..sir..is it possible to detect if the ctc number is already exist?i know this question is irrelevant in this thread but i just want to know if possible. i have this code to detect if the user inputs the ctc number and when the name is differ with the name in the database it will return.. but seems it doesn't work for me..

IF thisform.txtctcnum.Value = transaction.ctcnum
MESSAGEBOX("CTC number aleady exist! ",0+0,"IBC System v1.0")
thisform.txtctcnum.SetFocus()
RETURN
endif

zhed
 
IF thisform.txtctcnum.Value = transaction.ctcnum just checks if the current record has that ctcnum. To check the whole table for the existance you need LOCATE in combination with FOUND() or you need an index created via table designer or INDEX ON command once, and then can SEEK, SEEK() or INDEXSEEK().

Please look at the help on the mentioned commands and functions.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top