msQuery: is JOIN command possible in Excel?


G

Guest

I am writting a SQL sentence in msQuery and it says "Impossible to add
(SELECT table", in the next sentence:

SELECT id_User,id_PC,vTime FROM (SELECT 1 AS PC_new.id_User
`c:\PC_office`\PC_new.DBF PC_new UNION ALL SELECT 2 AS PC_old.id_User
`c:\PC_office`\PC_old.DBF PC_old)
WHERE id_User Like 'TED%'

Is it possible to do in Excel? What's wrong in my sentence?
Any idea?
 
Ad

Advertisements

G

Guest

Hi again,

I don't think is a matter of DBF tables... once you have done a connection
between Excel and a table ( I use ADODB) my problem is how to read from two
(or more)tables and then put this query into the excel, ALL THIS MUST BE DONE
IN JUST ONE SQL sentence. This is the point... just in one sql sentence...(in
Excel)

Cheers,
 
N

NickHK

So are you using ADODB or a QueryTable/MSQuery ?

MSQuery does not support UNION (IIRC), but connecting to DBF with ADO
should.

NickHK
 
Ad

Advertisements

G

Guest

I was trying both. My goal is get a VBA SQL sentence like

sSql="SELECT id_User,id_PC,vTime FROM (SELECT 1 AS PC_new.id_User
`c:\PC_office`\PC_new.DBF PC_new UNION ALL SELECT 2 AS
PC_old.id_User `c:\PC_office`\PC_old.DBF PC_old) WHERE id_User Like 'TED%'"

So, read same fields in differents tables to get one query, and then put
that in a Excel sheet.

Any clue?

TIA
 
M

MH

Just tested this in Excel 2003 and MS Query does support UNION queries.

I think it's the syntax of the SQL itself, if Sharon could post her table
structure and what she is trying to achieve then I might be able to help.

MH
 
G

Guest

I was reconsidering my question and I think I was wrong with the use of UNION.
I 'm trying to read records from two identical tables LIN06 and
LIN07(idprod,vdate,price,units) where idprod like '50404%', and put the
result rows say in activesheet().range("a4")

I wrote this new code, but it doesn't work since it gives a syntax error in
FROM clause:

Sub ReadOpe()

'Dim sTab6 As String, sTab7 As String
Dim sSQL As String
Dim cnn As ADODB.Connection

Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=c:\VB\"
.Properties("Extended Properties") = "dBASE 5.0;"
.Open
End With

'sTab6 = "[LIN06]"
'sTab7 = "[LIN07]"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM (SELECT * FROM LIN06, SELECT *
FROM LIN07) WHERE IDPROD LIKE '50404%'"
cnn.Execute sSQL

cnn.Close

End Sub

Is this enough clear?

Thanks for your help.
 
M

MH

I think that the SQL you should be using is:

SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN06
WHERE IDPROD Like '50404%'
UNION ALL SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN07
WHERE IDPROD Like '50404%'

This assumes that you want to select records from both tables where the
field IDPROD (possibly a typo, is this the same field as IPROD?) begins with
"50404".

Hopefully this will give you the rows you require.

MH

sharon said:
I was reconsidering my question and I think I was wrong with the use of
UNION.
I 'm trying to read records from two identical tables LIN06 and
LIN07(idprod,vdate,price,units) where idprod like '50404%', and put the
result rows say in activesheet().range("a4")

I wrote this new code, but it doesn't work since it gives a syntax error
in
FROM clause:

Sub ReadOpe()

'Dim sTab6 As String, sTab7 As String
Dim sSQL As String
Dim cnn As ADODB.Connection

Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=c:\VB\"
.Properties("Extended Properties") = "dBASE 5.0;"
.Open
End With

'sTab6 = "[LIN06]"
'sTab7 = "[LIN07]"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM (SELECT * FROM LIN06, SELECT *
FROM LIN07) WHERE IDPROD LIKE '50404%'"
cnn.Execute sSQL

cnn.Close

End Sub

Is this enough clear?

Thanks for your help.



MH said:
Just tested this in Excel 2003 and MS Query does support UNION queries.

I think it's the syntax of the SQL itself, if Sharon could post her table
structure and what she is trying to achieve then I might be able to help.

MH
 
G

Guest

Thanks for your answer.

I don't really know if it works fine. At least id doesn't give any error.
I can't find out how to populate range with cnn.Execute sSQL because I see
no results.
I thought it should show the sql result...
What I'm missing?

TIA


MH said:
I think that the SQL you should be using is:

SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN06
WHERE IDPROD Like '50404%'
UNION ALL SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN07
WHERE IDPROD Like '50404%'

This assumes that you want to select records from both tables where the
field IDPROD (possibly a typo, is this the same field as IPROD?) begins with
"50404".

Hopefully this will give you the rows you require.

MH

sharon said:
I was reconsidering my question and I think I was wrong with the use of
UNION.
I 'm trying to read records from two identical tables LIN06 and
LIN07(idprod,vdate,price,units) where idprod like '50404%', and put the
result rows say in activesheet().range("a4")

I wrote this new code, but it doesn't work since it gives a syntax error
in
FROM clause:

Sub ReadOpe()

'Dim sTab6 As String, sTab7 As String
Dim sSQL As String
Dim cnn As ADODB.Connection

Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=c:\VB\"
.Properties("Extended Properties") = "dBASE 5.0;"
.Open
End With

'sTab6 = "[LIN06]"
'sTab7 = "[LIN07]"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM (SELECT * FROM LIN06, SELECT *
FROM LIN07) WHERE IDPROD LIKE '50404%'"
cnn.Execute sSQL

cnn.Close

End Sub

Is this enough clear?

Thanks for your help.



MH said:
Just tested this in Excel 2003 and MS Query does support UNION queries.

I think it's the syntax of the SQL itself, if Sharon could post her table
structure and what she is trying to achieve then I might be able to help.

MH

AFAIK MSQuery does not support a UNION.

NickHK

I am writting a SQL sentence in msQuery and it says "Impossible to add
(SELECT table", in the next sentence:

SELECT id_User,id_PC,vTime FROM (SELECT 1 AS PC_new.id_User
`c:\PC_office`\PC_new.DBF PC_new UNION ALL SELECT 2 AS PC_old.id_User
`c:\PC_office`\PC_old.DBF PC_old)
WHERE id_User Like 'TED%'

Is it possible to do in Excel? What's wrong in my sentence?
Any idea?
 
Ad

Advertisements

N

NickHK

You can test your SQL against your data in the DB (Access, MySQL, dBase,
whatever) that you are querying, to see what results you should get.

But as your code does not appear to populate a RecordSet, nothing is
returned to Excel.
Check the help for Recordset and possibly CopyFromRecordSet.

NickHK

sharon said:
Thanks for your answer.

I don't really know if it works fine. At least id doesn't give any error.
I can't find out how to populate range with cnn.Execute sSQL because I see
no results.
I thought it should show the sql result...
What I'm missing?

TIA


MH said:
I think that the SQL you should be using is:

SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN06
WHERE IDPROD Like '50404%'
UNION ALL SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN07
WHERE IDPROD Like '50404%'

This assumes that you want to select records from both tables where the
field IDPROD (possibly a typo, is this the same field as IPROD?) begins with
"50404".

Hopefully this will give you the rows you require.

MH

sharon said:
I was reconsidering my question and I think I was wrong with the use of
UNION.
I 'm trying to read records from two identical tables LIN06 and
LIN07(idprod,vdate,price,units) where idprod like '50404%', and put the
result rows say in activesheet().range("a4")

I wrote this new code, but it doesn't work since it gives a syntax error
in
FROM clause:

Sub ReadOpe()

'Dim sTab6 As String, sTab7 As String
Dim sSQL As String
Dim cnn As ADODB.Connection

Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=c:\VB\"
.Properties("Extended Properties") = "dBASE 5.0;"
.Open
End With

'sTab6 = "[LIN06]"
'sTab7 = "[LIN07]"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM (SELECT * FROM LIN06, SELECT *
FROM LIN07) WHERE IDPROD LIKE '50404%'"
cnn.Execute sSQL

cnn.Close

End Sub

Is this enough clear?

Thanks for your help.



:

Just tested this in Excel 2003 and MS Query does support UNION queries.

I think it's the syntax of the SQL itself, if Sharon could post her table
structure and what she is trying to achieve then I might be able to help.

MH

AFAIK MSQuery does not support a UNION.

NickHK

I am writting a SQL sentence in msQuery and it says "Impossible to add
(SELECT table", in the next sentence:

SELECT id_User,id_PC,vTime FROM (SELECT 1 AS PC_new.id_User
`c:\PC_office`\PC_new.DBF PC_new UNION ALL SELECT 2 AS PC_old.id_User
`c:\PC_office`\PC_old.DBF PC_old)
WHERE id_User Like 'TED%'

Is it possible to do in Excel? What's wrong in my sentence?
Any idea?
 
M

MH

Try using the query I gave you in MSQuery, it works for me.

MH

sharon said:
Thanks for your answer.

I don't really know if it works fine. At least id doesn't give any error.
I can't find out how to populate range with cnn.Execute sSQL because I
see
no results.
I thought it should show the sql result...
What I'm missing?

TIA


MH said:
I think that the SQL you should be using is:

SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN06
WHERE IDPROD Like '50404%'
UNION ALL SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN07
WHERE IDPROD Like '50404%'

This assumes that you want to select records from both tables where the
field IDPROD (possibly a typo, is this the same field as IPROD?) begins
with
"50404".

Hopefully this will give you the rows you require.

MH

sharon said:
I was reconsidering my question and I think I was wrong with the use of
UNION.
I 'm trying to read records from two identical tables LIN06 and
LIN07(idprod,vdate,price,units) where idprod like '50404%', and put the
result rows say in activesheet().range("a4")

I wrote this new code, but it doesn't work since it gives a syntax
error
in
FROM clause:

Sub ReadOpe()

'Dim sTab6 As String, sTab7 As String
Dim sSQL As String
Dim cnn As ADODB.Connection

Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=c:\VB\"
.Properties("Extended Properties") = "dBASE 5.0;"
.Open
End With

'sTab6 = "[LIN06]"
'sTab7 = "[LIN07]"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM (SELECT * FROM LIN06,
SELECT *
FROM LIN07) WHERE IDPROD LIKE '50404%'"
cnn.Execute sSQL

cnn.Close

End Sub

Is this enough clear?

Thanks for your help.



:

Just tested this in Excel 2003 and MS Query does support UNION
queries.

I think it's the syntax of the SQL itself, if Sharon could post her
table
structure and what she is trying to achieve then I might be able to
help.

MH

AFAIK MSQuery does not support a UNION.

NickHK

I am writting a SQL sentence in msQuery and it says "Impossible to
add
(SELECT table", in the next sentence:

SELECT id_User,id_PC,vTime FROM (SELECT 1 AS PC_new.id_User
`c:\PC_office`\PC_new.DBF PC_new UNION ALL SELECT 2 AS
PC_old.id_User
`c:\PC_office`\PC_old.DBF PC_old)
WHERE id_User Like 'TED%'

Is it possible to do in Excel? What's wrong in my sentence?
Any idea?
 
G

Guest

Wow,

It's already working, via MSQquery (which unbelievably works...) but I will
feel more comfortable via VBA and filling a Recordset (which I had supposed
it was filled automatically..)

Thanks for your patience.



MH said:
Try using the query I gave you in MSQuery, it works for me.

MH

sharon said:
Thanks for your answer.

I don't really know if it works fine. At least id doesn't give any error.
I can't find out how to populate range with cnn.Execute sSQL because I
see
no results.
I thought it should show the sql result...
What I'm missing?

TIA


MH said:
I think that the SQL you should be using is:

SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN06
WHERE IDPROD Like '50404%'
UNION ALL SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN07
WHERE IDPROD Like '50404%'

This assumes that you want to select records from both tables where the
field IDPROD (possibly a typo, is this the same field as IPROD?) begins
with
"50404".

Hopefully this will give you the rows you require.

MH

I was reconsidering my question and I think I was wrong with the use of
UNION.
I 'm trying to read records from two identical tables LIN06 and
LIN07(idprod,vdate,price,units) where idprod like '50404%', and put the
result rows say in activesheet().range("a4")

I wrote this new code, but it doesn't work since it gives a syntax
error
in
FROM clause:

Sub ReadOpe()

'Dim sTab6 As String, sTab7 As String
Dim sSQL As String
Dim cnn As ADODB.Connection

Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=c:\VB\"
.Properties("Extended Properties") = "dBASE 5.0;"
.Open
End With

'sTab6 = "[LIN06]"
'sTab7 = "[LIN07]"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM (SELECT * FROM LIN06,
SELECT *
FROM LIN07) WHERE IDPROD LIKE '50404%'"
cnn.Execute sSQL

cnn.Close

End Sub

Is this enough clear?

Thanks for your help.



:

Just tested this in Excel 2003 and MS Query does support UNION
queries.

I think it's the syntax of the SQL itself, if Sharon could post her
table
structure and what she is trying to achieve then I might be able to
help.

MH

AFAIK MSQuery does not support a UNION.

NickHK

I am writting a SQL sentence in msQuery and it says "Impossible to
add
(SELECT table", in the next sentence:

SELECT id_User,id_PC,vTime FROM (SELECT 1 AS PC_new.id_User
`c:\PC_office`\PC_new.DBF PC_new UNION ALL SELECT 2 AS
PC_old.id_User
`c:\PC_office`\PC_old.DBF PC_old)
WHERE id_User Like 'TED%'

Is it possible to do in Excel? What's wrong in my sentence?
Any idea?
 
M

MH

'Try this:

Sub ReadOpe()

Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sSQL As String

cnn.Open "driver={Microsoft dBase Driver (*.dbf)};" & _
"driverid=277;dbq=c:\VB\"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM LIN06.dbf " & _
"WHERE IDPROD Like '50404%' " & _
"UNION ALL SELECT IPROD,VDATE,PRICE,UNITS " & _
"FROM LIN07.dbf WHERE IDPROD Like '50404%'"

rs.Open sSQL, cnn, , , adCmdText

Application.ActiveCell.CopyFromRecordset rs

rs.Close
Set rs = Nothing

cnn.Close
Set cnn = Nothing

End Sub

'If this does not work, post back with any error messages etc.

'MH

sharon said:
Wow,

It's already working, via MSQquery (which unbelievably works...) but I
will
feel more comfortable via VBA and filling a Recordset (which I had
supposed
it was filled automatically..)

Thanks for your patience.



MH said:
Try using the query I gave you in MSQuery, it works for me.

MH

sharon said:
Thanks for your answer.

I don't really know if it works fine. At least id doesn't give any
error.
I can't find out how to populate range with cnn.Execute sSQL because I
see
no results.
I thought it should show the sql result...
What I'm missing?

TIA


:

I think that the SQL you should be using is:

SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN06
WHERE IDPROD Like '50404%'
UNION ALL SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN07
WHERE IDPROD Like '50404%'

This assumes that you want to select records from both tables where
the
field IDPROD (possibly a typo, is this the same field as IPROD?)
begins
with
"50404".

Hopefully this will give you the rows you require.

MH

I was reconsidering my question and I think I was wrong with the use
of
UNION.
I 'm trying to read records from two identical tables LIN06 and
LIN07(idprod,vdate,price,units) where idprod like '50404%', and put
the
result rows say in activesheet().range("a4")

I wrote this new code, but it doesn't work since it gives a syntax
error
in
FROM clause:

Sub ReadOpe()

'Dim sTab6 As String, sTab7 As String
Dim sSQL As String
Dim cnn As ADODB.Connection

Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=c:\VB\"
.Properties("Extended Properties") = "dBASE 5.0;"
.Open
End With

'sTab6 = "[LIN06]"
'sTab7 = "[LIN07]"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM (SELECT * FROM LIN06,
SELECT *
FROM LIN07) WHERE IDPROD LIKE '50404%'"
cnn.Execute sSQL

cnn.Close

End Sub

Is this enough clear?

Thanks for your help.



:

Just tested this in Excel 2003 and MS Query does support UNION
queries.

I think it's the syntax of the SQL itself, if Sharon could post her
table
structure and what she is trying to achieve then I might be able to
help.

MH

AFAIK MSQuery does not support a UNION.

NickHK

I am writting a SQL sentence in msQuery and it says "Impossible
to
add
(SELECT table", in the next sentence:

SELECT id_User,id_PC,vTime FROM (SELECT 1 AS PC_new.id_User
`c:\PC_office`\PC_new.DBF PC_new UNION ALL SELECT 2 AS
PC_old.id_User
`c:\PC_office`\PC_old.DBF PC_old)
WHERE id_User Like 'TED%'

Is it possible to do in Excel? What's wrong in my sentence?
Any idea?
 
Ad

Advertisements

G

Guest

This is what it was looking for!

Great. Thanks a lot.



MH said:
'Try this:

Sub ReadOpe()

Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sSQL As String

cnn.Open "driver={Microsoft dBase Driver (*.dbf)};" & _
"driverid=277;dbq=c:\VB\"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM LIN06.dbf " & _
"WHERE IDPROD Like '50404%' " & _
"UNION ALL SELECT IPROD,VDATE,PRICE,UNITS " & _
"FROM LIN07.dbf WHERE IDPROD Like '50404%'"

rs.Open sSQL, cnn, , , adCmdText

Application.ActiveCell.CopyFromRecordset rs

rs.Close
Set rs = Nothing

cnn.Close
Set cnn = Nothing

End Sub

'If this does not work, post back with any error messages etc.

'MH

sharon said:
Wow,

It's already working, via MSQquery (which unbelievably works...) but I
will
feel more comfortable via VBA and filling a Recordset (which I had
supposed
it was filled automatically..)

Thanks for your patience.



MH said:
Try using the query I gave you in MSQuery, it works for me.

MH

Thanks for your answer.

I don't really know if it works fine. At least id doesn't give any
error.
I can't find out how to populate range with cnn.Execute sSQL because I
see
no results.
I thought it should show the sql result...
What I'm missing?

TIA


:

I think that the SQL you should be using is:

SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN06
WHERE IDPROD Like '50404%'
UNION ALL SELECT IPROD,VDATE,PRICE,UNITS
FROM LIN07
WHERE IDPROD Like '50404%'

This assumes that you want to select records from both tables where
the
field IDPROD (possibly a typo, is this the same field as IPROD?)
begins
with
"50404".

Hopefully this will give you the rows you require.

MH

I was reconsidering my question and I think I was wrong with the use
of
UNION.
I 'm trying to read records from two identical tables LIN06 and
LIN07(idprod,vdate,price,units) where idprod like '50404%', and put
the
result rows say in activesheet().range("a4")

I wrote this new code, but it doesn't work since it gives a syntax
error
in
FROM clause:

Sub ReadOpe()

'Dim sTab6 As String, sTab7 As String
Dim sSQL As String
Dim cnn As ADODB.Connection

Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=c:\VB\"
.Properties("Extended Properties") = "dBASE 5.0;"
.Open
End With

'sTab6 = "[LIN06]"
'sTab7 = "[LIN07]"

sSQL = "SELECT IPROD,VDATE,PRICE,UNITS FROM (SELECT * FROM LIN06,
SELECT *
FROM LIN07) WHERE IDPROD LIKE '50404%'"
cnn.Execute sSQL

cnn.Close

End Sub

Is this enough clear?

Thanks for your help.



:

Just tested this in Excel 2003 and MS Query does support UNION
queries.

I think it's the syntax of the SQL itself, if Sharon could post her
table
structure and what she is trying to achieve then I might be able to
help.

MH

AFAIK MSQuery does not support a UNION.

NickHK

I am writting a SQL sentence in msQuery and it says "Impossible
to
add
(SELECT table", in the next sentence:

SELECT id_User,id_PC,vTime FROM (SELECT 1 AS PC_new.id_User
`c:\PC_office`\PC_new.DBF PC_new UNION ALL SELECT 2 AS
PC_old.id_User
`c:\PC_office`\PC_old.DBF PC_old)
WHERE id_User Like 'TED%'

Is it possible to do in Excel? What's wrong in my sentence?
Any idea?
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top