• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

Excel - sql to oracle database

Hi,
The sql query work perfectly to query the data from oracle database via Ms Access.
However, the moment i copy the exactly sql query into excel VBA. it turn out not working.
The error message i get is from excel VBA as follows

Run-time error '-2147467259 (80004005)
Automation error
Unspecified error
   

   Dim conn As New ADODB.Connection
    Dim connString
    connString = "DSN=XXX;Uid=XXX;Pwd=XXX"
    
    Dim rsRecords As New ADODB.Recordset
    Dim sqlStr As String
    
 sqlStr = "SELECT TPJ_WO.CUSTOMERPONO, TPJ_SI.SHIPMENTNO, TPJ_SDETAILS.ITEMCODE, TCM_MASTER.ITEMNAME, TPJ_WO.WORKORDERNO, TPJ_WO.ITEMNAME, TPJ_WO.TARGETPOWER, TPJ_SDETAILS.SHIPMENTQUANTITY, TCM_FGMASTER.SHIPPINGTYPE, TPJ_SI.SHIPMENTDATE"

sqlStr = sqlStr & "FROM (((TPJ_SI INNER JOIN TPJ_SDETAILS ON "
sqlStr = sqlStr & "TPJ_SI.SHIPMENTNO = TPJ_SDETAILS.SHIPMENTNO) "
sqlStr = sqlStr & "INNER JOIN TPJ_WO ON TPJ_SDETAILS.LOTNO = TPJ_WO.WORKORDERNO)"
sqlStr = sqlStr & "INNER JOIN TCM_MASTER ON TPJ_SDETAILS.ITEMCODE = TCM_MASTER.ITEMCODE)"
sqlStr = sqlStr & "INNER JOIN TCM_FGMASTER ON TPJ_SDETAILS.ITEMCODE = TCM_FGMASTER.FINALPRODUCTCODE;"
    
    conn.Open connString
    rsRecords.CursorLocation = adUseClient
    rsRecords.Open sqlStr, conn, adOpenForwardOnly, adLockReadOnly

Open in new window



Thank you.
0
meniyk
Asked:
meniyk
  • 5
  • 3
  • 2
2 Solutions
 
PortletPaulfreelancerCommented:
Access requires parentheses within the join structures that the vast majority of databases do not require. This is what your current query looks like re-formatted:
SELECT TPJ_WO.CUSTOMERPONO
	, TPJ_SI.SHIPMENTNO
	, TPJ_SDETAILS.ITEMCODE
	, TCM_MASTER.ITEMNAME
	, TPJ_WO.WORKORDERNO
	, TPJ_WO.ITEMNAME
	, TPJ_WO.TARGETPOWER
	, TPJ_SDETAILS.SHIPMENTQUANTITY
	, TCM_FGMASTER.SHIPPINGTYPE
	, TPJ_SI.SHIPMENTDATE
FROM (
	(
		(
			TPJ_SI INNER JOIN TPJ_SDETAILS ON TPJ_SI.SHIPMENTNO = TPJ_SDETAILS.SHIPMENTNO
			) INNER JOIN TPJ_WO ON TPJ_SDETAILS.LOTNO = TPJ_WO.WORKORDERNO
		) INNER JOIN TCM_MASTER ON TPJ_SDETAILS.ITEMCODE = TCM_MASTER.ITEMCODE
	)
INNER JOIN TCM_FGMASTER ON TPJ_SDETAILS.ITEMCODE = TCM_FGMASTER.FINALPRODUCTCOD;
 

Open in new window

All those parentheses after the word FROM are not required in Oracle (or mssql) and would look like this once they are removed:
SELECT TPJ_WO.CUSTOMERPONO
	, TPJ_SI.SHIPMENTNO
	, TPJ_SDETAILS.ITEMCODE
	, TCM_MASTER.ITEMNAME
	, TPJ_WO.WORKORDERNO
	, TPJ_WO.ITEMNAME
	, TPJ_WO.TARGETPOWER
	, TPJ_SDETAILS.SHIPMENTQUANTITY
	, TCM_FGMASTER.SHIPPINGTYPE
	, TPJ_SI.SHIPMENTDATE
FROM TPJ_SI
INNER JOIN TPJ_SDETAILS ON TPJ_SI.SHIPMENTNO = TPJ_SDETAILS.SHIPMENTNO
INNER JOIN TPJ_WO ON TPJ_SDETAILS.LOTNO = TPJ_WO.WORKORDERNO
INNER JOIN TCM_MASTER ON TPJ_SDETAILS.ITEMCODE = TCM_MASTER.ITEMCODE
INNER JOIN TCM_FGMASTER ON TPJ_SDETAILS.ITEMCODE = TCM_FGMASTER.FINALPRODUCTCOD;

Open in new window

aside from that I can't see any problem in the sql (oh, those redundant parentheses would be ignored by Oracle I believe) - so that error isn't due to the sql itself.
0
 
adriankohwsCommented:
Check your reference. There might be a conflict. Nothing wrong with your coding.

If something like below both exist in your reference, you would have problem.

Reference: Microsoft DAO 3.6 Object Library
Reference: Microsoft ActiveX Data Objects 2.1 Library
0
 
meniykAuthor Commented:
Hi PortletPaul,

Thank you for your response.

I have tried and use dthe query you have constructed. It does not work either.
It's work if i just do a simply query such as "SELECT * FROM TPJ_SDETAILS"

so, i'm not sure it this due to the limited payload in the Excel VBA String Object(i doubt) that send across to network.

I have no clues at all....

Thank you.

Yk
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
meniykAuthor Commented:
Hi Adriankohws,

thank you for your response.
I have checked the reference, only Reference: Microsoft ActiveX Data Objects 2.1 Library  exists.

I have no clues how to resolved this issue. It is kind of hard for me to nail down where is problem. I suspect could it be the limited payload of string in Excel VBA Object or ADODB.Recordset send across network...

Thank you.
regards
YK
0
 
PortletPaulfreelancerCommented:
curious, try this perhaps. I have put back the parentheses within the from clause (even though I believe they are redundant) but also given each table a single letter alias
SELECT W.CUSTOMERPONO, S.SHIPMENTNO, D.ITEMCODE, M.ITEMNAME, W.WORKORDERNO, W.ITEMNAME, W.TARGETPOWER, D.SHIPMENTQUANTITY, F.SHIPPINGTYPE, S.SHIPMENTDATE FROM ((((TPJ_SI S INNER JOIN TPJ_SDETAILS D ON S.SHIPMENTNO = D.SHIPMENTNO) INNER JOIN TPJ_WO W ON D.LOTNO = W.WORKORDERNO) INNER JOIN TCM_MASTER M ON D.ITEMCODE = M.ITEMCODE) INNER JOIN TCM_FGMASTER F ON D.ITEMCODE = F.FINALPRODUCTCOD);

Open in new window

This code looks like this when formatted
0
 
adriankohwsCommented:
Can you try not using joins and using condition to join the columns instead? For example,

Select A.xxx,  B.xxx, C.xxx Where A.xxx = B.xxx and C.yyy = B.yyy

Sometimes it helps.
0
 
meniykAuthor Commented:
Hi PortletPaul,
thank you for your response.
It does not work if i join more than 2 tables onwards. I doubt the queries have problem.
I'm not sure does it due to oracle database or Excel VBA ADODB objects..
I hope i can get some pointers.

Thanks

regards
YK.
0
 
meniykAuthor Commented:
Hi adriankohws,
thank you for your response.

it work, if i just use 2 table join and with condition of where clause.
However, when if i join more than 2 tables onwards, that's where the error msg come..
I'm getting annoyed with the "Excel VBA greeting message" with no clues what is going on...

it seem that there are some limitation as to max table allow join in single queries in VBA excel ADODB object..

it seem that pretty strange to me.. :(
Thanks

regards
YK
0
 
PortletPaulfreelancerCommented:
you can rest assured that Oracle supports more than 2 table joins in a query.
this is absolutely not the constraint.
0
 
meniykAuthor Commented:
Hi PortletPaul & adriankohws,

Manage to solve the issues now.. one of my table's name was wrong as a result not working properly... thank you for both of your time. and the points has rewarded accordinlgy.

Thank you!
best regards
YK
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now