January 23, 2013

Enabling floating form designer in Delphi XE3


Delphi XE3 has an interesting feature removed from previous versions: the floating VCL form designer. You can enable it again easily, at your own risk.

While the IDE is not running, launch the registry editor, locate the key  HKEY_CURRENT_USER\Software\Embarcadero\BDS\10.0\Form Design and set the "Embedded Designer" to FALSE.

Note: Don't do that if you use FireMonkey. It's form designer doesn't work when floating.

If you need sometimes to have the floating form designer and sometimes not, you way ask the IDE to load his options from another registry key. Use the "-r MyRegKey" in a new shortcut to BDS.EXE. The first time you launch Delphi with that option, the registry key is created with a copy of the current standard key. You can then change the options without affecting the standard registry key. You can also change packages and so on.


January 21, 2013

Automate Microsoft Office from Delphi



Microsoft Office (Word, Excel and others) are applications which can be fully automated from another application. Everything you can do by hand can also be done programmatically from another application and of course from your Delphi application.

Office API and Delphi components


Microsoft Office exposes his features thru a bunch of interfaces which are made accessible thru Windows COM API.

To ease the automation, Delphi is delivered with non-visual components which are "wrapper" around the underlying COM objects and exposes the same properties, methods and events as the COM interface.

Delphi has several sets of components for working with several Office versions. There are components for Office 2000, Office XP and Office 2010. 2010 version is available from Delphi XE2. Of course there are more Office versions, but don’t worry: Microsoft has carefully made his interfaces upward compatible. For example, if you use the Office 2000 components, you can still automate all Office versions from 2000 to the latest. The only restriction is that you cannot easily use new features if you use the old component.

Actually, it is better to use the older components which are capable of doing what you need to do!

This is because the compatibility in Microsoft office API goes upward and not downward.



Making the components available


Since there are several sets of components, you must make sure the correct version is installed.

For Office 2000 and Office XP, Embarcadero provides pre-built packages. For Office 2010, Embarcadero provides the source code but no package. Don’t worry, it is easy to create the package.

To see the installed packages, launch Delphi and go to the menu / Component / Install Packages. You see a long list of all installed design time packages with a checkbox telling that the package is available or not.

Locate “Microsoft Office 2000 Sample Automation Server Wrapper Components” (Or Office XP) and check the checkbox in front of the one you plan to use. Click OK and verify that your component palette now include a tab “Servers”.

To use Office 2010 components, first uncheck both Office 2000 and Office XP component. Then create a new package: go to menu / file / new / other. On the tree, select Delphi Projects and then on the right pane double click on the package icon.

Save the package in a convenient directory, naming it “Office 2010”. Then in the project manager, right click on Office 2010 and select “Add…”. The file open dialog is showing. Navigate to the directory where Delphi is installed, probably “C:\Program Files (x86)\Embarcadero\RAD Studio\10.0” and then navigate to “OCX\Servers\pas2010”. You’ll find all the files required to support the full Office 2010 suite. You may add all files but for the purpose of this article, only Word2010, Office2010 and VBIDE2010 are strictly required.

Once the files have been added, build and install the package: Right click on Office2010 in Project Explorer and select “Build” and then “Install”. In the process, you’ll be asked to add VCL framework. Accept. Finally, you have Office 2010 components installed and ready to be used. Strangely (Probably a small bug), then components are installed in the “OfficeXP” tab in the component palette and they all have the default icon. Not really a problem.

Using Office Components


As an example, we will create a sample application to insert a sentence at the end of a Word document. This is quick and easy!

Create a new VCL forms application, drop a TWordApplication and a TButton on the form. Then add the code below as the button’s OnClick handler.

The quickest way to locate it is to enter WordApplication in the component palette search tool.

TForm1.Button1Click(Sender: TObject);
begin
  WordApplication1.Connect;
  WordApplication1.Visible := TRUE;
  WordApplication1.Selection.EndOf(wdStory, wdMove);
  WordApplication1.Selection.Text := 'Delphi Rocks !' + #13;
  WordApplication1.Selection.EndOf(wdStory, wdMove);
  WordApplication1.Disconnect;
end;

Compile and run the application. Start Word, making both Word visible and your application. Click the button and see a line is added at the end of the Word document. Magic! You see that your Delphi application is automating Word.

To understand all the features you can do with Word automation, you must consult Microsoft documentation. Unfortunately, this documentation is written for Visual Basic. Don’t worry, whatever the language is, the object model, the functions and properties do not vary. This is only a matter of syntax. The above Delphi code is inspired from sample code provided by Microsoft.

This article is available at:
     http://francois-piette.blogspot.be/2013/01/automate-microsoft-office-from-delphi.html

Update: Delphi XE4 MS-Office components article

Follow me on Twitter
Follow me on LinkedIn
Follow me on Google+
Visit my website: http://www.overbyte.be

January 19, 2013

Using ADO to display database schema


In this article, I will show how to get the database schema and display it in a tree view using the ADO components and Delphi (This applies to other languages as well).

ADO stands for ActiveX Data Object and is a software layer that allows programmers a uniform and comprehensive way of developing applications that can access almost any datastore. ADO is actually using MDAC, Microsoft Data Access Components.

In Delphi, ADO is seen as a set of components named "dbGO". They exist in Delphi since Delphi 5.

Using ADO components, you can access database using MDAC, OLE-DB and ODBC. That is almost all well-known databases such as Oracle, Microsoft SQL Server, Access, InterBase, FireBird and many more.

Within Delphi, it is very easy to use ADO components. They are fully integrated in the VCL and all data-aware components. Delphi online documentation (http://docwiki.embarcadero.com/RADStudio/XE3/en/Working_with_ADO_components_Index) explains how to use those components.

Accessing the database schema


A database schema contain the complete description of a database: tables, fields, columns, indices and more. In most applications, you don’t need to worry about the schema because you already know what’s in your database. You probably created it for your application and you know all the tables, fields, columns you have to access to make your application do what it has to do!

But sometimes, you don’t know the database. For example if you are writing a query tool, or if you are writing a code generator, you have to discover what’s in the database. In such software you need to access the schema.

Discovering the tables


Getting the list of tables of a given database is quite easy. You need a TAdoConnection component to connect to the database and the call GetTableNames to have a string list filled with all table names, with or without system tables according to the last argument.

The code looks like this:

begin
  Tables := TStringList.Create;
  AdoConnection1.Connected := TRUE;
  ADOConnection1.GetTableNames(Tables, FALSE);
  Memo1.Lines := Tables;
  Tables.Free;
end;

The code above assumes that you have used the object inspector to setup a connection string to connect to your database. A connection string can be built and tested using the object inspector by clicking on the ellipsis at the right of the ConnectionString property.
Once you get the tables, you can get the field names using a similar call. GetFieldNames will take a table name and a string list and will fill the string list with the table’s field names. The code looks like this:

begin
  FieldNames := TStringList.Create;
  AdoConnection1.Connected := TRUE;
  ADOConnection1.GetFieldNames('Company', FieldNames);
  Memo1.Lines := FieldNames;
  Tables.Free;
end;

The above code is really simple, isn’t it? But there is a problem: you still don’t know each field data type.

To get hand on the data types and other attributes of each field, we have to use slightly more complex code:

var
  Fields : Recordset;
  FieldLen : Integer;
  Flags : Integer;
  DataType : DataTypeEnum;
begin
  Fields := ADOConnection1.ConnectionObject.OpenSchema(
                     adSchemaColumns,
                     VarArrayOf([Null, Null, ‘Company’]),
                     EmptyParam);
  while not Fields.EOF do begin
    DataType := Fields.Fields['DATA_TYPE'].Value;
    FieldLen := StrToIntDef(VarToStr(
    Fields.Fields['CHARACTER_MAXIMUM_LENGTH'].Value), 0);
    Flags := StrToIntDef(VarToStr(
    Fields.Fields['COLUMN_FLAGS'].Value), 0);
    Fields.MoveNext;
  end;
end;

This code deserves some explanations.

RecordSet is a data type defined in ADOInt unit. As its name implies, it represents a collection of records, like the result set of a query, each record is made of Fields and each fields has a value.

DataTypeEnum is actually an integer. And there is a bunch of constants identifying the data types supported by ADO. You find constants like adInteger and adChar. See ADOInt unit for all definitions.

ConnectionObject is a property of the TAdoConnection object. It is an interface of the underlying ActiveX and it represents the connection to the database. ConnectionObject interface has a method OpenSchema which will create a RecordSet containing a subset of the database schema.

The subset used here is identified by the constant adSchemaColumns which as you can guess represents the columns of a table.

The second parameter, which is Criteria, the sample code passes in an array that limits the returned information to just ‘Company’ table columns. To achieve this result, the array specifies a nonempty value for the TABLE_NAME, which is the third constraint available for adSchemaColumns. (adSchemaColumns has four available constraints: TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, and COLUMN_NAME).

The third parameter is not used here could be a SchemaId.

The RecordSet returned by OpenSchema contains a record per field, with several columns describing the corresponding field. Fields are named “DATA_TYPE”, “COLUMN_NAME” and similar. See Microsoft documentation for further reference: http://msdn.microsoft.com/en-us/library/ee266174(v=bts.10).aspx

There is nothing special in accessing the columns except the value is a variant which could be empty. This is why I wrote lines such as:

 Flags := StrToIntDef(VarToStr(Fields.Fields['COLUMN_FLAGS'].Value), 0);

Fields.Fields['COLUMN_FLAGS'].Value returns the variant, VarToStr convert it to a string, possibly and empty string, and StrToIntDef convert the string to an integer defaulting to zero in case the string doesn’t represents an integer (for example when empty).

An example application


To better illustrate the use of all those functions, I wrote a little sample program which connects to a database and fetch the schema, displaying the result in a TTreeView component.

I kept the user interface fairly simple (See screen dump below). To avoid a long load time, I have loaded the list of tables when the user clicks on the “Load” button. I load the columns data when the user actually expand the table node, and only on the first click. This is really fast, even if the database is large.

I maintain the information related to each tree node into an object attached to each node. The code handles the node according to the attached object type.

Since handling a TTreeView is not the subject of this article, I won’t further describe the code. Interested people can download the full project from my website:  http://www.overbyte.be/arch/Using_ADO_to_display_database_schema.zip



In the next article, I plan to apply this code to a code generator, AKA “Wizard”, which will create a form according to the structure of a database table. Stay tuned!

Source code


The actual complete source code is available from my website here
The article is here.

Suggested readings


Using a custom attribute for data persistence
Writing an iterator for a container
Original method to iterate thru the bits of an integer
Adding properties to a set
Internet Component Suite (ICS)
MidWare multi-tier framework

Follow me on Twitter