#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default SQL Code

Hi All,

I hav the follwing code (from KeepITcool) to get data
from two tables (cash & Credit)to my sheet1.

In the third Table (Rev) Tktno, SalRef are key fields and
I want get from that table ItemA, ItemB and ItemC
corresponding to Tktno, so my data will be from both
table cash and credit with additional fields from table
Rev.

both cash and credit Tktno are included in Table "Rev".

How can I modify my following code to get this.

I am very new to SQL (import data) and this is my first
attempt.


Dim sCmd()
Dim i%

ReDim sCmd(8)


sCmd(0) = _
" SELECT " & _
" c.arl, c.Tktno, c.bsp,c.SalRef, c.Issuedate," & _
" c.class, c.Reissueno, c.PassName, c.IRNum, " & _
" c.PubFare, c.ComFare, c.fd, c.A_fd, c.unpaid,c.Rev," & _
" c.tax1, c.tax2, c.tax3, c.slsman, "
sCmd(1) = _
" c.AddColl, c.AttrDscntVal, c.Target, c.Stock, " & _
" c.SpComVal, c.Net2Air, c.TourCode, " & _
" c.Remarks, c.Commissionable, c.prov, c.TicketType"
& vbCrLf
sCmd(2) = _
" FROM abcd.dbo.Cash c" & vbCrLf
sCmd(3) = _
" WHERE (c.bsp<'U') AND (c.Tktno Not Like '%I%')" & _
" AND (c.Issuedate={ts '" & Dt1 & " 00:00:00'} " & _
" AND c.Issuedate<={ts '" & Dt2 & " 00:00:00'})" &
vbCrLf
sCmd(4) = " UNION " & vbCrLf
sCmd(5) = sCmd(0)
sCmd(6) = sCmd(1)
sCmd(7) = Replace(sCmd(2), ".Cash", ".Credit")
sCmd(8) = sCmd(3)

For i = 0 To 8
Debug.Assert Len(sCmd(i)) <= 255
Next
Sheets(sh).Visible = True
Sheets(sh).Select
Range("A1").Select

With Selection.QueryTable
.Connection = _
"ODBC;DRIVER=SQL Server;SERVER=abc.com;UID=User;" & _
"PWD=pwd;APP=Microsoft Office 2003;WSID=USER"
.CommandText = sCmd
.Refresh BackgroundQuery:=False
End With


TIA
Soniya
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default SQL Code

Hi Soniya,

just a guess: do you have to finish your sql-statement (sCmd) with a
semicolon?

arno

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default SQL Code

Try:

SELECT *
FROM CASH A,
(SELECT *
FROM CREDIT B,
REV C
WHERE B.?=C.?
AND B.?=C.?) B
WHERE A.?=B.?

* = all columns. If you want specific collumnd, specify ALIAS.ColName e.g.
A.Tktno etc as required.

The WHERE clause specified the conditions to be satisfield for the table
JOINS. The ? stand for actual column names--replace as necessary.

"Soniya" wrote:

Hi All,

I hav the follwing code (from KeepITcool) to get data
from two tables (cash & Credit)to my sheet1.

In the third Table (Rev) Tktno, SalRef are key fields and
I want get from that table ItemA, ItemB and ItemC
corresponding to Tktno, so my data will be from both
table cash and credit with additional fields from table
Rev.

both cash and credit Tktno are included in Table "Rev".

How can I modify my following code to get this.

I am very new to SQL (import data) and this is my first
attempt.


Dim sCmd()
Dim i%

ReDim sCmd(8)


sCmd(0) = _
" SELECT " & _
" c.arl, c.Tktno, c.bsp,c.SalRef, c.Issuedate," & _
" c.class, c.Reissueno, c.PassName, c.IRNum, " & _
" c.PubFare, c.ComFare, c.fd, c.A_fd, c.unpaid,c.Rev," & _
" c.tax1, c.tax2, c.tax3, c.slsman, "
sCmd(1) = _
" c.AddColl, c.AttrDscntVal, c.Target, c.Stock, " & _
" c.SpComVal, c.Net2Air, c.TourCode, " & _
" c.Remarks, c.Commissionable, c.prov, c.TicketType"
& vbCrLf
sCmd(2) = _
" FROM abcd.dbo.Cash c" & vbCrLf
sCmd(3) = _
" WHERE (c.bsp<'U') AND (c.Tktno Not Like '%I%')" & _
" AND (c.Issuedate={ts '" & Dt1 & " 00:00:00'} " & _
" AND c.Issuedate<={ts '" & Dt2 & " 00:00:00'})" &
vbCrLf
sCmd(4) = " UNION " & vbCrLf
sCmd(5) = sCmd(0)
sCmd(6) = sCmd(1)
sCmd(7) = Replace(sCmd(2), ".Cash", ".Credit")
sCmd(8) = sCmd(3)

For i = 0 To 8
Debug.Assert Len(sCmd(i)) <= 255
Next
Sheets(sh).Visible = True
Sheets(sh).Select
Range("A1").Select

With Selection.QueryTable
.Connection = _
"ODBC;DRIVER=SQL Server;SERVER=abc.com;UID=User;" & _
"PWD=pwd;APP=Microsoft Office 2003;WSID=USER"
.CommandText = sCmd
.Refresh BackgroundQuery:=False
End With


TIA
Soniya

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default SQL Code

"AA2e72E" wrote ...

Try:

SELECT *
FROM CASH A,
(SELECT *
FROM CREDIT B,
REV C
WHERE B.?=C.?
AND B.?=C.?) B
WHERE A.?=B.?

* = all columns. If you want specific collumnd, specify ALIAS.ColName e.g.
A.Tktno etc as required.


I can't see that this solution would work. More than one column in
CREDIT is enough to break your code.

The WHERE clause specified the conditions to be satisfield for the table
JOINS.


Such join syntax has been superceded by SQL-92 INNER JOIN syntax e.g.

SELECT C.MyDataCol
FROM CREDIT B
INNER JOIN
REV C
WHERE ON B.MyKeyCol=C.MyKeyCol

I think even Oracle support this syntax now <g.

Jamie.

--
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default SQL Code

Oops! Make that:

SELECT C.MyDataCol
FROM CREDIT B
INNER JOIN
REV C
ON B.MyKeyCol=C.MyKeyCol

Jamie.

--
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
split post code (zip code) out of cell that includes full address Concord Excel Discussion (Misc queries) 4 October 15th 09 06:59 PM
Code to conditional format all black after date specified in code? wx4usa Excel Discussion (Misc queries) 3 December 26th 08 07:06 PM
Drop Down/List w/Code and Definition, only code entered when selec Spiritdancer Excel Worksheet Functions 2 November 2nd 07 03:57 AM
option buttons run Click code when value is changed via VBA code neonangel Excel Programming 5 July 27th 04 08:32 AM
VBA code delete code but ask for password and unlock VBA protection WashoeJeff Excel Programming 0 January 27th 04 07:07 AM


All times are GMT +1. The time now is 12:14 PM.

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

About Us

"It's about Microsoft Excel"