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!

Calling Functions from DataModule (Help pls) 1

Status
Not open for further replies.

TheHelmsman

Programmer
Apr 25, 2003
5
0
0
IL
Greetings!

I am start using DataModule to hold all non-visual components separatly from the main form.

Here is my problem:

I have wrote a function at my main form [mainForm] which uses some checkbox states (which are phisycally located on the same form).

Then i have a menu compoonent (non-visual) which is in my data module [dataModule]. When i am going to write a code for the onClick event for some menu item i am going to write it inside the data module. Now, how can i call the function whis is in my main form [mainForm] from the data module [dataModule]???

Every time i get the error on the compilation stage.

Sincerely The Helmsman.
 
you can put the Mainform in the USES clause of the data module (USES clause in implementation section)

then you can access any functions or procedures from datamodule by saying MAINFORMNAME.PROCEDURE NAME

for eg Mymainform.cleardata;
 
Nope it doesn't work :-( I have tried it before. Here is a listing of the both forms:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, TabNotBk, Grids, StdCtrls, Buttons, settingsFrameSet;

type
TmainForm = class(TForm)
TabbedNotebook1: TTabbedNotebook;
Panel1: TPanel;
TreeView1: TTreeView;
Splitter1: TSplitter;
TabbedNotebook2: TTabbedNotebook;
Panel2: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
ListBox1: TListBox;
RichEdit1: TRichEdit;
StatusBar1: TStatusBar;
Panel3: TPanel;
Panel4: TPanel;
Label12: TLabel;
Label11: TLabel;
Label10: TLabel;
Label9: TLabel;
Edit5: TEdit;
Label13: TLabel;
Label14: TLabel;
Label15: TLabel;
Label16: TLabel;
Edit6: TEdit;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Panel5: TPanel;
settingsFrame1: TsettingsFrame;
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
mainForm: TmainForm;

implementation

uses pswdDataModule;

{$R *.dfm}

{ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
random password generator | advanced version
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ }
function RandomPwd(): string;
var
// defining variables for future use
useNum, useLet, useSpec : TCheckBoxState;
strTable : String;
pwdLen : Integer;
N, K, X, Y: Integer;

begin
useLet := mainForm.settingsFrame1.CheckBox1.State;
useNum := mainForm.settingsFrame1.CheckBox2.State;
useSpec := mainForm.settingsFrame1.CheckBox3.State;
pwdLen := StrToInt(mainForm.settingsFrame1.ComboBox1.Text);

if (useLet = cbChecked) then
strTable := '0123456789';
if (useNum = cbChecked) then
strTable := strTable + 'ABCDEFGHIJKLMabcdefghijklm' +
'NOPQRSTUVWXYZnopqrstuvwxyz';
if (useSpec = cbChecked) then
strTable := strTable + '!#$%&/()=?@<>|{[]}\*~+#;:.-_';

if (pwdLen > Length(strTable)) then K := Length(strTable)-1
else K := pwdLen;
SetLength(result, K); // setting up length of the final line
Y := Length(strTable); // table length for inner cycle
N := 0; // default value of the cycle beginning

while N < K do // cycle for K symbol creation
begin
X := Random(Y) + 1; // taking next random symbol
// checking presence of this symbol in the final line
if (pos(strTable[X], result) = 0) then
begin
inc(N); // symbol not found
Result[N] := strTable[X]; //saving this symbol
end;
end;
end;

end.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
unit pswdDataModule;

interface

uses
SysUtils, Classes, Menus, ImgList, Controls, ExtCtrls, DCPtiger,
DCPsha512, DCPsha256, DCPsha1, DCPripemd160, DCPripemd128, DCPmd5,
DCPmd4, DCPcrypt2, DCPhaval, DCPtwofish, DCPtea, DCPserpent, DCPrijndael,
DCPrc6, DCPrc5, DCPrc4, DCPrc2, DCPmisty1, DCPmars, DCPidea, DCPice,
DCPdes, DCPblockciphers, DCPcast256, DCPcast128, DCPblowfish;

type
TDataModule1 = class(TDataModule)
MainMenu1: TMainMenu;
File1: TMenuItem;
Open1: TMenuItem;
Save1: TMenuItem;
Print1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Management1: TMenuItem;
Help1: TMenuItem;
Help2: TMenuItem;
New1: TMenuItem;
Add1: TMenuItem;
Make1: TMenuItem;
N2: TMenuItem;
Executesite1: TMenuItem;
Stayontop1: TMenuItem;
Kill1: TMenuItem;
Hash1: TMenuItem;
N3: TMenuItem;
Encrypt1: TMenuItem;
Decrypt1: TMenuItem;
About1: TMenuItem;
Helpindex1: TMenuItem;
ImageList1: TImageList;
Timer1: TTimer;
DCP_blowfish1: TDCP_blowfish;
DCP_cast1281: TDCP_cast128;
DCP_cast2561: TDCP_cast256;
DCP_des1: TDCP_des;
DCP_3des1: TDCP_3des;
DCP_ice1: TDCP_ice;
DCP_thinice1: TDCP_thinice;
DCP_ice21: TDCP_ice2;
DCP_idea1: TDCP_idea;
DCP_mars1: TDCP_mars;
DCP_misty11: TDCP_misty1;
DCP_rc21: TDCP_rc2;
DCP_rc41: TDCP_rc4;
DCP_rc51: TDCP_rc5;
DCP_rc61: TDCP_rc6;
DCP_rijndael1: TDCP_rijndael;
DCP_serpent1: TDCP_serpent;
DCP_tea1: TDCP_tea;
DCP_twofish1: TDCP_twofish;
DCP_haval1: TDCP_haval;
DCP_md41: TDCP_md4;
DCP_md51: TDCP_md5;
DCP_ripemd1281: TDCP_ripemd128;
DCP_ripemd1601: TDCP_ripemd160;
DCP_sha11: TDCP_sha1;
DCP_sha2561: TDCP_sha256;
DCP_sha3841: TDCP_sha384;
DCP_sha5121: TDCP_sha512;
DCP_tiger1: TDCP_tiger;
procedure Exit1Click(Sender: TObject);
procedure Make1Click(Sender: TObject);
function RandomPswd(): String;
private
{ Private declarations }
public
{ Public declarations }
end;

var
DataModule1: TDataModule1;

implementation

uses main, ShellApi;

{$R *.dfm}

procedure TDataModule1.Exit1Click(Sender: TObject);
begin
mainForm.Close;
end;

procedure TDataModule1.Make1Click(Sender: TObject);
begin
main.RandomPwd();
end;

end.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I 've got a error :
Undeclared identifier 'RandomPwd()'
and
Unsatisfied forward or external declaration 'TDataModule1.RandomPwd'
ANY IDEAS???
 

shouldnt your procedure be

procedure TDataModule1.Make1Click(Sender: TObject);
begin
mainform.RandomPwd();
end;

instead of

procedure TDataModule1.Make1Click(Sender: TObject);
begin
main.RandomPwd();
end;

 
Nope. Actually the name of the form written at the top: in my case - unit main, but anyway i've tried it: doesn't work - same error . I suppose i need to define a function constructor, but haven't any idea how to do it. :-(

Pre-history: Two years ago I have wrote the program w/o datamodule and everything works perfect until today. Now i'am going to make a new version of the program and because i use the new version of DCP i am going to put about 15..20 non-visual components on the main form. Actually if i'll not find any other solution, i'll do this. At least i'll be sure that everything will still work as usual. The only decision on using DataModule is to make life easy. Now i'am suppose it not :)

Still any help is very appreciated!

Sincerely The Helmsman.
 

i read your code now
problem is in main form not datamodule


in the declaration section

add
function RandomPwd(): string;
in your public declaration of main form



and then function below should be written as

function TMainform.RandomPwd(): string;
 
Yo! Now it works!!!! Thanks a lot!
It's really simple. Actually i didn't awaiting to got the answer so quick and easy!!! I've search all Google before to find an answer. Now it's solved!!!

God bless you!

Sincerely The Helmsman.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top