Recent

Author Topic: Run a procedure based upon a condition  (Read 2932 times)

J-G

  • Hero Member
  • *****
  • Posts: 966
Run a procedure based upon a condition
« on: January 05, 2019, 07:08:52 pm »
I'm sure this ought to be possible but can't get my head around it.

I've not posted a question for some time since I've been busy with other 'work' but with the new year I find that I need to do some modifications to my accounts program. Although I have a 'Year End' procedure when I wrote that originally I made the mistake of making it only trigger if I ran the program on 1st Jan. Since it is now the 5th, the [Year End] button doesn't show.

Of course I can check that the YE procedure has been run by checking if the new year folder has been created and that works whatever date I first run the program but it would be much more efficient if I could invoke the YE procedure once I've determined that it needs to be run.

Currently I show a button and that has an 'On Click' event so I'm asking if I can cal that same procedure without clicking the mouse?
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Run a procedure based upon a condition
« Reply #1 on: January 05, 2019, 07:42:11 pm »
Currently I show a button and that has an 'On Click' event so I'm asking if I can cal that same procedure without clicking the mouse?

Of course. After all it's nothing but a method of your form class. Say the OnClick handler is called MyButtonClick; then you can do from elsewhere:
Code: Pascal  [Select][+][-]
  1. TForm1.SomeProcedure;
  2. begin
  3.   if ItsJanFiveAndStillNotDone then
  4.     MyButtonClick(nil);
  5.     {or
  6.     MyButtonClick(MyButton);
  7.     or whoever you want to impersonate the Sender
  8.     }
  9. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 7516
Re: Run a procedure based upon a condition
« Reply #2 on: January 06, 2019, 12:14:27 am »
ouht, wait a sec...

Code: Pascal  [Select][+][-]
  1. if itsJanFifthAndStillNotDone then
  2.     Begin
  3.       If Assigned(Button1.OnClick) then Button1.OnClick(Nil) else
  4.         MessageBox('Oh No', 'Did you forget something?',YesNo);
  5.      End;
  6.  
The only true wisdom is knowing you know nothing

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Run a procedure based upon a condition
« Reply #3 on: January 06, 2019, 03:27:35 am »
Thanks Lucamar, I knew there must be a way but I haven't needed the facility previously.

As is always the case, one small change leads to many others !  -  hence the delay in my response  -  as you know, it works flawlessly but I now have an odd corollary; a completely different file from the one I use to check for Year End now returns error 5 - suggesting that it is Read Only on the first time the program tries to read it. There is no problem when it is created during the Year End process  -  ie. it is created then opened correctly but the next time I run the program I get the error.

I've checked to make sure that I haven't forgotten to close the file after reading it so I know it's not that the file is left open. I've even tried closing the file 'on error' but that returned 'file not open'.

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Run a procedure based upon a condition
« Reply #4 on: January 06, 2019, 03:31:36 am »
Hmm ... how are you opening/creating the file the first time?

ouht, wait a sec...
Code: Pascal  [Select][+][-]
  1. if itsJanFifthAndStillNotDone then
  2.     Begin
  3.       If Assigned(Button1.OnClick) then Button1.OnClick(Nil) else
  4.         MessageBox('Oh No', 'Did you forget something?',YesNo);
  5.      End;

A good measure but not needed in this case since one can simply call the handler directly: Button1Click(Nil);
« Last Edit: January 06, 2019, 03:34:41 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Run a procedure based upon a condition
« Reply #5 on: January 06, 2019, 03:53:58 am »
A very simple 

Code: Pascal  [Select][+][-]
  1.   reWrite(YearAdv);
  2.   inc(YearAdvOpenCount);                    // debuging code
  3.  
  4.   write(YearAdv,Next_Inv);
  5.   system.close(YearAdv);
  6.   inc(YearAdvClosCount);                      // debuging code
  7.  
It just holds a single 'Word' which is the next Sales Invoice Number.

Once created (and closed) it is opened, read and closed again as part of the normal data initialization process.

It's been a long day and I was wrong when I stated that this was a different file from the one used to evaluate the year end  - - - -   it is the same file.


FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 966
[Solved] Re: Run a procedure based upon a condition
« Reply #6 on: January 06, 2019, 12:21:23 pm »
Can't give a definitive reason but I changed the file that I used to check for Year End to one which is never normally opened (a .BAK file that is always created during the year end process) and I no longer have the 'Read Only' error.

This is odd because if it is YE, then the file never gets opened anyway because it doesn't exist!  -  I expect reset(f) to return error 3 = 'Path Doesn't exist' which means that the directory & files for the new year haven't been created.

Hey Ho - - - the joys of programming :)

As it happens, this blip led me to realize that I had forgotten to log both a bank payment in and a Purchase Invoice for last year.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Run a procedure based upon a condition
« Reply #7 on: January 06, 2019, 02:11:09 pm »
The Users' Guide has this to say about error five:
Quote
5 File access denied :: Permission to access the file is denied. This error might be caused by one of several reasons:
  • Trying to open for writing a file which is read-only, or which is actually a directory.
  • File is currently locked or used by another process.
  • Trying to create a new file, or directory while a file or directory of the same name already exists.
  • Trying to read from a file which was opened in write-only mode.
  • Trying to write from a file which was opened in read-only mode.
  • Trying to remove a directory or file while it is not possible.
  • No permission to access the file or directory.
Only (rather, one) way I can think of having this error is if by some happenstance your reWrite(YearAdv) is conflicting with a Read(YearAdv) in another place; perhaps a (di)synchronization effect?
« Last Edit: January 06, 2019, 02:12:47 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: Run a procedure based upon a condition
« Reply #8 on: January 06, 2019, 03:48:58 pm »
Thanks for the further input Lucamar, my quick and easy 'goto' resource for error messages is my Turbo Pascal 4 manual  :o  which has a slightly different wording.

Now my problem is solved, and I can use my accounting system again, I doubt that I will do much more research as to the reason for the error. I'm very much an 'If it ain't broke, don't fix it' person  :)
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018