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 Andrzejek 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
49
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.
 
Hello, i understand some concerns about chatgpt, i usually try to get my code from it then learn the help guide about what it used.
I also saw someone that doesnt understand whats the problem, the problem with my code is when i go to the record, i see down left record 2/2, but the issue is when i try to use refresh or click on the other fields it just becomes readonly locking them basically.

ill try some codes from the thread here and ill let you know what i find success with and what i understood the best!
 
use refresh or click on the other fields it just becomes readonly locking them basically.
First of all it's absolutely necessary to do the thisforn.refresh(), otherwise you only get a control refreshed when clicking on it.
If controls get "locked", that is readonly/disabled, you didn't find a record, end up at EOF(), that is after the last record, there's a place for the record pointer of a workarea, where no record yet exists.

If you don't want that, then you have to cover the case of not finding a record with LOCATE as Joe shows in the firs answer. He posted the comment
Code:
** put something here to go someplace else, then refresh again.

That could be GO TOP or you just stay at EOF and take it as normal to not be able to edit a record as none is found. If you also have a grid on the form, that'll still list all records, be at the end and have no record mark in a row with data, as the record pointer is after all data.

That's a concept you need to know, when no record is found, then FOUND() is false and that is totally equivalent that EOF() is true. So FOUND() can also be seen as a synonym for NOT EOF() or in short notation !EOF(), if you ever find that in code.

Well, and that's a big point, if the chatgpt generated code doesn't even contain FOUND(), EOF() and Thisform.Refresh() you don't know and can't learn that it's missing and necessary.

You don't learn that from chatgpt, chatgtp is bad at teaching because just having code that only works halfways as there is no actual knowledge behind it is not teaching you anything more than vague direction of what could be done. ChatGPT is not really AI in a certain sense, it's a large language model that comes up with what would be continuation of a text and that includes a code answer, but it halucinates things that don't exist, OpenAI has reduced that in the latest models, but it still isn't having the knowledge of filtering out best or even ideal answers. In short, chatgtp didn't opnly learn from the best answers, it learned from all text it rad, also junk and wrong text.

Ask here in the first place, not only in the second place, and you don't need to dig into code you don't understand and that only halfway works, you get a code sample including explanations.
 
Last edited:
Hello, here is what i have a problem with now.
the table itself gets the records by using the form
And in that form i made a code that works like integer autoinc, but the thing is i used the following code and sifra is Character(3)

Im not sure if the same is gonna work if i try to change it to numeric
But ive generally always use this code for tables where i need like ID or (sifra)

Just letting you know because i see in alot of codes you provide there is something to do with N.

PUBLIC WBROJ
calculate max(val(roba.sifra)) to aaa
aaaa=subst(right(str(aaa),3),3,3)
if aaaa='9'
sele * from opstine where sifra!=aaaa into cursor Manji
calculate max(val(Manji.sifra)) to aaa
bbb=aaa+1
wbroj=right('000'+alltr(str(bbb,3)),3)
sele roba
else
bbb=aaa+1
wbroj=right('000'+alltr(str(bbb,3)),3)
ENDIF

APPEND BLANK
thisform.text3.Value = wbroj
 
sifra is Character(3)
Don't store numbers in a string, that's awful.

Even your "original" ChatGPT code was expecting sifra to be an integer or numeric field, not char. You will get a type error and you didn't report one, so how should we know? Numbers are stored n numeric or integer fields, not character fields. Your computation of a next number is overcomplicated, convoluted and bound to give you problems, go for an integer field, Filip, and throw out this nonsensical usage of char fields for numbers.
 

Part and Inventory Search

Sponsor

Back
Top