Link a table in DAO database object in Excel (1 Viewer)

chergh

blah
Local time
Today, 12:45
Joined
Jun 15, 2004
Messages
1,414
I'm doing some stuff in excel and some of it is going to be easier to get results of calculations by using sql rather than coding excel functions.

For some of these calculations I am wanting to use the following sort of sql.

Code:
select stuff from table
where stuff not in (select things from otherTable)

Now the tables in both these instances are excel named ranges which are in two different workbooks. As I can't add multiple workbooks to a single dao database object I am trying to add a link to the second table in the database object. I've got the following code:

Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim tdf As DAO.TableDef

Dim linkpath As String
Dim path As String

path = Workbooks("otd3.xls").path & "\otd3.xls"
linkpath = Workbooks("lead times.xls").path & "\lead times.xls"

Set db = DAO.OpenDatabase(path, False, False, "Excel 8.0")

Set tdf = db.CreateTableDef("LeadList")
tdf.Connect = "Excel 8.0;DATABASE=" & linkpath
tdf.SourceTableName = "Blah"

db.TableDefs.Append tdf

The code however fails on the last line giving me a run time error 3251 "Operation is not supported for thistype of object". anyone know if this is possible and if not got any suggestions on how I can acheive this.

I know could just just create a second database object and then copy the data into the same workbook and then create a named range and then use that in my SQL statement would rather use an alternative if I can.
 

llkhoutx

Registered User.
Local time
Today, 06:45
Joined
Feb 26, 2001
Messages
4,018
Use the TransferSpreadsheet command. It's well documented in Access Help Menu.
 

chergh

blah
Local time
Today, 12:45
Joined
Jun 15, 2004
Messages
1,414
Use the TransferSpreadsheet command. It's well documented in Access Help Menu.

Yeah I know but you've missed what I am saying. This code is within excel and I'm just wanting to use a bit of sql with my excel data. Unofortunatley some of the data is in a different workbook so I was wanting to add that data as a linked table to the database object I am creating within excel, I would also prefer to avoid just copying the data into the workbook.
 

llkhoutx

Registered User.
Local time
Today, 06:45
Joined
Feb 26, 2001
Messages
4,018
Link it wth the File >> External >> Data >> Link Table then brouwse the the Excel file and sheet.
 

chergh

blah
Local time
Today, 12:45
Joined
Jun 15, 2004
Messages
1,414
Your still missing the point that I'm not using Access at all and I'm doing everything within excel.

Anyway I've resigned myself to using a db object to retrieve the info I want, copy it into the open spreadsheet and creating a named range and then just deleting it when I'm finished with it.
 

phonic

Registered User.
Local time
Today, 12:45
Joined
Oct 16, 2008
Messages
21
Hi chergh, did you find a solution to this? I'm trying to do the same thing.
 

chergh

blah
Local time
Today, 12:45
Joined
Jun 15, 2004
Messages
1,414
No it appears you can't link to an excel spreadsheet named range in a DAO database object created in excel. The named range has to be within workbook where the database object is created. So I just import the entire named range into the workbook I am using and then create the named range in the workbook I copied it into.
 

LPurvis

AWF VIP
Local time
Today, 12:45
Joined
Jun 16, 2008
Messages
1,269
If I understand your intent correctly, you've been trying to create linked tables where the database object is actually an Excel workbook?
Although DAO inherently creates Jet objects (especially these days) - only a Jet database can create linked tables as an object.
If there was an actual database involved (even just a blank MDB file that you create and destroy for this express purpose) then you'd be able to create the two linked tables to relevant named ranges on whatever sheet.
Performing whatever you wanted on thise actual linked tables - then deleting or dropping the whole MDB etc.

Though that's a bit cumbersome.
You can, however, use Jet directly to query external objects (i.e. without links).
It's just a question of a properly formatted Jet expression.
Then your query might look something like

SELECT * FROM [Excel 8.0;DATABASE=C:\lead times.xls].[Blah] A
WHERE A.stuff Not In (SELECT things FROM [Excel 8.0;DATABASE=C:\lead times.xls].[Blah2])

Allowing you to perform your calcuations based on that data (though not update it of course).

Cheers.
 

Users who are viewing this thread

Top Bottom