* * *

Author Topic: [SOLVED] DataModule  (Read 786 times)

Landslyde

  • Full Member
  • ***
  • Posts: 163
[SOLVED] DataModule
« on: May 28, 2018, 06:44:59 pm »
I searched for posts abt this but cldn't find anything on it. So that makes me think that it's so simple that everyone just knows how to do it...everyone but me. And that makes me feel kinda stupid.

I have Unit1/Form1 and Unit2/DataModule1. I can't figure out how to access my database components from Unit2/DataModule1 and use them on Unit1/Form1. On Unit1 I'v e added Unit2:
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Unit2

I thought this wld be the right way, but I still can't access anything from Unit2/DataModule1. The DataModule1 has on it: DataSource1, SQLQuery1, SQLTransaction1, and SQLite3Connection1.

I've read tutorial 4 on using the DataModule. But that doesn't go in to deep enough detail for me to be able to understand what I'm doing wrong. I cld use some help here. Thanks.
« Last Edit: May 28, 2018, 08:34:05 pm by Landslyde »
Linux Mint 19
Lazarus 1.8.4
FPC 3.0.4

HeavyUser

  • Full Member
  • ***
  • Posts: 157
Re: DataModule
« Reply #1 on: May 28, 2018, 07:10:05 pm »
How about a description of the problem you are having what exactly "I can't use them" means for you?
In hte mean time here are a couple of things to check.
1) make sure that Form1 and Datamaodule1 are autocreated. see project options\forms
2) make sure that datamodule1 is created before form1
3) open both form1 and datamodule1 in deisgner
4) I usually place the TDatasource in the form, it was designed to be the bridge between dataset and dataconotrols and helps change the underline dataset easily.

Landslyde

  • Full Member
  • ***
  • Posts: 163
Re: DataModule
« Reply #2 on: May 28, 2018, 08:12:10 pm »
Hey HeavyUser:

Quote
How about a description of the problem you are having what exactly "I can't use them" means for you?

Well, I did as you said:
Quote
2) make sure that datamodule1 is created before form1

Now my stuff looks like this: Unit1/DataModule1 and Unit2/Form1

I placed my data components on DataModule1. Then I placed a DBGrid and DBNavigator on Form1. So far so good.

Then I clicked on the DBGrid, went to Events and randomly selected OnMouseEnter, double-clicked that and brought up a new Procedure on Unit2:
Code: Pascal  [Select]
  1. procedure TForm1.DBGrid1MouseEnter(Sender: TObject);
  2. begin
  3.  
  4. end;

Now all I want to do is create a new SELECT query. But the thing is, I can't access SQLQuery1 now, which is a part of Unit1/DataModule1. How do I access Unit1/DataModule1 stuff from Unit2/Form1?
Linux Mint 19
Lazarus 1.8.4
FPC 3.0.4

Landslyde

  • Full Member
  • ***
  • Posts: 163
Re: DataModule
« Reply #3 on: May 28, 2018, 08:33:41 pm »
Got it.

You were right abt creating the DataModule before the form. I mean, I guess that was the issue. Because I can now, after adding Unit1 to Unit2's uses, I can access the data components. I cldn't do that when I was creating the form before the datamodule though. Kinda strange it has to be sequenced that way.

Thanks for your help, HeavyUser.
Linux Mint 19
Lazarus 1.8.4
FPC 3.0.4

HeavyUser

  • Full Member
  • ***
  • Posts: 157
Re: DataModule
« Reply #4 on: May 28, 2018, 10:06:02 pm »
Got it.

You were right abt creating the DataModule before the form. I mean, I guess that was the issue. Because I can now, after adding Unit1 to Unit2's uses, I can access the data components. I cldn't do that when I was creating the form before the datamodule though. Kinda strange it has to be sequenced that way.

Thanks for your help, HeavyUser.
just to make sure, when a say that the datamodule1 should be created before form1 I do not mean that you as a user should create them in that order I mean that after opening the menu "project options" and selecting the forms leaf on the left in the list that shows up with the autocreated forms the datamodule1 must be over the form1, or if you want a code based approach, opening the project1.lpr source code the creation order of the commands should be
Code: Pascal  [Select]
  1.   Application.CreateForm(TDatamodule1, Datamodule1);
  2.   Application.CreateForm(TForm1, Form1);
  3.  
and not
Code: Pascal  [Select]
  1.   Application.CreateForm(TForm1, Form1);
  2.   Application.CreateForm(TDatamodule1, Datamodule1);
  3.  
the second one raise a lot of errors while the first one works 99% of the time.

Now As you have noticed in order to access any units public/published data/controls you need to have that unit in the uses clause of your working unit, this is straight forward but it is better in most cases to have it in the implementation's uses clause not the inrface's one to avoid circular reference problems.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus