Create a new spreadsheet programmatically with SQL query

Shamusvw

New Member
Joined
Feb 1, 2017
Messages
28
Office Version
  1. 365
Platform
  1. Windows
I am wanting to create a new spreadsheet programmatically using Delphi (which I can do).
What I can't do though is figure out how to set up the ODBC connection and insert a query from code. Is it possible?
I initially recorded a macro to see the VBA code, and then tried to duplicate it in Delphi.
This is what I'm trying :

with xls.WorkBooks[1].ListObjects.Add(0, 'ODBC;DSN=MySQL;', xls.WorkSheets[1].Range('$A$1')).QueryTable do
begin
CommandType := 0;
CommandText := 'SELECT 1';
...
end;

My very first error is that a record, object or class type is required on the very first line of this code.
I would appreciate any insght.
Thanks.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
So this is my progress (figured out the CODE tags)
Code:
var
 xls : OLEVariant;
...
begin
 xls.WorkBooks[1].WorkSheets[1].Name := 'Test';
 xls.Worksheets[1].ListObjects.Add;
 ShowMessage(intToStr(xls.Worksheets[1].ListObjects.Count));

This shows it created it successfully. Still trying to assign properties though like the ODBC DSN;
 
Upvote 0

Forum statistics

Threads
1,225,375
Messages
6,184,613
Members
453,247
Latest member
scouterjames

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top