Link to home
Start Free TrialLog in
Avatar of meniyk
meniyk

asked on

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.
Avatar of PortletPaul
PortletPaul
Flag of Australia image

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.
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
Avatar of meniyk
meniyk

ASKER

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
Avatar of meniyk

ASKER

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
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
SOLUTION
Avatar of adriankohws
adriankohws
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of meniyk

ASKER

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.
Avatar of meniyk

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of meniyk

ASKER

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