1. William John Adam Trindade
  2. PowerBuilder
  3. Wednesday, 22 November 2017 14:33 PM UTC

Hi,

Our PB Version : 12.5.2 Build 5652

Windows 7 SP1

Excel 2013

We are trying export data to an existing Excel File (template).

Our code:

string ls_fname   
transaction lt_tran

ls_fname = "C:\Temp\Template.xlsx"

TRY
	lt_tran = create transaction
	// Profile Excel
	lt_tran.DBMS = "OLE DB"
	lt_tran.AutoCommit = TRUE
	lt_tran.DBParm = "PROVIDER='Microsoft.ACE.OLEDB.12.0';DATASOURCE ='" + ls_fname + "';PROVIDERSTRING='Excel 12.0 XML;HDR=NO'"
	connect using lt_tran;
	if lt_tran.sqlcode <> 0 then
		messagebox("Error" , "Erreur de connexion au fichier d'entrée est " + ls_fname + " @ " + lt_tran.sqlerrtext, stopsign!)
		return -1
	end if
	
	ll_row = dw_2.RowCount()


//	Ecrit les donnees de produits dans l'onglet 2
For ll_cpt = 1 to ll_row
	
	ls_select = 'insert into [Products] (F1,F2,F3,F4) (' 
	
	ls_select += "'" + dw_2.Object.produits_code_produit[ ll_cpt ] +"',"
	ls_select += "'" + dw_2.Object.produits_ligne_produit_id[ ll_cpt ] +"',"
	ls_select += "'" + dw_2.Object.produits_nom_produit[ ll_cpt ] +"',"
	ls_select += "'" + dw_2.Object.produit_code_sh_code_hs[ ll_cpt ] +"',"
	ls_select +=  ")"
	EXECUTE IMMEDIATE :ls_select USING lt_tran;
next
FINALLY
	disconnect using lt_tran;
	destroy ld_dst
	destroy lt_tran
END TRY

But we get an error at this line:

connect using lt_tran;

If we watch the content of lt_tran:

sqlerrortext= An error occurred, but error text could not be retrieved due to a failure in the error handler.

sqlcode=-1

sqldbcode=999

The file template is a empty excel file (xlsx) without macros or formulas.

Thank you

Accepted Answer
Ken Guo @Appeon Accepted Answer Pending Moderation
  1. Thursday, 23 November 2017 09:26 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi,

Please make the below changes and try again:

  1. Change the below script:

lt_tran.DBParm = "PROVIDER='Microsoft.ACE.OLEDB.12.0';DATASOURCE ='" + ls_fname + "';PROVIDERSTRING='Excel 12.0 XML;HDR=NO'"

To:

lt_tran.DBParm = "PROVIDER='Microsoft.ACE.OLEDB.12.0',DATASOURCE ='" + ls_fname + "',PROVIDERSTRING='Excel 12.0 XML;HDR=Yes'"

(Remarks:change the two semicolons in red to comma, this makes the ‘connect using lt_tran;’

possible. And please try to set the HDR parameter to YES to better map the fields in Excel).

 

  1. Change the below script:

ls_select = 'insert into [Products] (F1,F2,F3,F4) ('

To:

ls_select = 'insert into [Products$] (F1,F2,F3,F4) Values  ('

(Remarks:Products is the Sheet Name in Excel, and ’$’ should be added in the SQL statement; ’Values’ is a required field for Insert SQL.)

 

  1. Change the below script:

ls_select += "'" + dw_2.Object.produit_code_sh_code_hs[ ll_cpt ] +"',"

To:

ls_select += "'" + dw_2.Object.produit_code_sh_code_hs[ ll_cpt ] +"'"

(Remarks: No full stop is needed in last column of Insert.)

 

  1. According to your Insert SQL, you used the Excel Empty file and a sheet named ‘Products’ is required, and in this ‘Products’ sheet, four columns ’F1’,’F2’,’F3’,’F4’ (without quotes) should be inputted.

 

Regards,

Ken

Comment
  1. William John Adam Trindade
  2. Thursday, 23 November 2017 14:37 PM UTC
Thanks Ken,



 



The problem was really the semicolon instead of a comma. I not tested yet rest of my code. The error blocked me at connection.



 

  1. Helpful
There are no comments made yet.
Michael Kramer Accepted Answer Pending Moderation
  1. Thursday, 23 November 2017 10:19 AM UTC
  2. PowerBuilder
  3. # 1

Your DBParm string needs comma instead of semicolon as separator between PROVIDER, DATASOURCE, and PROVIDERSTRING.

 
FAILS >>
lt_tran.DBParm = "PROVIDER='Microsoft.ACE.OLEDB.12.0';DATASOURCE='C:\Temp\Template.xlsx';PROVIDERSTRING='Excel 12.0 XML;HDR=NO'"
 
Succeeds >>
lt_tran.DBParm = "PROVIDER='Microsoft.ACE.OLEDB.12.0',DATASOURCE='C:\Temp\Template.xlsx',PROVIDERSTRING='Excel 12.0 XML;HDR=NO'"

 

HTH

Comment
  1. William John Adam Trindade
  2. Thursday, 23 November 2017 14:38 PM UTC
Thanks Michael,



 



The problem was really the semicolon instead of a comma.

  1. Helpful
There are no comments made yet.
Eduardo G. Accepted Answer Pending Moderation
  1. Thursday, 23 November 2017 09:12 AM UTC
  2. PowerBuilder
  3. # 2

Try either one of them, and try which one works for you.

lt_tran.DBParm   = "PROVIDER='Microsoft.ACE.OLEDB.12.0', DATASOURCE='" + ls_fname + "',PROVIDERSTRING='Excel 12.0 XML;HDR=NO'"

OR
lt_tran.DBParm   = "PROVIDER='Microsoft.ACE.OLEDB.12.0', DATASOURCE='" + ls_fname + "',PROVIDERSTRING='Excel 12.0; MaxScanRows=0;HDR=Yes;IMEX=1'"

Comment
  1. William John Adam Trindade
  2. Thursday, 23 November 2017 14:35 PM UTC
Thanks Eduardo,



The problem was really the semicolon instead of a comma.

  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.