insert records from an excel file to a DB MS access delphi

1

I'm doing a program that imports Excel into the database, This is my code:

var
importdir, ipo: string;
begin
if OpenDialog1.Execute then
begin
importdir := ExtractFileDir( OpenDialog1.FileName );
ipo := OpenDialog1.FileName;
end;
ADOConnection1.Execute( 'insert into archivo select * from [Excel 8.0;  database='+ipo+'].hoja1' );
end;

When I select the file I get this error

  

The Microsoft Jet could not find the object 'leaf1' Make sure the object exists and the path is correct.

    
asked by pablo 11.09.2018 в 01:22
source

2 answers

0

Given that the connections with ADO must be different to connect to Excel and MSAccess (dodo not say that I assume you are doing it with ADO), I think the solution is to insert the records one by one. Create 2 connections one for Excel and one for Access.

  

Provider = Microsoft.Jet.OLEDB.4.0; Password=""; Data Source =. \ DataExcel.xls; Extended Properties="Excel 8.0; HDR = Yes; IMEX = 1"; Persist Security Info = True

.

  

Provider = Microsoft.Jet.OLEDB.4.0; Password=""; Data Source = .DataAccess.mdb; Persist Security Info = True

From there, it's about reading records using an ADOQuery / ADOTable from one connection and inserting them using the other.

    
answered by 12.09.2018 в 12:47
0

As a general rule, when you make queries using the JetEngine against Excel, the sheet names are followed by a dollar symbol, so it will probably work for you just by changing sheet1 by sheet1 $, something like this:

begin
  if OpenDialog1.Execute then
    ADOConnection1.Execute( 'insert into archivo select * from [Excel 8.0;  database=' + OpenDialog1.FileName + '].hoja1$');
end;

Greetings.

    
answered by 13.09.2018 в 15:45