Error creating form in Unit…fmx: FMX datamodules not supported.
Image by Steph - hkhazo.biz.id

Error creating form in Unit…fmx: FMX datamodules not supported.

Posted on

If you’re reading this article, chances are you’re stuck with an frustrating error in Delphi while trying to create a form in a unit that inherits from a TDataModule descendant class. Don’t worry, you’re not alone! This error can be a real showstopper, but fear not, dear reader, for we’re about to dive into the solution.

What’s going on?

The error message “Error creating form in Unit…fmx: FMX datamodules not supported” is quite self-explanatory. When you try to create a form in a unit that inherits from a TDataModule descendant class, the IDE throws this error because FMX (FireMonkey) doesn’t support datamodules.

But why is that? Well, Datamodules are a VCL (Visual Component Library) concept, and FMX is a separate framework designed for cross-platform development. FMX has its own set of components and modules, and datamodules don’t fit into that picture.

The problem with TDataModule descendant classes

TDataModule is a VCL class that provides a way to encapsulate data access and business logic in a separate module. When you create a datamodule, it’s essentially a non-visual component that contains datasets, queries, and other data-related components.

Now, when you create a unit that inherits from a TDataModule descendant class, you’re trying to mix VCL and FMX, which, as we established earlier, don’t play nicely together.

Solution 1: Use a different data access approach

One way to solve this problem is to abandon the datamodule approach and use a different method for data access and business logic. Here are a few alternatives:

  • Use a separate unit for your data access and business logic, without inheriting from TDataModule.
  • Create a framework-independent data access layer using interfaces and implementations.
  • Use a third-party library or framework that provides data access and business logic capabilities.

These approaches require some refactoring, but they’ll allow you to keep your data access and business logic separate from your UI logic, which is a good practice anyway.

Solution 2: Use a hybrid approach

If you’re dead set on using datamodules, you can create a hybrid approach that combines VCL and FMX. This might sound crazy, but hear me out:

1. Create a VCL datamodule for your data access and business logic.

2. Create an FMX unit that contains your form and UI logic.

3. Use interfaces or Messaging components to communicate between the VCL datamodule and the FMX unit.

VCL Datamodule FMX Unit
Data access and business logic UI logic and form
Uses VCL components Uses FMX components

This approach requires some additional plumbing, but it allows you to reuse your existing VCL datamodule code and still create an FMX-based UI.

Code snippets to get you started

To help you get started, here are some code snippets that demonstrate the hybrid approach:

// VCL Datamodule unit
unit uDataModule;

interface

uses
  System.SysUtils, System.Classes, Data.DB, Data.Win.ADODB;

type
  TDataModule = class(TDataModule)
  private
    { Private declarations }
  public
    { Public declarations }
    function GetData: string;
  end;

implementation

{ TDataModule }

function TDataModule.GetData: string;
begin
  // Return some data from your database or whatever
  Result := 'Hello from VCL!';
end;

end.

// FMX Unit
unit uMainForm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, 
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,
  FMX.Dialogs, uDataModule;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  DataModule: TDataModule;
begin
  DataModule := TDataModule.Create(nil);
  try
    // Use the VCL datamodule to get some data
    ShowMessage(DataModule.GetData);
  finally
    DataModule.Free;
  end;
end;

end.

Conclusion

The “Error creating form in Unit…fmx: FMX datamodules not supported” error might seem daunting, but with these solutions, you should be able to work around it. Remember, sometimes it’s better to rethink your approach and use a different data access strategy. However, if you’re stuck with datamodules, the hybrid approach can be a viable option.

By following these instructions and code snippets, you should be able to create a working FMX-based UI that communicates with a VCL datamodule. Happy coding!

Additional resources

If you’re looking for more information on data access and business logic in Delphi, here are some additional resources:

Frequently Asked Question

Stuck with the notorious “Error creating form in Unit…fmx: FMX datamodules not supported” when inheriting from a TDataModule descendant class? Don’t worry, we’ve got you covered!

What is causing this error?

This error occurs when you’re trying to create a form in a unit that inherits from a TDataModule descendant class, which is not supported in FMX applications. FMX uses a different framework for its data modules, so you can’t mix and match VCL and FMX components.

What is the difference between VCL and FMX?

VCL (Visual Component Library) is a component library used for building Windows applications, while FMX (FireMonkey) is a multi-platform, cross-device application framework used for building Windows, macOS, Android, and iOS applications. FMX provides a different set of components and frameworks, which is why you can’t use VCL components in FMX applications.

How can I fix this error?

To fix this error, you need to use the correct data module type for your FMX application. Instead of using a TDataModule descendant class, use a TFmxObject descendant class. This will allow you to create a form in your unit without any issues.

Can I reuse my existing VCL code in an FMX application?

Unfortunately, you can’t reuse your existing VCL code as-is in an FMX application. You’ll need to rewrite or refactor your code to use the FMX components and frameworks. This might require significant changes, but it’s a necessary step to ensure your application works correctly in an FMX environment.

Where can I find more information on FMX development?

You can find a wealth of information on FMX development in the official Embarcadero documentation, as well as online resources like blogs, tutorials, and forums. Additionally, you can consult with experienced FMX developers or take online courses to learn more about FMX-specific best practices and techniques.

Leave a Reply

Your email address will not be published. Required fields are marked *