SELECT * FROM EXCEL DOC

I have found this http://delphi.about.com/od/database/l/aa090903a.htm to see how I should connect to a excel doc. I have followed the guide, but when I try to do a SELECT as the guide says I should do. It dosen't work. My first sheet is "Sheet1" and I have the data as the guide.

The select I should use is:
SELECT * FROM [Sheet1$]

But it raise a exception in the FetchData procedure.

Excel 2007 is the format of my document.
procedure TFrmMain.ConnectToExcel;
var strConn :  widestring;
begin
  strConn:='Provider=Microsoft.ACE.OLEDB.12.0;' +
          'Data Source=' + Edit2.Text + ';' +
          'Extended Properties="Excel 12.0 Xml;HDR=YES";Persist Security Info=False';

  AdoConnection1.Connected:=False;
  AdoConnection1.ConnectionString:=strConn;
  try
    AdoConnection1.Open;
    AdoConnection1.GetTableNames(ComboBox1.Items,True);
  except
    ShowMessage('Unable to connect to Excel, make sure the workbook ' + Edit2.Text + ' exist!');
    raise;
  end;
end;(*ConnectToExcel*)

procedure TFrmMain.FetchData;
begin
  StatusBar1.SimpleText:='';

  ConnectToExcel;

  AdoQuery1.Close;
  AdoQuery1.SQL.Text:=Edit3.Text;
  try
    AdoQuery1.Open;
  except
    ShowMessage('Unable to read data from Excel, make sure the query ' + Edit3.Text + ' is meaningful!');
    raise;
  end;
end;

procedure TFrmMain.Button1Click(Sender: TObject);
begin
  FetchData;
end;

Open in new window

QC20NAsked:
Who is Participating?
 
Ferruccio AccalaiConnect With a Mentor Senior developer, analyst and customer assistance Commented:
Sorry, forgot to attach my files
AdoDelphiExcel.zip
0
 
irishbuddhaCommented:
Exactly what exception is being raised?
0
 
QC20NAuthor Commented:
Well, it is in line 28. ADOQuery1.Open is where the exception is raised.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
As the Exception handling code says, are you sure about the query text in the editbox (Edit3)?
0
 
QC20NAuthor Commented:
You see, that is my question. I have just followed the guide in the link and I can't see what the problem is. I was then hoping that someone could inform me if the SELECT command is ok.
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Are you using the example from the delphi.about.com?
Because that test application works right.

However in my test, changing the provider from Jet to ACE I got an error saying "installable ISAM not found".

Is this your case?
0
 
QC20NAuthor Commented:
It runs perfect until I do a ADOQuery1.Open. Do you have Excel 2007? Cause I have found out if you need to connect to excel 2007 your connectionstring should be "Provider=Microsoft.ACE.OLEDB.12.0;"

as this link http://www.connectionstrings.com/excel-2007#p84
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Yes of course.
Vista business Sp2 and Excel 2007

If I use ACE.OLEDB and the .xls file from delphi.about.com I got the ISAM error,
but using a .xlsl file (Excel 2007 version) everything is OK.

What about your xlsl file?
Could you attach your file and code so I can test it in my machine?

0
 
QC20NAuthor Commented:
xlsl - you say? My excelfile is xlsx.

I will attach it.
AdoDelphiExcel.xlsx
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Yes, it was a typo.
Anyway I've tested your attached file also and it's working fine.
Even if the error you got could be different by finding the Sheet1, you already will got back that error.

Are you shure about the location of your file?
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
In addition, I mean that i'm testing also it compiling the application in the same directory where the xlsx file is, writing in Edit2 just the filename without the whole path.
I repeat that it work fine
0
 
QC20NAuthor Commented:
So you are also using:

strConn:='Provider=Microsoft.ACE.OLEDB.12.0;' +
          'Data Source=' + Edit2.Text + ';' +
          'Extended Properties="Excel 12.0 Xml;HDR=YES";Persist Security Info=False';
0
 
QC20NAuthor Commented:
And also using:

SELECT * FROM [Sheet1$]
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Yes of course!
Try it by yourself
0
 
QC20NAuthor Commented:
That is very strange. Yes, it seems to work fine for now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.