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

Go to (n) record where n is thisform.text1.value

Filip Brnic

Programmer
Dec 25, 2023
43
RS
I wanna create a command button that does this, i tried alot of things and chatgpt cant help me, i looked trough the help guide but i dont get what im doing wrong
the table is roba
theres an index for field sifra
I want it to basically go to the record where N=Sifra like you would do with lets say go 2, but instead i want it to do go(N) where n is thisform.text1.value
TELL ME if i need to change structure of the table, currently its integer autoinc, i kinda need that because of the other forms, so i hope someone can make it work witw integer autoinc...
this is the code ive tried
n = THISFORM.text1.Value
IF EMPTY(n)
RETURN
ENDIF
n = VAL(n)
SELECT Roba
IF !USED("Roba")
MESSAGEBOX("Table 'Roba' is not open.")
RETURN
ENDIF

LOCATE FOR Sifra = n
 
Two things.

1. If you do a locate and find something, the form will still need to be refreshed or you will still see the OLD values. Do this with Thisform.Refresh() after the locate.

2. Whenever you do something like this, you should add some code to handle the possibility that nothing was found.

So you could do something like this:

Code:
Locate for Sifra = n
if found()
   thisform.refresh()
else
   MessageBox("Not found")
   ** put something here to go someplace else, then refresh again.
endif
 
Yet another misunderstanding:
The GO command always positions to the sequential record number, counted from top, wheras a LOCATE or SEEK compares a fieldcontent to your number.
The content of your SIFRA field may be identical to the logical record number, but normally you don't mess with recordnumbers but use a field with a unique identifier. Recordnumbers change when you delete a record snd then pack the table.
 
I didn't think he was referring to a record number. He mentioned a column named Sifra, so assuming that's the column name, and the value in the variable N is valid, the locate would work, but as I mentioned, he would not know that it worked because the screen would still need to be refreshed.

Also, there should always be a way to handle cases where the value is not found.

If it turns out he does want the absolute record position, then GO would work, but there would need to be logic BEFORE issuing GO in case they try to go past the end of the file. In that case if n <= recount() would be used before the GO.
 
Personally, I don't understand what the problem is. The record cannot be found? The form doesn't show result if the record has been found?
 
I would create an index on the table as follows:

Code:
INDEX ON BINTOC(sifra) TAG sifra

to build the index tag. Then in the InteractiveChange() event of the text box I would enter the following:

Code:
LOCAL lnSeek
IF VARTYPE(this.Value) = 'N'
    lnSeek = this.Value
ELSE
    lnSeek = CAST(this.Value TO Integer)
ENDIF
IF SEEK(BINTOC(lnSeek), "Roba", "sifra")
    thisform.grdMyGrid.Refresh()                                && this should be your grid object
ELSE
    WAIT WINDOW "Record Not Found" WINDOW AT thisform.Height/2, thisform.Width/2 NOWAIT TIMEOUT 3     && This is optional...
ENDIF

There is not a need to have a separate commandbutton.
 
Last edited:
Hi,

No offense, but the shown code snippet simply doesn't make sense
Code:
n = THISFORM.text1.Value
IF EMPTY(n)
RETURN
ENDIF
n = VAL(n)
SELECT Roba
IF !USED("Roba")
MESSAGEBOX("Table 'Roba' is not open.")
RETURN
ENDIF

LOCATE FOR Sifra = n


Adapting it you might want to write something like below (not tested)

Code:
Local lnX
lnX = THISFORM.text1.Value
IF VARTYPE(lnX) != "N"
= MESSAGEBOX("No Number")
ELSE
IF !USED("Roba")
MESSAGEBOX("Table 'Roba' is not open.")
ELSE
SELECT Roba
IF INDEXSEEK(lnX, .T., "ROBA", "SIFRA")
This.Form.Refresh()
ELSE
= MESSAGEBOX("Record not found")
LOCATE
This.Form.Refresh()
ENDIF
ENDIF
ENDIF

Please do also have a look at all the other hints in this thread.

hth

MarK
 
Last edited:
the shown code snippet simply doesn't make sense
I think it's what ChatGPT generated. Not very straight forward, it can even break when text1 textbox is set to a numeric value, but if text1 is a standard empty textbox without controlsource and default value, it'll be char type and the code works, except it doesn't refresh the form. So you can't say it makes no sense at all.

You can do much better and shorter, but it's not even bad to do LOCATE, as that also makes use of the index.

Well, and in your code, you made a typo:
Code:
This.Form.Refresh()
must be
Code:
ThisForm.Refresh()
I also have opinions, but what's most important is that Filip does not only have working code, but code he understands himself. I'm not convinced there's a learning effort, if you use AI to generate your code and then don't even have the patience to go through help topics of the code to understand it.
 

Part and Inventory Search

Sponsor

Back
Top