1. hatem moalla
  2. PowerBuilder
  3. Monday, 26 February 2024 09:13 AM UTC

in my powerbuilder 19 project, i have a DB called "gest_article" and a column "reference" set as a primary key and unique value, and a data window called "dwc_article" is a freeform to insert an article.

i wanna get a message box for duplicated references if i write a reference that already exists and i wanna get this message box before submitting the form

what script should i write and where should i write it and in what event

i tried chatgpt and the script is full of errors

and thank you

John Fauss Accepted Answer Pending Moderation
  1. Tuesday, 27 February 2024 02:55 AM UTC
  2. PowerBuilder
  3. # 1

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!

Comment
There are no comments made yet.
hatem moalla Accepted Answer Pending Moderation
  1. Monday, 26 February 2024 15:56 PM UTC
  2. PowerBuilder
  3. # 2

i started with this script in the event "itemchanged" of datawindow. i got no error but still no message box even tho i write a reference that already exists.
here is the script, can you correct me, please

integer li_row, li_count
string ls_reference

li_row = dwc_article.GetRow()
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
Comment
  1. Arnd Schmidt
  2. Monday, 26 February 2024 16:21 PM UTC
How about reading PowerBuilder's Help about the itemchanged event?
  1. Helpful
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Monday, 26 February 2024 14:40 PM UTC
  2. PowerBuilder
  3. # 3

You may wish to consider performing what Chris has suggested in the DataWindow's ItemChanged event, so that you can detect the conflict and inform the user at the time they supply the (duplicate) value, instead of waiting until they save the data.

Also, I agree with Chris 100% regarding ChatGPT... I have yet to see any AI-generated PB code that was even close to being correct. Don't waste your time.

Comment
  1. Berka Frenfert
  2. Tuesday, 27 February 2024 12:24 PM UTC
Exactly, ChatGPT is supper crazy when we ask PowerBuilder questions. so funny sometimes.
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Monday, 26 February 2024 14:28 PM UTC
  2. PowerBuilder
  3. # 4

Hi Hatem;

  I normally would perform a "Select Count (*) where <key col> = ?????" so that if I get > 0, then I know that this key value exists before I call the DC/DS Update() command.

  Yes, the "chatgpt" generated code examples that I've seen was certainly "lacklustre". Nothing that I would touch.

Regards ... Chris 

Comment
  1. hatem moalla
  2. Monday, 26 February 2024 15:52 PM UTC
i started with this script in the event "itemchanged" of datawindow. i got no error but still no message box even tho i write a reference that already exists

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

"
  1. Helpful
  1. Berka Frenfert
  2. Tuesday, 27 February 2024 12:15 PM UTC
Really, I never applied this approach towards checking duplicates. I make sure my datawindow itself handles all errors. In design mode i set updateable columns and primary key columns. Call Update() and check SQLCA.SQLCODE of transaction object. This worked for me because one time check is better than checking same thing twice.



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.
  1. Helpful
  1. Berka Frenfert
  2. Tuesday, 27 February 2024 12:27 PM UTC
ah, forgot to let you know there is a retrieve() function as well that helps you to fetch data. never mind
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.