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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I call a function from anoth 1

Status
Not open for further replies.

grwd

Technical User
Jan 20, 2002
20
US
How do I call a function from another Unit
I have an app with a main form and multiple DataModules.
In the Unit for the main form (MAIN), I have a generic function GetNewId (myTable, myFieldName).
Each DataModule is listed in the Uses clause of MAIN.
Main is listed in the implimentation section of each of the DataModule units.
(The uses clauses must be set this way for other purposes)

When I try to call the GetNewId function from i procedure in a DataModule unit, I get the following Error:
[Error] DMPrayer1.pas(203): Undeclared identifier: 'GetNewId'
[Fatal Error] MAIN.PAS(12): Could not compile used unit 'DMPrayer1.pas'

Questions:
1. (Considering the above info), how might I successfully call this function (below) from within a DataModule unit?

2. If is is just a matter of declaring the function,
exactly where the declaration should go

The function in 'main' is as follows:
============begin================
Unit Main;
interface

uses
...;

type
...
end;

var
...;

implementation

uses
...;

{$R *.DFM}

function GetNewId(myTable: TTable; myFieldName: String): Integer;
Var
newId : Integer;
idFieldName : String;
begin
idFieldName := 'NewId';
myTable.Active := True;
try
myTable.Active := True;
myTable.LockTable(ltWriteLock);
myTable.LockTable(ltReadLock);
myTable.Refresh;
myTable.Edit;
newId := myTable.FieldByName(myFieldName).value;
Result := newId; //Get new Id
Inc(newId); //Set next Id
myTable.FieldByName(idFieldName).value := newId;
myTable.Post;
finally
myTable.UnlockTable(ltWriteLock);
myTable.UnlockTable(ltReadLock);
myTable.Refresh;
myTable.Active := False;
end;
end;
...

Tim
tim@tgm.org
 
How are you calling the function?

If you are calling it like:

GetNewId(ttable, string);

try this:

frmMain.GetNewId(ttable, string);

And you will have to decalre the function in Main as public.
 
There is no qualifier on the function name in the implementation section of unit Main. That makes it available to all functions/procedures below it in that unit. To use it in another unit, all you have to do is include a declaration in the interface section of unit Main. That makes it global and can then be called from any unit that uses unit Main.

In other words, just insert this line somewhere above the implementation keyword in unit Main:
Code:
function GetNewId(myTable: TTable; myFieldName: String): Integer;
Then you can simply call it with
Code:
  nResult := GetNewId( tblTable, sFieldName );

 
Zathras,
That did the trick!

I am a minister (not a programmer) programming a significantly large application by myself because I can't seem to find any skilled volunteers to help with it. Thank God for Delphi or it just would not be possible at all.

And let me say thank God for people like you that are at least willing to answer my questions when I run into a brick wall.
Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top