Solved

Excel - sql to oracle database

Posted on 2013-06-14
10
321 Views
Last Modified: 2013-06-24
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
Comment
Question by:meniyk
  • 5
  • 3
  • 2
10 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39247641
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
 
LVL 10

Expert Comment

by:adriankohws
ID: 39247981
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
 

Author Comment

by:meniyk
ID: 39268842
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
 

Author Comment

by:meniyk
ID: 39268845
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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39269014
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 10

Assisted Solution

by:adriankohws
adriankohws earned 150 total points
ID: 39269388
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
 

Author Comment

by:meniyk
ID: 39270144
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
 

Author Comment

by:meniyk
ID: 39270148
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
 
LVL 48

Accepted Solution

by:
PortletPaul earned 350 total points
ID: 39270159
you can rest assured that Oracle supports more than 2 table joins in a query.
this is absolutely not the constraint.
0
 

Author Comment

by:meniyk
ID: 39273821
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now