User-defined function: using parameter in error message

Discussions about CSEntry
Post Reply
Keith Tomlin
Posts: 56
Joined: March 23rd, 2014, 9:30 am

User-defined function: using parameter in error message

Post by Keith Tomlin »

Hello there - I'm trying to use a function which can be used multiple times to check if the value of one variable is incorrectly greater than that for another. If that's the case then the user gets an error message asking if they want to review the data. The syntax for my function so far is:-

function CheckStaff (Q1, Q2);
if Q1 <> 99 then
if Q2 > Q1 then
errmsg ("Number trained is greater than number employed! Do you want to review?") select ("Yes", Q2, "No", continue);
endif;
endif;
end;

This would then be placed in the postproc of the relevant fields. But at the moment it's giving me the following error at the line of the error message in the function:-
ERROR: It cannot be a work variable near line 24 in GLOBAL procedure

Which I think means that there is a problem with using the parameter Q2 within the select part of the error message. Could anyone advise me what the correct syntax should be?

Many thanks

Keith
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: User-defined function: using parameter in error message

Post by josh »

That is a poorly worded error message. The problem is that the second argument to select (where you have Q2) needs to be the name of the field to skip to but Q2 is the value of the field. In other words if you call your function with two fields NUMTRAINED and NUMEMPLOYED where the user has entered 10 for NUMTRAINED and 20 for NUMEMPLOYED e.g. CheckStaff(NUMTRAINED, NUMEMPLOYED), then inside the function Q2 will be equal to 20 so you are effectively saying select("Yes", 20, "No", continue) when what you want is select("Yes", NUMEMPLOYED, "No", continue). What you could do is to add a 3rd argument to your function which would be the name of the field to go to for review and then use that in the select:

Code: Select all

function CheckStaff (Q1, Q2, alpha fieldToReview)
	if Q1 <> 99 then
		if Q2 > Q1 then
			errmsg ("Number trained is greater than number employed! Do you want to review?") select ("Yes", fieldToReview, "No", continue);
		endif;
	endif;
end;
then in your postproc to call the function you could do:

Code: Select all

PROC NUMEMPLOYED
CheckStaff(NUMTRAINED, NUMEMPLOYED, "NUMEMPLOYED")
or if you want to be fancy:

Code: Select all

PROC NUMEMPLOYED
CheckStaff(NUMTRAINED, NUMEMPLOYED, getsymbol())
Keith Tomlin
Posts: 56
Joined: March 23rd, 2014, 9:30 am

Re: User-defined function: using parameter in error message

Post by Keith Tomlin »

Hi Josh - thank you for your quick reply. Your solution works well.

All the best

Keith
Post Reply