Thread: Oracle Forms 9 Problem


Permlink Replies: 12 - Pages: 1 - Last Post: May 6, 2006 6:51 AM Last Post By: Sykiemikey
Sykiemikey

Posts: 8
Registered: 04/26/06
Oracle Forms 9 Problem
Posted: Apr 26, 2006 2:17 PM
Click to report abuse...   Click to reply to this thread Reply
I am learning forms with Oracle 9i and have run into a problem.

The following code runs just fine in a Save Button with a WHEN
-BUTTON-PRESSED trigger that inserts a record from a stacked single
record block (does not allow updates or deletes from it).

DECLARE
edit_alert alert;
show boolean := false;
message varchar2(150) := 'You Must Fill the Following Fields:';
choice number;
BEGIN
IF :add_region.region_id IS NULL THEN
show := true;
message := message || CHR(10) || '----- Region Id';
END IF;

IF show = true THEN
edit_alert := Find_Alert('EDIT');
set_alert_property(edit_alert, ALERT_MESSAGE_TEXT, message);
choice := show_alert(edit_alert);
ELSE
insert_record;
commit;
clear_block;
go_block('regions');
END IF;
END;

But when I try to use the following trigger on a Save button which is
almost identical on a table canvas with a multi-line block that allows
queries, updates and deletes...it basically skips the IF....IS NULL
section because the show never turns true and the alert never fires so
it automatically trys to update the record, but fails because of the
null value. It works perfect if the item is not null and gets inserted.
I figure that it has something to do with :regions.region_id within a
multi-line block and the if statement not knowing the current record.
I have searched and searched for a solution with no luck. Can anyone
help?

DECLARE
edit_alert alert;
show boolean := false;
message varchar2(150) := 'You Must Fill the Following Fields:';
choice number;
BEGIN
IF :regions.region_id IS NULL THEN
show := true;
message := message || CHR(10) || '----- Region Id';
END IF;

IF show = true THEN
edit_alert := Find_Alert('EDIT');
set_alert_property(edit_alert, ALERT_MESSAGE_TEXT, message);
choice := show_alert(edit_alert);
ELSE
update_record;
commit;
END IF;
END;

I appreciate any time and help....

Michael

Steve Cosner

Posts: 3,913
Registered: 08/11/98
Re: Oracle Forms 9 Problem
Posted: Apr 26, 2006 10:19 PM   in response to: Sykiemikey in response to: Sykiemikey
Click to report abuse...   Click to reply to this thread Reply
Standard forms don't work like that.

To insert or update a record, you just type in new values, and press a button that does a Commit_Form. Forms does the inserting for you.

So all you need to do is make sure the required fields are entered. To do that, you write code in a when-validate-record trigger, and if a field is not entered, you display your message, either using an alert or a simple Message() command, and then Raise Form_Trigger_failure; This causes processing to stop until you fill the fields.
Sykiemikey

Posts: 8
Registered: 04/26/06
Re: Oracle Forms 9 Problem
Posted: Apr 29, 2006 5:16 AM   in response to: Steve Cosner in response to: Steve Cosner
Click to report abuse...   Click to reply to this thread Reply
OK, so I moved my save button code to a when-validate-record trigger and changed my save button to just have commit_form; It still doesn't seem to see the :regions.region_id as being null......and in turn does not show my custom alert.

You say the forms don't work that way, but it works perfect in my add form and it is relatively the exact same code.

My problem has either got to be the fact that I run a query on the block before I update, or it is just not seeing the current record in a multi-record block.

If anyone has any further insight, it would be most appreciated.

M
Sykiemikey

Posts: 8
Registered: 04/26/06
Re: Oracle Forms 9 Problem
Posted: Apr 29, 2006 8:07 PM   in response to: Sykiemikey in response to: Sykiemikey
Click to report abuse...   Click to reply to this thread Reply
I figured out a solution, don't know if it is the most efficient, but it works.

I have a save button that contains the following.

update_record;
commit;

and an ON-ERROR trigger on the block with the following to check not null and foreign key integrity....

DECLARE
edit_alert alert;
null_values boolean := false;
invalid_values boolean := false;

cou_id hr_countries.country_id%TYPE;

null_message varchar2(200) := 'CANNOT BE NULL:' || CHR(10);
invalid_message varchar2(200) := 'INVALID VALUES:' || CHR(10);
full_message varchar2(400) := '';
choice number;
BEGIN
IF :locations.city IS NULL THEN
null_values := true;
null_message := null_message || '----- City' || CHR(10);
END IF;

IF :locations.country_id IS NOT NULL THEN
BEGIN
SELECT country_id
INTO cou_id
FROM hr_countries
WHERE UPPER(country_id) = UPPER(:locations.country_id);
EXCEPTION
WHEN OTHERS THEN
invalid_values := true;
:locations.country_id := null;
invalid_message := invalid_message || '----- Country Id' || CHR(10);
END;
END IF;

IF null_values = true OR invalid_values = true THEN
IF null_values = true THEN
full_message := full_message || null_message;
END IF;
IF invalid_values = true THEN
full_message := full_message || invalid_message;
END IF;
edit_alert := Find_Alert('EDIT');
set_alert_property(edit_alert, ALERT_MESSAGE_TEXT, full_message);
choice := show_alert(edit_alert);
END IF;
END;

Thanks for your help.

Michael

Steve Cosner

Posts: 3,913
Registered: 08/11/98
Re: Oracle Forms 9 Problem
Posted: Apr 29, 2006 9:18 PM   in response to: Sykiemikey in response to: Sykiemikey
Click to report abuse...   Click to reply to this thread Reply
You need to start out with a simple form first. You are messing around with on-error triggers (not something you normally need) and update_record, which people almost never use in forms.

You have multiple blocks, both Locations and Regions. Create a form with just one block, and forget the alert. Use the Message command first.

You are playing with too many variables -- it is no wonder things don't work.

Now get back to one block in one form, write a when-validate-record trigger, and use the command:

Raise form_trigger_failure;
Sykiemikey

Posts: 8
Registered: 04/26/06
Re: Oracle Forms 9 Problem
Posted: Apr 30, 2006 9:32 AM   in response to: Steve Cosner in response to: Steve Cosner
Click to report abuse...   Click to reply to this thread Reply
The alert is required, which is why I have no choice to use it. I must have custom error messages. Using a When-Validate-Record trigger was not working for me because it posed the same problem with it not seeing the value of the variable and not being able to validate it. I even tried using commit_form, etc. I followed your instructions to a tee, but to no avail.

I do have multiple blocks, about 26 to be exact...some tabbed and others stacked. I didn't followup with the regions because I ended up not needing validation on it because in my edit forms, I made the primary keys un-editable.

Thanks for your help Steve....but at this point, with the due date so near, I think I will need to stick with what works, and play around with it later.

Michael
Sykiemikey

Posts: 8
Registered: 04/26/06
Re: Oracle Forms 9 Problem
Posted: Apr 30, 2006 9:41 AM   in response to: Sykiemikey in response to: Sykiemikey
Click to report abuse...   Click to reply to this thread Reply
I do have a different question though. If I have forms calling forms, and forms calling menu modules or images, I have to use the long physical path to the file.

I have searched and searched and can't find anyway to get the start path of the form, then concatenate it with the files if they are in the same folder, etc.

C# has the Application.StartPath(), does oracle forms have anything comparable?

I have to turn this in to my teacher, so changing registry keys, environment variables and such will not be effective.

Thanks much
Michael
jwh

Posts: 1,482
Registered: 10/09/01
Re: Oracle Forms 9 Problem
Posted: Apr 30, 2006 11:53 AM   in response to: Sykiemikey in response to: Sykiemikey
Click to report abuse...   Click to reply to this thread Reply
You say the forms don't work that way

Steve said standard forms don't work that way. Your teacher may want to see working programs but (s)he might put more emphasis on your understanding of the forms environment, in which case using standard practices would be best. It's better to be standard in the real world too, although sometimes you really do just have to get something to work before a deadline.

As an example of non-standard practices being bad, consider your idea of having validation/committing in a button. This is Ok as long as the user always presses the button but there are other ways to commit the data, such as closing the form, executing a query or navigating to a new record in a master block. That validation code will not run in those circumstances. Always use a when-validate-* trigger or the property pallette for validation.

Your on-error trigger is also bad. Firstly, do you know why it is firing? Is it because there's a not-null restriction on the region_id item and a null value in the field? If it's firing for any other reason, such as because there is a duplicate record or the database has shut down, then the user will never know because you're overriding standard functionality. If it is firing because of the not-null restriction then a user who does put a value in there will not have their country_id checked at all.

There may be other reasons as well but the on-error trigger is certainly firing because you're calling the update_record procedure in a button. That built-in is only allowed in an on-update trigger (it's annoying that forms will compile with errors like this but that's just the way it is). This will only cause it to fire once, so only the current record will be validated.

Your foreign key integrity check is wrong. You're using
WHERE UPPER(country_id) = UPPER(:locations.country_id);
so if I enter FranCe and FRANCE is on the database the program will not see a problem. That's Ok as long as throughout the system you refer to upper(country_id), but this will negate an index on that column so should be avoided. The standard method is to use an LOV and to set the item to 'Validate from List = true' (property pallette). Also, it's very rare to validate an item other than in its own when-validate-item trigger. The exception is paired items such as the start/end of a date range, where one date must precede the other and it's more user-friendly to validate both fields together.

I can't see why the "if...is null" won't work in the WVR trigger, but if you use standard practices throughout the form and that is the only problem then you're much better off than now.

For the path, use "get_form_property(<module_name>,file_name)" and strip off the <module_name>.fmx part. It seems you have a valid reason for not being standard here (using environment variables) so that's Ok ;-). Good luck with the assignment.

Sykiemikey

Posts: 8
Registered: 04/26/06
Re: Oracle Forms 9 Problem
Posted: Apr 30, 2006 4:22 PM   in response to: jwh in response to: jwh
Click to report abuse...   Click to reply to this thread Reply
Mr. Healy,
You have been most helpful. Thanks for reminding me that quality is a key issue. I'm totally stumped on the when-validate trigger. It seems to catch my foreign key problems, but will not catch the null values no matter what I do. The only trigger that catches the null values is the on-error trigger.

I replaced all of my "update_record; commit;" statements with "commit_form" and it eliminated an error code 0 that I kept receiving.

What if I add an
ELSE THEN
full_message := sqlcode || ': ' sqlerrm;

to the mix to catch all other errors?
I have played around with every variation I can find or think of and am pretty desparate at this point.

Also, about the get_form_property(<module>, file_name)
I used a replace to get rid of the filename(a trim wouldn't work for me) and it works perfect for my images and other forms. The only thing I can't get it to work for is the menu module because it seems to load before the when-new-form-instance trigger that creates the global variable path for the menu. I tried a pre-form trigger and it does the same. Is there anything I can do to create a global variable before the menu_modules load?
jwh

Posts: 1,482
Registered: 10/09/01
Re: Oracle Forms 9 Problem
Posted: May 1, 2006 2:32 AM   in response to: Sykiemikey in response to: Sykiemikey
Click to report abuse...   Click to reply to this thread Reply
If you validate in the on-error trigger then you cannot be sure that each record will be validated. The trigger will only fire when an error is encountered, so it might not fire at all, and only the values in the fields on the current record (the record the user is on when the error happens) can be read. Move the validation to the vhen-validate-record trigger and then fix the problem in that trigger. If you delete the on-error trigger it might help you find the problem. Put a message in the when-validate trigger to make sure the trigger is firing and to show the value and length of data in the field you're testing.

On-Error triggers don't use sqlcode and sqlerrm. Check the forms help for how to trap specific errors in on-error and on-message triggers.

Your teacher probably has an environment set up where they can put all forms/menus/libraries into a directory and run the required form. Unless you've been told otherwise, it's probably best to ignore the details of how the forms will be run.
Sykiemikey

Posts: 8
Registered: 04/26/06
Re: Oracle Forms 9 Problem
Posted: May 1, 2006 1:09 PM   in response to: jwh in response to: jwh
Click to report abuse...   Click to reply to this thread Reply
So here is an update.

I FINALLY figured out what the problem was with my IF....NOT NULL statement in my query/update/delete form. It was referring to an item that was set to required! I changed that and moved it to a When-validate-record trigger and it worked like a charm. I got my validations down to a minimum by making the primary keys uneditable and getting rid of the foreign key checks by using LOV's and validating from the list. I even learned how to add a null value to the record group using a UNION and the dual table for LOVs associated with non-required foreign key items.

I also figured out how to actually utilize the on-error trigger. Tell me if I'm wrong.

I have a
DECLARE
"variables"
BEGIN
IF MESSAGE_TYPE='FRM' AND MESSAGE_CODE=50026 THEN
"custom alert for date format"
END IF;
END;

It works perfect....but tell me....I added this on-error trigger only to blocks that have dates. Will this allow other errors (database down) to show in the status bar?

Thanks James for your very prompt and professional help.
I was about to settle for a really bad solution....thanks for the push.

Michael
Steve Cosner

Posts: 3,913
Registered: 08/11/98
Re: Oracle Forms 9 Problem
Posted: May 1, 2006 3:15 PM   in response to: Sykiemikey in response to: Sykiemikey
Click to report abuse...   Click to reply to this thread Reply
DECLARE
"variables"
BEGIN
IF MESSAGE_TYPE='FRM' AND MESSAGE_CODE=50026 THEN
"custom alert for date format"
END IF;
END;


I always put the on-error trigger at the form-level, but I believe your block-level triggers should be ok. If you put it at the form-level, yours could be coded like this:

DECLARE
Err_Code NUMBER(5) := ERROR_CODE;
MSG VARCHAR2(150)
:= SUBSTR(' '||ERROR_TYPE||'-'||TO_CHAR(Err_Code)||': '
||ERROR_TEXT,1,150);
BEGIN
IF Err_Code=50026 THEN
"custom alert for date format";
ELSE
Message(MSG);
END IF;
Raise form_trigger_failure;
END;
Sykiemikey

Posts: 8
Registered: 04/26/06
Re: Oracle Forms 9 Problem
Posted: May 6, 2006 6:51 AM   in response to: Steve Cosner in response to: Steve Cosner
Click to report abuse...   Click to reply to this thread Reply
Steve,
I changed my on-error to go at form level(no reason to duplicate the exact same trigger in the blocks), and am also going to add a few other custom messages. So far it works perfect.

Thank you all for your help. I posted else where the same day I started this post and received no replies. You guys rock!

Michael
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums