Hi, Hatem -
I want to start this reply by saying that it appears you may be relatively new to PowerBuilder. There's nothing wrong with that, because everyone has to be new to PB at some point. That being said, if you're going to be a PB developer, you need to learn how things are done in PB. Asking questions here is one way, ChatGPT is a poor (in my opinion) way, but please realize the primary purpose of the Q&A forum is to ask technical questions about Appeon products. While responders may include (at their discretion) snippets of code (as I am going to do below), in general you should not expect responders to write your code for you... that begins to approach what many would consider consulting services. It is important to also recognize that most of use have full-time jobs that must take first priority. What then, can you do?
1. If you have not already done so, consider taking the (free) lessons at Appeon -U: https://www.appeon.com/appeon-u
2. Read through the Application Techniques publication: https://docs.appeon.com/pb2022r3/application_techniques/index.html
3. Look online for one or more PB books. There are a few older books that can be found for a reasonable price.
4. Look at examples: The CodeXchange section on the Appeon Community contains almost 200 example applications. Some are complex, but many are simple: https://community.appeon.com/index.php/codeexchange/powerbuilder
5. There are also several dozen example applications available for free download on Roland Smith's TopWizProgramming.com web site: https://www.topwizprogramming.com/freecode.html
The TopWiz examples and the ones in CodeXchange typically focus on one particular subject or topic and are not intended to teach you how to create applications using PB, but the first three items I've listed above are better suited to do so. I encourage you to check them out.
----------------------
Now, to answer your question, the ItemChanged event in the DataWindow Control (commonly abbreviated as DWC) fires when the user has modified data but before the changed data has been placed into the DataWindow's primary data buffer. Your code tries to obtain the new data value from the DataWindow's primary data buffer, but that value does not yet exist there.
The ItemChanged event gives you the opportunity to examine and either accept or reject the new data. The new/pending data value is supplied to the event in the string argument named "data". Because the ItemChanged event fires when any row/column's data has changed, the argument named "dwo" (an object of type DWObject) can be used to identify the name of the column that the user has modified. The column's name can be examined via the Name property of the dwo object. Typically, an ItemChanged event begins with a Choose Case statement based on dwo.Name:
Choose Case dwo.Name
Case "customer_name"
// Inspect and approve/reject the value in the event argument named "data"...
// For example:
If Trim(data) = "" Then
// A return code of 1 rejects the user's data and prevents focus
// from changing to another column. A return code of 2 rejects the
// user's data, but allows focus to change to another column.
Return 1
End If
Case "customer_type"
// Inspect and approve/reject...
Case "article"
// Inspect and approve/reject...
Case ...
// Repeat for each column, as needed...
End Choose
// A return code of 0 accepts the user's data.
Return 0
The event argument named "row" (data type: Long) contains the row number of the column whose data value is being changed.
So... if you want to issue a SQL statement such as what Chris has suggested, you can do so in one of these "Case" clauses of the Choose Case statement, then depending on the count returned by the SQL SELECT statement, issue the appropriate return code (0, 1, or 2).
I hope this helps. Good luck!
John
One more thing: When you are satisfied with any answer to a question you post, please take a moment and mark the question/issue as resolved. Thanks!
here is the script can you correct me please
"integer li_row, li_count
string ls_reference
li_row = Row
ls_reference = dwc_article.GetItemString(li_row, "reference")
select count(reference) into :li_count from gest_article where reference = :ls_reference using SQLCA;
if li_count > 0 then
MessageBox("Duplicate Reference", "This reference already exists. Please choose a different one.")
dwc_article.SetItem(li_row, "reference", "") // Clear the reference field
end if
"
I dont like writing SQL queries in code. I make datawindow object for every simple and complex query. use SetTransObject(SQLCA) and Update() function and all the errors are covered this way.
Also, i use many many datawindow controls in window and hide those i dont need show. There are many advantage in this approach. You collect data and apply formulas, sort and filter and do all kind row manipulations right in your window on client side.
I advise you to avoid use of writing quesries in your code and see how datawindows can be used that work much better than queries in code.
Ther are whole bunch of datawindow types you can use, even external datawindows can be used which have no DB table assocated with them. Just giving you ideas.
Hope this help you going deeper into software pacific ocean to find leftover in sunk titanic.