0
votes

I'm following the tutorial on About.com on importing Excel files to Delphi. When I try to compile the file I keep on getting an error message about undeclared identifier. To my knowledge the form is names and available in the project as it should be. I will appreciate any suggestions on fixing this error.

unit sample_map;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Data.DB, Data.Win.ADODB,
  Vcl.AppEvnts, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.DBCtrls, Vcl.StdCtrls,
  UWebGMapsCommon, System.Generics.Collections, UWebGMaps;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Button1: TButton;
    DataSource1: TDataSource;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DBNavigator1: TDBNavigator;
    ApplicationEvents1: TApplicationEvents;
    StatusBar1: TStatusBar;
    DBGrid1: TDBGrid;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ApplicationEvents1Exception(Sender: TObject; E: Exception);

  private
  procedure TForm1.FetchData;
    begin
      StatusBar1.SimpleText:='';
      ConnectToExcel;
      AdoQuery1.Close;
      AdoQuery1.SQL.Text:=Edit2.Text;
        try
          AdoQuery1.Open;
        except
        ShowMessage('Unable to read data from Excel,
                 make sure the query ' + Edit1.Text +
                 ' is meaningful!');
        raise;
    end;
  end;

  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.ApplicationEvents1Exception(Sender: TObject; E: Exception);
begin
  StatusBar1.SimpleText := E.Message;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  openDialog : TOpenDialog;    // Open dialog variable
  strConn : WideString; // Declare wide string for the connection
  FileName: string;

begin
  // Create the open dialog object - assign to our open dialog variable
  openDialog := TOpenDialog.Create(self);

  // Set up the starting directory to be the current one
  openDialog.InitialDir := GetCurrentDir;

  // Only allow existing files to be selected
  openDialog.Options := [ofFileMustExist];

  // Allow only .Excel and .pas files to be selected
  openDialog.Filter :=
    'Excel 2003|*.xls|Excel 2007 and older|*.xlsx';

  // Select pascal files as the starting filter type
  openDialog.FilterIndex := 2;

  // Procedure to read the Excel file
  FileName := '';
  if PromptForFileName(FileName,                          // Chosen filename holder
                      'Excel 2003 and older|*.xls|Excel 2007 and older|*.xlsx',  // Filter(s) (optional)
                      '.xlsx',                            // Default extension (opt)
                      'Choose file',                     // Dialog title (opt)
                      GetCurrentDir,                     // Initial dir (opt)
                      False) then                        // Is it a save dlg? (opt)
  begin
    strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
               'Data Source=' + FileName + ';' +
               'Extended Properties=Excel 8.0;';
    // Connect the Excel file
    AdoConnection1.Connected:=False;
    AdoConnection1.ConnectionString:=strConn;

    // Insert the file name to the dialog box and so forth
    Edit1.Text := FileName;

    // Ad worksheets to the combo box

  end
  else
    ShowMessage('Dialog cancelled.');

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AdoConnection1.LoginPrompt := False;
  AdoQuery1.Connection       := AdoConnection1;
  DataSource1.DataSet        := AdoQuery1;
  DBGrid1.DataSource         := DataSource1;
  DBNavigator1.DataSource    := DataSource1;


end;

end.
2
What identifier is the compiler is saying is undeclared? - MartynA
Apparently it's TForm1 - Konrad
It's procedure TForm1.FetchData;, thanks :) - Konrad

2 Answers

3
votes

You have placed the implementation of TForm1.FetchData in the wrong place. It cannot be placed in the class declaration. It must be in the implementation section of the unit. Your code should look like this:

interface

....

type
  TForm1 = class(TForm)
    .... // IDE fields here
  private
    procedure FetchData;
  end;

....

implementation

....

procedure TForm1.FetchData;
begin
  .... body of function here
end;

The tutorial that you link to contains a complete unit. I suggest that you compare the code in that complete unit with the code that you have produced.

1
votes

You have included the implementation of a method in the interface section. Correct the interface section so it reads as follows :-

Private
  Procedure FetchData;
Public
End;

Then move the rest of the code down to the implementation section like this :-

  procedure TForm1.FetchData;
    begin
      StatusBar1.SimpleText:='';
      ConnectToExcel;
      AdoQuery1.Close;
      AdoQuery1.SQL.Text:=Edit2.Text;
        try
          AdoQuery1.Open;
        except
        ShowMessage('Unable to read data from Excel,
                 make sure the query ' + Edit1.Text +
                 ' is meaningful!');
        raise;
    end;
  end;