Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you please help me shorten and get rid of repeated routine... 5

Status
Not open for further replies.

Mandy_crw

Programmer
Jul 23, 2020
578
0
16
PH
PROCEDURE CmdWrite.click()

LOCAL lclScanner as character, limctr as integer

limctr = 0

IF this.Parent.text5.value = 1

SCAN NOOPTIMIZE FOR ALLTRIM(tsulat.special) = "S" AND ALLTRIM(this.Parent.CboGrdLvl.value) = ALLTRIM(tsulat.grade)

this.parent.text1.value = TRIM(tsulat.sname) + ", " + TRIM(tsulat.fname)
this.Parent.text2.value = TRIM(tsulat.mobile)
this.Parent.text4.value = TRIM(tsulat.idnum)

*Computations ----------------------------------------------

dyus = 0
paytuit = 0
buks = 0
devisor = 0

TRANSSF = 0
TRANSBA = 0
TRANSBO = 0
TRANSUN = 0
TRANSOT = 0

devide = SUBSTR(DTOC(DATE()),1,2)

IF devide = "09"
devisor = 9
ENDIF

IF devide = "10"
devisor = 8
ENDIF

IF devide = "11"
devisor = 7
ENDIF

IF devide = "12"
devisor = 6
ENDIF

IF devide = "01"
devisor = 5
ENDIF

IF devide = "02"
devisor = 4
ENDIF

IF devide = "03"
devisor = 3
ENDIF

IF devide = "04"
devisor = 2
ENDIF

IF devide = "05"
devisor = 1
ENDIF

IF devide = "06"
devisor = 1
ENDIF

IF devide = "07"
devisor = 1
ENDIF

IF devide = "08"
devisor = 1
ENDIF

SELECT trans

SCAN FOR ALLTRIM(this.Parent.text4.value) = trans.idnum

iden = trans.transcode

DO case

CASE iden = "SF"
TRANSSF = TRANSSF + TRANS.transamt

CASE IDEN = "BA"
TRANSBA = TRANSBA + TRANS.transamt

CASE IDEN = "BO"
TRANSBO = TRANSBO + TRANS.transamt

CASE IDEN = "UN"
TRANSUN = TRANSUN + TRANS.transamt

CASE IDEN = "OT"
TRANSOT = TRANSOT + TRANS.transamt

ENDCASE

ENDSCAN

SELECT tsulat

*Computations ----------------------------------------------

twesyon = ((tsulat.tuition - TRANSSF)/devisor)
hapbuks = ((tsulat.books - TRANSBO)/devisor)
hapback = ((tsulat.baccount - TRANSBA)/devisor)
hapuni = ((tsulat.uniform - TRANSUN)/devisor)
hapot = ((tsulat.others - TRANSOT)/devisor)
dyus = twesyon + hapbuks + hapback + hapuni + hapot
todyus = (tsulat.tuition + tsulat.baccount + tsulat.books + tsulat.uniform + tsulat.others) - (transsf + transba + transbo + transun + transot)

*Recording or Replacemnt ----------------------------------

REPLACE tsulat.pbaccnt WITH transba
REPLACE tsulat.ptuition WITH transsf
REPLACE tsulat.puniform WITH transun
REPLACE tsulat.pbooks WITH transbo
REPLACE tsulat.pothers WITH transot

IF ALLTRIM(this.Parent.CboGrdLvl.value) = "F"
this.Parent.text2.value = TRIM(tsulat.adviser)
ELSE
this.Parent.text2.value = TRIM(tsulat.mobile)
ENDIF

*Account Details ------------------------------------------

this.Parent.edit1.Value = this.parent.text1.value + CHR(13) + ;
"AccntDetails:" + CHR(13) +;
"BackAcct.: " + LTRIM(transform(tsulat.baccount,"###,###,###.##"))+ CHR(13) + ;
"SchoolFees: " + LTRIM(transform(tsulat.tuition,"###,###,###.##"))+ CHR(13)+ ;
"Books: " + LTRIM(transform(tsulat.books,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(transform(tsulat.uniform,"###,###,###.##"))+ CHR(13)+;
"Others: " + LTRIM(transform(tsulat.others,"###,###,###.##")) + "."

*Payment Details: ---------------------------------

this.Parent.edit1.Value = "Payment Details:" + CHR(13) + ;
"BackAcct: " + LTRIM(transform(transba,"###,###,###.##"))+ CHR(13) + ;
"SchoolFees: " + LTRIM(transform(transsf,"###,###,###.##"))+ CHR(13)+;
"Books: " + LTRIM(transform(transbo,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(transform(transun,"###,###,###.##"))+CHR(13)+ ;
"Others: " + LTRIM(transform(transot,"###,###,###.##")) + "."

*Amount Due: --------------------------------------

this.Parent.edit1.Value = "Amount due for " + cMONTH(DATE()) + ":" + CHR(13) + ;
"BackAcct: " + LTRIM(transform(hapback,"###,###,###.##")) + CHR(13) + ;
"SchoolFees: " + LTRIM(transform(twesyon,"###,###,###.##")) + CHR(13) + ;
"Books: " + LTRIM(TRANSFORM(hapbuks,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(TRANSFORM(hapuni,"###,###,###.##")) + CHR(13) + ;
"Others: " + LTRIM(transform(hapot,"###,###,###.##")) + CHR(13) + ;
"Total: " + LTRIM(TRANSFORM(dyus,"###,###,###.##")) + CHR(13) + ;
"For reference only!"

limctr = limctr + 1
this.Parent.text3.value = limctr

ENDSCAN

ELSE
SELECT tsulat

SCAN NOOPTIMIZE FOR ALLTRIM(this.Parent.CboGrdLvl.value) = ALLTRIM(tsulat.grade)

this.parent.text1.value = TRIM(tsulat.sname) + ", " + TRIM(tsulat.fname)
this.Parent.text2.value = TRIM(tsulat.mobile)
this.Parent.text4.value = TRIM(tsulat.idnum)

*Computations ----------------------------------------------

dyus = 0
paytuit = 0
buks = 0
devisor = 0

TRANSSF = 0
TRANSBA = 0
TRANSBO = 0
TRANSUN = 0
TRANSOT = 0

devide = SUBSTR(DTOC(DATE()),1,2)

IF devide = "09"
devisor = 9
ENDIF

IF devide = "10"
devisor = 8
ENDIF

IF devide = "11"
devisor = 7
ENDIF

IF devide = "12"
devisor = 6
ENDIF

IF devide = "01"
devisor = 5
ENDIF

IF devide = "02"
devisor = 4
ENDIF

IF devide = "03"
devisor = 3
ENDIF

IF devide = "04"
devisor = 2
ENDIF

IF devide = "05"
devisor = 1
ENDIF

IF devide = "06"
devisor = 1
ENDIF

IF devide = "07"
devisor = 1
ENDIF

IF devide = "08"
devisor = 1
ENDIF

SELECT trans

SCAN FOR ALLTRIM(this.Parent.text4.value) = trans.idnum

iden = trans.transcode

DO case

CASE iden = "SF"
TRANSSF = TRANSSF + TRANS.transamt

CASE IDEN = "BA"
TRANSBA = TRANSBA + TRANS.transamt

CASE IDEN = "BO"
TRANSBO = TRANSBO + TRANS.transamt

CASE IDEN = "UN"
TRANSUN = TRANSUN + TRANS.transamt

CASE IDEN = "OT"
TRANSOT = TRANSOT + TRANS.transamt

ENDCASE

ENDSCAN

SELECT tsulat

*Computations ----------------------------------------------

twesyon = ((tsulat.tuition - TRANSSF)/devisor)
hapbuks = ((tsulat.books - TRANSBO)/devisor)
hapback = ((tsulat.baccount - TRANSBA)/devisor)
hapuni = ((tsulat.uniform - TRANSUN)/devisor)
hapot = ((tsulat.others - TRANSOT)/devisor)
dyus = twesyon + hapbuks + hapback + hapuni + hapot
todyus = (tsulat.tuition + tsulat.baccount + tsulat.books + tsulat.uniform + tsulat.others) - (transsf + transba + transbo + transun + transot)

*Recording or Replacemnt ----------------------------------

REPLACE tsulat.pbaccnt WITH transba
REPLACE tsulat.ptuition WITH transsf
REPLACE tsulat.puniform WITH transun
REPLACE tsulat.pbooks WITH transbo
REPLACE tsulat.pothers WITH transot

IF ALLTRIM(this.Parent.CboGrdLvl.value) = "F"
this.Parent.text2.value = TRIM(tsulat.adviser)
ELSE
this.Parent.text2.value = TRIM(tsulat.mobile)
ENDIF

*Account Details ------------------------------------------

this.Parent.edit1.Value = this.parent.text1.value + CHR(13) + ;
"AccntDetails: " + CHR(13) +;
"BackAcct.: " + LTRIM(transform(tsulat.baccount,"###,###,###.##"))+ CHR(13) + ;
"SchoolFees: " + LTRIM(transform(tsulat.tuition,"###,###,###.##"))+ CHR(13)+ ;
"Books: " + LTRIM(transform(tsulat.books,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(transform(tsulat.uniform,"###,###,###.##"))+ CHR(13)+;
"Others: " + LTRIM(transform(tsulat.others,"###,###,###.##")) + "."

*Payment Details: ---------------------------------

this.Parent.edit1.Value = "Payment Details:" + CHR(13) + ;
"BackAcct: " + LTRIM(transform(transba,"###,###,###.##"))+ CHR(13) + ;
"SchoolFees: " + LTRIM(transform(transsf,"###,###,###.##"))+ CHR(13)+;
"Books: " + LTRIM(transform(transbo,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(transform(transun,"###,###,###.##"))+CHR(13)+ ;
"Others: " + LTRIM(transform(transot,"###,###,###.##")) + "."

*Amount Due: --------------------------------------

this.Parent.edit1.Value = "Amount due for " + cMONTH(DATE()) + ":" + CHR(13) + ;
"BackAcct: " + LTRIM(transform(hapback,"###,###,###.##")) + CHR(13) + ;
"SchoolFees: " + LTRIM(transform(twesyon,"###,###,###.##")) + CHR(13) + ;
"Books: " + LTRIM(TRANSFORM(hapbuks,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(TRANSFORM(hapuni,"###,###,###.##")) + CHR(13) + ;
"Others: " + LTRIM(transform(hapot,"###,###,###.##")) + CHR(13) + ;
"Total: " + LTRIM(TRANSFORM(dyus,"###,###,###.##")) + CHR(13) + ;
"For reference only!"

limctr = limctr + 1
this.Parent.text3.value = limctr

ENDSCAN

ENDIF

IF ALLTRIM(UPPER(this.Parent.cboGrdLvl.value)) = "A"

SCAN

this.parent.text1.value = TRIM(tsulat.sname) + ", " + TRIM(tsulat.fname)
this.Parent.text2.value = TRIM(tsulat.mobile)
this.Parent.text4.value = TRIM(tsulat.idnum)

*Computations ----------------------------------------------

dyus = 0
paytuit = 0
buks = 0
devisor = 0

TRANSSF = 0
TRANSBA = 0
TRANSBO = 0
TRANSUN = 0
TRANSOT = 0

devide = SUBSTR(DTOC(DATE()),1,2)

IF devide = "09"
devisor = 9
ENDIF

IF devide = "10"
devisor = 8
ENDIF

IF devide = "11"
devisor = 7
ENDIF

IF devide = "12"
devisor = 6
ENDIF

IF devide = "01"
devisor = 5
ENDIF

IF devide = "02"
devisor = 4
ENDIF

IF devide = "03"
devisor = 3
ENDIF

IF devide = "04"
devisor = 2
ENDIF

IF devide = "05"
devisor = 1
ENDIF

IF devide = "06"
devisor = 1
ENDIF

IF devide = "07"
devisor = 1
ENDIF

IF devide = "08"
devisor = 1
ENDIF

SELECT trans

SCAN FOR ALLTRIM(this.Parent.text4.value) = trans.idnum

iden = trans.transcode

DO case

CASE iden = "SF"
TRANSSF = TRANSSF + TRANS.transamt

CASE IDEN = "BA"
TRANSBA = TRANSBA + TRANS.transamt

CASE IDEN = "BO"
TRANSBO = TRANSBO + TRANS.transamt

CASE IDEN = "UN"
TRANSUN = TRANSUN + TRANS.transamt

CASE IDEN = "OT"
TRANSOT = TRANSOT + TRANS.transamt

ENDCASE

ENDSCAN

SELECT tsulat

*Computations ----------------------------------------------

twesyon = ((tsulat.tuition - TRANSSF)/devisor)
hapbuks = ((tsulat.books - TRANSBO)/devisor)
hapback = ((tsulat.baccount - TRANSBA)/devisor)
hapuni = ((tsulat.uniform - TRANSUN)/devisor)
hapot = ((tsulat.others - TRANSOT)/devisor)
dyus = twesyon + hapbuks + hapback + hapuni + hapot
todyus = (tsulat.tuition + tsulat.baccount + tsulat.books + tsulat.uniform + tsulat.others) - (transsf + transba + transbo + transun + transot)


*Recording or Replacemnt ----------------------------------

REPLACE tsulat.pbaccnt WITH transba
REPLACE tsulat.ptuition WITH transsf
REPLACE tsulat.puniform WITH transun
REPLACE tsulat.pbooks WITH transbo
REPLACE tsulat.pothers WITH transot

IF ALLTRIM(this.Parent.CboGrdLvl.value) = "F"
this.Parent.text2.value = TRIM(tsulat.adviser)
ELSE
this.Parent.text2.value = TRIM(tsulat.mobile)
ENDIF

*Account Details ------------------------------------------

this.Parent.edit1.Value = this.parent.text1.value + CHR(13) + ;
"AccntDetails: " + CHR(13) +;
"BackAcct.: " + LTRIM(transform(tsulat.baccount,"###,###,###.##"))+ CHR(13) + ;
"SchoolFees: " + LTRIM(transform(tsulat.tuition,"###,###,###.##"))+ CHR(13)+ ;
"Books: " + LTRIM(transform(tsulat.books,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(transform(tsulat.uniform,"###,###,###.##"))+ CHR(13)+;
"Others: " + LTRIM(transform(tsulat.others,"###,###,###.##")) + "."

*Payment Details: ---------------------------------

this.Parent.edit1.Value = "Payment Details:" + CHR(13) + ;
"BackAcct: " + LTRIM(transform(transba,"###,###,###.##"))+ CHR(13) + ;
"SchoolFees: " + LTRIM(transform(transsf,"###,###,###.##"))+ CHR(13)+;
"Books: " + LTRIM(transform(transbo,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(transform(transun,"###,###,###.##"))+CHR(13)+ ;
"Others: " + LTRIM(transform(transot,"###,###,###.##")) + "."

*Amount Due: --------------------------------------

this.Parent.edit1.Value = "Amount due for " + cMONTH(DATE()) + ":" + CHR(13) + ;
"BackAcct: " + LTRIM(transform(hapback,"###,###,###.##")) + CHR(13) + ;
"SchoolFees: " + LTRIM(transform(twesyon,"###,###,###.##")) + CHR(13) + ;
"Books: " + LTRIM(TRANSFORM(hapbuks,"###,###,###.##")) + CHR(13) + ;
"Uniform: " + LTRIM(TRANSFORM(hapuni,"###,###,###.##")) + CHR(13) + ;
"Others: " + LTRIM(transform(hapot,"###,###,###.##")) + CHR(13) + ;
"Total: " + LTRIM(TRANSFORM(dyus,"###,###,###.##")) + CHR(13) + ;
"For reference only!"

limctr = limctr + 1
this.Parent.text3.value = limctr

ENDSCAN

ENDIF

L = MESSAGEBOX("Message sent to" + " " + ALLTRIM(STR(liMCTR))+ " recipient/s!"+ CHR(13)+ "Would you like to another grade?",4+64+256+4096,"SCMS Account Wave!",15000)

DO CASE
CASE L = -1
this.Parent.text4.value = " "
this.Parent.text1.value = " "
this.Parent.text2.value = " "
this.Parent.edit1.Value = " "
thisform.pgfnames.activepage = 9

CASE L = 6
RETURN

CASE L = 7
this.Parent.text4.value = " "
this.Parent.text1.value = " "
this.Parent.text2.value = " "
this.Parent.edit1.Value = " "
thisform.pgfnames.activepage = 9

ENDCASE

ENDPROC
 
Mandy,

There are a few things you could to reduce the amount of code:

1. Read the Help page on ICASE().

2. Where you have many successive IF / ENDIFs, all setting different values to the same variable, replace them all with a single ICASE(). For example: [tt]Dividor = ICASE(devide = "09", 9, ....)[/tt].

3. The whole of the block containing IF [tt]devide = "09" / devisor = 9 ENDIF[/tt] etc. seems to be repeated at least once. Put it into a separate function.

4. Where you have a series of consecutive REPLACEs, all of which update the same table, replace them with a single REPLACE. For example: [tt]REPLACE tsulat.pbaccnt WITH transba, tsulat.ptuition WITH transsf, ...[/tt] etc. This not only saves a bit of code, but will run faster.

I'm sure there are other things you can do. I didn't look closely at all your code. No doubt other forum members will have more energy than I have. But you are asking a lot if you want us to examine all that code in detail.

And it would help greatly if you could PLEASE use the
Code:
 tags when posting large chunks of code like this. This has been pointed out to you more than once before. Please do take note of it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

[url=http://www.ml-consult.co.uk/foxstuff.htm]Visual FoxPro articles, tips and downloads[/url]
 
Here's a start

Code:
DEVISOR = SELECTOR(DEVIDE)

FUNCTION SELECTOR
	PARAMETERS  m.DEVIDE
	PRIVATE  m.DEVIDE, m.DEVISOR
	m.DEVISOR = 1
	DO CASE
		CASE m.DEVIDE = "09"
			m.DEVISOR = 9
		CASE m.DEVIDE = "10"
			m.DEVISOR = 8
		CASE m.DEVIDE = "11"
			m.DEVISOR = 7
		CASE m.DEVIDE = "12"
			m.DEVISOR = 6
		CASE m.DEVIDE = "01"
			m.DEVISOR = 5
		CASE m.DEVIDE = "02"
			m.DEVISOR = 4
		CASE m.DEVIDE = "03"
			m.DEVISOR = 3
		CASE m.DEVIDE = "04"
			m.DEVISOR = 2
	ENDCASE
RETURN(m.DEVISOR)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Mandy,

Or even shorter, an alternate to Griff's function:

Code:
cDev = 'XXXX,04,03,02,01,12,11,10,09'
devisor = IIF(m.devide$m.cDev, 1, AT(m.devide,m.cDev)/3)

Get the idea? HTH.

Steve
 
Steve,

Shouldn't that be:

Code:
cDev = 'XXXX,04,03,02,01,12,11,10,09'
devisor = IIF(!m.devide$m.cDev, 1, AT(m.devide,m.cDev)/3)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
If I had your email... no one would have been any the wiser...

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
For me as a beginner, this shortening was twice as valuable,
because it made me realize right away how important the SET DATE command is in VFP.

In Mandy's program is devide = SUBSTR (DTOC (DATE ()), 1,2)
- and that concerns the american date format (mm / dd / yy) and generates the month number - but for me the German format (dd.mm.yy)
was default in my VFP program


That could result in chaos if I would adopt a code including date calculations without checking it before!

Thanks
Klaus


Peace worldwide - it starts here...
 
To get the parts of a date you have some functions:

Code:
? Year(Date())
? Month(Date())
? Day(Date())

So don't cut out a part of the string format of the date, as Klaus points out, that will fail if the date isn't MDY but DMY or even YMD.
I only looked at the discussion Griff started, not at all that code. Whenever something is redundant, why don't you put it into a function?

But all in all, if it works, don't break it. It might not be easy to maintain, but then I also experienced well-factored code to need special cases all over the place, so a common function needed to have either many special case branches or the whole thing evolved into multiple functions (or methods in OOP code). It's not easy to predict.

Even when now code is duplicated, you might later only need to change the second repeat but keep the first pass as it was. Code will only become more readable and maintainable when you first plan how to divide and conquer the code into functions or classes or modules.

Chriss
 
Thanks for all your input... i'll try to study it well for me to understand... i'm having difficulty understanding suggestions of Steve and Griff but i'm challenged to do it.. I really love VFP 9 programming... thanks everyone...
 
Mandy said:
...but i'm challenged to do it...

Quite a challenge indeed!

Some of us here (including myself) started out in dBase. Then Fox. Then FoxPlus. Then Visual Foxpro. (I may have left some out). Anyway, each step involved learning only the new stuff, not starting from scratch each time. That's why I give you lots of credit for your "catch-up" determination. I'm sure you'll succeed!

Steve
 
Hi,

Code:
devide = SUBSTR(DTOC(DATE()),1,2)

IF devide = "09"
devisor = 9
ENDIF

IF devide = "10"
devisor = 8
ENDIF

IF devide = "11"
devisor = 7
ENDIF

IF devide = "12"
devisor = 6
ENDIF

IF devide = "01"
devisor = 5
ENDIF

IF devide = "02"
devisor = 4
ENDIF

IF devide = "03"
devisor = 3
ENDIF

IF devide = "04"
devisor = 2
ENDIF

IF devide = "05"
devisor = 1
ENDIF

IF devide = "06"
devisor = 1
ENDIF

IF devide = "07"
devisor = 1
ENDIF

IF devide = "08"
devisor = 1
ENDIF

The formula is as simple as below (please see also thread184-1809559)

Code:
iDevisor = ICASE(BETWEEN(MONTH(DATE()), 9, 12), 18 - MONTH(DATE()), BETWEEN(MONTH(DATE()), 1 , 5), 6 - MONTH(DATE()), 1)

hth

MarK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top