Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default SQL Multiple table joins in VBA

Hi,

I want to be able to merge data from three data tables from my underlying
sql database. My code works when executed as a stand-alone statement fired
directly at the db but in VB it falls down with an error dialog box showing
no error number!

The statement works fine when dealing with two tables but not the third!

For the record here is the statement (spaced for clarity) :-

SELECT
Tickets.Account, Tickets.Qty, Tickets.Contract, Tickets.Currency_Code,
Tickets.Exchange, Tickets.Futures_Date,
Tickets.Premium, Tickets.Strike_Price, Tickets.Expiry_Date, Tickets.PC,
Contracts.Lots_Size, InstrumentCodes.InstrumentCode
FROM Tickets

LEFT OUTER JOIN InstrumentCodes ON InstrumentCodes.Contract =
Tickets.Contract AND InstrumentCodes.Exchange = Tickets.Exchange AND
InstrumentCodes.Currency = Tickets.Currency_Code

INNER JOIN Contracts ON Contracts.Short_Code = Tickets.Contract AND
Contracts.Exchange = Tickets.Exchange AND Contracts.Currency_Code =
Tickets.Currency_Code

WHERE (Tickets.Account = '3MM') AND (Tickets.Contract = 'ZNHG')

ORDER BY Tickets.Account, Tickets.Contract, Tickets.Currency_Code,
Tickets.Exchange, Tickets.Futures_Date

Any thoughts?

Thanks.

James.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default SQL Multiple table joins in VBA

This is a SQL question.
Try:-

SELECT
t.Account, t.Qty, t.Contract, t.Currency_Code,
t.Exchange, t.Futures_Date,
t.Premium, t.Strike_Price, t.Expiry_Date, t.PC,
c.Lots_Size, i.InstrumentCode
FROM Tickets t,InstrumentCodes i , contracts c
where
i.Contract = t.Contract
AND
i.Exchange = t.Exchange
AND
i.Currency = t.Currency_Code
and
c.Short_Code = t.Contract
AND
c.Exchange = t.Exchange
AND
c.Currency_Code = t.Currency_Code

and (Tickets.Account = '3MM') AND (Tickets.Contract
= 'ZNHG')

ORDER BY t.Account, t.Contract, t.Currency_Code,
t.Exchange, t.Futures_Date


Patrick Molloy
Microsoft Excel MVP


-----Original Message-----
Hi,

I want to be able to merge data from three data tables

from my underlying
sql database. My code works when executed as a stand-

alone statement fired
directly at the db but in VB it falls down with an error

dialog box showing
no error number!

The statement works fine when dealing with two tables

but not the third!

For the record here is the statement (spaced for

clarity) :-

SELECT
Tickets.Account, Tickets.Qty, Tickets.Contract,

Tickets.Currency_Code,
Tickets.Exchange, Tickets.Futures_Date,
Tickets.Premium, Tickets.Strike_Price,

Tickets.Expiry_Date, Tickets.PC,
Contracts.Lots_Size, InstrumentCodes.InstrumentCode
FROM Tickets

LEFT OUTER JOIN InstrumentCodes ON

InstrumentCodes.Contract =
Tickets.Contract AND InstrumentCodes.Exchange =

Tickets.Exchange AND
InstrumentCodes.Currency = Tickets.Currency_Code

INNER JOIN Contracts ON Contracts.Short_Code =

Tickets.Contract AND
Contracts.Exchange = Tickets.Exchange AND

Contracts.Currency_Code =
Tickets.Currency_Code

WHERE (Tickets.Account = '3MM') AND (Tickets.Contract

= 'ZNHG')

ORDER BY Tickets.Account, Tickets.Contract,

Tickets.Currency_Code,
Tickets.Exchange, Tickets.Futures_Date

Any thoughts?

Thanks.

James.


.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default SQL Multiple table joins in VBA

Thanks Patrick that works.

How bizarre though as your version is the same as mine just short hand sql
(apart from the inner join which I have tried changing anyway).

Oh well - end of the day it works and i'm very grateful.

Thanks again.

James.

"Patrick Molloy" wrote in message
...
This is a SQL question.
Try:-

SELECT
t.Account, t.Qty, t.Contract, t.Currency_Code,
t.Exchange, t.Futures_Date,
t.Premium, t.Strike_Price, t.Expiry_Date, t.PC,
c.Lots_Size, i.InstrumentCode
FROM Tickets t,InstrumentCodes i , contracts c
where
i.Contract = t.Contract
AND
i.Exchange = t.Exchange
AND
i.Currency = t.Currency_Code
and
c.Short_Code = t.Contract
AND
c.Exchange = t.Exchange
AND
c.Currency_Code = t.Currency_Code

and (Tickets.Account = '3MM') AND (Tickets.Contract
= 'ZNHG')

ORDER BY t.Account, t.Contract, t.Currency_Code,
t.Exchange, t.Futures_Date


Patrick Molloy
Microsoft Excel MVP


-----Original Message-----
Hi,

I want to be able to merge data from three data tables

from my underlying
sql database. My code works when executed as a stand-

alone statement fired
directly at the db but in VB it falls down with an error

dialog box showing
no error number!

The statement works fine when dealing with two tables

but not the third!

For the record here is the statement (spaced for

clarity) :-

SELECT
Tickets.Account, Tickets.Qty, Tickets.Contract,

Tickets.Currency_Code,
Tickets.Exchange, Tickets.Futures_Date,
Tickets.Premium, Tickets.Strike_Price,

Tickets.Expiry_Date, Tickets.PC,
Contracts.Lots_Size, InstrumentCodes.InstrumentCode
FROM Tickets

LEFT OUTER JOIN InstrumentCodes ON

InstrumentCodes.Contract =
Tickets.Contract AND InstrumentCodes.Exchange =

Tickets.Exchange AND
InstrumentCodes.Currency = Tickets.Currency_Code

INNER JOIN Contracts ON Contracts.Short_Code =

Tickets.Contract AND
Contracts.Exchange = Tickets.Exchange AND

Contracts.Currency_Code =
Tickets.Currency_Code

WHERE (Tickets.Account = '3MM') AND (Tickets.Contract

= 'ZNHG')

ORDER BY Tickets.Account, Tickets.Contract,

Tickets.Currency_Code,
Tickets.Exchange, Tickets.Futures_Date

Any thoughts?

Thanks.

James.


.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Query Joins bmmulligan1 Excel Discussion (Misc queries) 1 February 1st 10 07:22 PM
Excel query via ODBC, left joins on multiple tables Todd Excel Discussion (Misc queries) 0 February 26th 09 05:42 PM
MS SQL Server Joins Concept venumadhav g[_2_] Excel Worksheet Functions 1 September 12th 08 06:49 AM
Outer Joins The Rook[_2_] Excel Discussion (Misc queries) 3 March 15th 07 04:20 PM
Columns in Full Outer Joins David Weilmuenster Excel Programming 0 October 3rd 03 04:20 PM


All times are GMT +1. The time now is 10:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"