Hot Topic (More than 10 Replies) Need help with a subroutine (Read 1618 times)
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Need help with a subroutine
Jun 18th, 2008 at 1:35pm
Print Post Print Post  
I have the following code in a number of command buttons:
Code
Select All
// Make sure you're not in the Retrieve Spec

IF @MODE() = 2
THEN @MSGBOX("This button does not work in Search Mode","","")

ELSE

// Tests to be sure an Invoice Number has been assigned
IF @IsBlank(INVOICE_NO_1)
THEN
	{
	NotifyForm(1)
	@MsgBox("The Invoice Number has not been assigned.","Please click ASSIGN INVOICE #","")
	}

ELSE

// Tests to be sure the Invoice has been Coded
IF @IsBlank(CODING)
THEN
	{
	 NotifyForm(1)
	 @MsgBox("This Invoice has not been assigned a job type Code.","","")
	Throwfocus(CODING)
	}

ELSE

// Tests to be sure a Salesman has been assigned
IF @IsBlank(SALESMAN)
THEN
	{
	 NotifyForm(1)
	 @MsgBox("Please assign a Salesman to this Invoice.","","")
	Throwfocus(SALESMAN)
	}

ELSE

// Check to make sure you don't have a positive Credit Memo
IF (PURCHASE_ORDER_NO = "CREDIT MEMO" and INVOICE_TOTAL > 0)
THEN @MsgBox("The total for this credit memo is a positive amount.", "Make it negative or change the P.O. description", "")


ELSE
// Saves the record and then Exits
	{
	Clear(Checkcode)
	NotifyForm(0)
	FormCommit("")
	@Exit
	} 



They're all the same except for the first Mode check and the final Save/Save&Continue/Save&Exit process.  I would like to move the middle four tests into a subroutine, PerformSaveTests(), but I'm not sure how the "ELSE" situations would work.  This doesn't work:
Code
Select All
// Make sure you're not in the Retrieve Spec

IF @MODE() = 2
THEN @MSGBOX("This button does not work in Search Mode","","")

ELSE
PerformSaveTests()

ELSE
// Saves the record and then Exits
	{
	Clear(Checkcode)
	NotifyForm(0)
	FormCommit("")
	@Exit
	} 



The program editor balks at that last ELSE, but if it's not there then the final action will be performed every time.  What am I missing here?  Thanks for any help.
  

**
Captain Infinity
Back to top
IP Logged
 
Ben
Lantica Support
*****
Offline



Posts: 218
Joined: Apr 7th, 2005
Re: Need help with a subroutine
Reply #1 - Jun 18th, 2008 at 2:03pm
Print Post Print Post  
You will need to turn PerformSaveTests() into a function. You will want it to return 0 or 1 depending on wheter the record can be saved or not. Information on Functions can be found on page 75 of the 2.0 User Guide.

Below is programming that should fix the problem you were running into. You had one too many Else's in there.

Code
Select All
// Make sure you're not in the Retrieve Spec
IF @MODE() = 2
THEN @MSGBOX("This button does not work in Search Mode","","")

ELSE If PerformSaveTests() = 1 Then
{
    Clear(Checkcode)
    NotifyForm(0)
    FormCommit("")
    @Exit
  }

 

  
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Need help with a subroutine
Reply #2 - Jun 18th, 2008 at 2:59pm
Print Post Print Post  
Thanks Ben.  I'm having some trouble.

Here's my function code:
Code
Select All
// Performs tests before saving the invoice to the database
FUNCTION PerformSaveTests() as Int
Var vRet as Int

// Tests to be sure an Invoice Number has been assigned
IF @IsBlank(INVOICE_NO_1)
THEN
	{
	NotifyForm(1)
	@MsgBox("The Invoice Number has not been assigned.","Please click ASSIGN INVOICE #","")
	}

ELSE

// Tests to be sure the Invoice has been Coded
IF @IsBlank(CODING)
THEN
	{
	 NotifyForm(1)
	 @MsgBox("This Invoice has not been assigned a job type Code.","","")
	Throwfocus(CODING)
	}

ELSE

// Tests to be sure a Salesman has been assigned
IF @IsBlank(SALESMAN)
THEN
	{
	 NotifyForm(1)
	 @MsgBox("Please assign a Salesman to this Invoice.","","")
	Throwfocus(SALESMAN)
	}

ELSE

// Check to make sure you don't have a positive Credit Memo
IF (PURCHASE_ORDER_NO = "CREDIT MEMO" and INVOICE_TOTAL > 0)
THEN @MsgBox("The total for this credit memo is a positive amount.", "Make it negative or change the P.O. description", "")

Return(vRet)
END FUNCTION 



The element programming is as you've written it.

When any of the error conditions is met, I get a happy little message box, sweet as pie.  However, if none are met (meaning all is good to go) the Save & Exit does not occur, the form just stares back at me.  Help?
  

**
Captain Infinity
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Need help with a subroutine
Reply #3 - Jun 18th, 2008 at 3:06pm
Print Post Print Post  
Maybe
Code
Select All
ELSE If PerformSaveTests() = 0 Then 

?

Yup.  Success!  Thanks for your help Ben, I've cleaned out about 99 lines of code!
  

**
Captain Infinity
Back to top
IP Logged
 
Ben
Lantica Support
*****
Offline



Posts: 218
Joined: Apr 7th, 2005
Re: Need help with a subroutine
Reply #4 - Jun 18th, 2008 at 4:50pm
Print Post Print Post  
The only problem I can see in the programming is that vRet is never set in your function. The way it is currently written would leave vRet = 0 all the time.
  
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Need help with a subroutine
Reply #5 - Jun 18th, 2008 at 4:54pm
Print Post Print Post  
Also, you want to return PerformSaveTests() to a variable, then check the variable value instead of running the function repeatedly.

var vGo as Int

    vGo = PerformSaveTests()
    If vGo = 0
       Do stuff
    Else If vGo = 1
       Do other stuff
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Need help with a subroutine
Reply #6 - Jun 18th, 2008 at 5:16pm
Print Post Print Post  
Thanks Erica, I'll work on that.  But I only want the button to "do" anything if the return is 0, otherwise it should just do nothing.  What do you suggest?

Ben, what should I set it to, and where?
  

**
Captain Infinity
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Need help with a subroutine
Reply #7 - Jun 18th, 2008 at 5:29pm
Print Post Print Post  
Ben, like this?  (untested)
Code
Select All
// Performs tests before saving the invoice to the database
FUNCTION PerformSaveTests() as Int
Var vRet as Int

// Tests to be sure an Invoice Number has been assigned
IF @IsBlank(INVOICE_NO_1)
THEN
	{
	NotifyForm(1)
	vRet = 1
	@MsgBox("The Invoice Number has not been assigned.","Please click ASSIGN INVOICE #","")
	}

ELSE

// Tests to be sure the Invoice has been Coded
IF @IsBlank(CODING)
THEN
	{
	NotifyForm(1)
	vRet = 1
	@MsgBox("This Invoice has not been assigned a job type Code.","","")
	Throwfocus(CODING)
	}

ELSE

// Tests to be sure a Salesman has been assigned
IF @IsBlank(SALESMAN)
THEN
	{
	NotifyForm(1)
	vRet = 1
	@MsgBox("Please assign a Salesman to this Invoice.","","")
	Throwfocus(SALESMAN)
	}

ELSE

// Check to make sure you don't have a positive Credit Memo
IF (PURCHASE_ORDER_NO = "CREDIT MEMO" and INVOICE_TOTAL > 0)
THEN
	{
	NotifyForm(1)
	vRet = 1
	@MsgBox("The total for this credit memo is a positive amount.", "Make it negative or change the P.O. description", "")
	}

ELSE vRet = 0

Return(vRet)
END FUNCTION 

  

**
Captain Infinity
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Need help with a subroutine
Reply #8 - Jun 18th, 2008 at 5:35pm
Print Post Print Post  
Infinity wrote on Jun 18th, 2008 at 5:16pm:
Thanks Erica, I'll work on that.  But I only want the button to "do" anything if the return is 0, otherwise it should just do nothing.

I may have put the different variations on your code together wrong. I thought you were testing the value of Perform SaveTests() twice. If not, never mind.  Smiley
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Need help with a subroutine
Reply #9 - Jun 18th, 2008 at 5:39pm
Print Post Print Post  
Erica, inside the function there are error conditions that set NotifyForm to 1 if met, and in the element, after the function runs, NotifyForm is set to 0 if it was all clean.  So I am applying two outcomes, just in different places.
  

**
Captain Infinity
Back to top
IP Logged
 
Ben
Lantica Support
*****
Offline



Posts: 218
Joined: Apr 7th, 2005
Re: Need help with a subroutine
Reply #10 - Jun 18th, 2008 at 6:54pm
Print Post Print Post  
It looks like what you came up with should work.
  
Back to top
IP Logged