When I try to execute this code in my excel VBA script, I get an error saying "invalid Procedure call or argument". Everything looks good to me, so where am I going wrong? Below is the code, thanks!
, Destination:=Range("$A$3")).QueryTable
.CommandText = Array( _
"Select C.SafetyStockQty as SftyStock, C.QtyOnHand as QOH01, G.QtyOnHand as QOHOF,D.QtyOnHand as QOHRDS,E.QtyOnHand as QOHRW ,H.QtyOnHand as QOH02 ,I.QtyOnHand as QOHP1 ,J.QtyOnHand as QOHPDS , F.GrossReqRule as GRR , F.PartCategory as Category ,F.LeadTime " & _
"From CompanyR.dbo.InvWarehouse C " _
, _
"left join CompanyR.dbo.InvWarehouse D on C.StockCode = D.StockCode and D.Warehouse = 'DS' " & _
"left join CompanyR.dbo.InvWarehouse E on C.StockCode = E.StockCode and E.Warehouse = 'RW' " _
, _
"left join CompanyR.dbo.InvWarehouse G on C.StockCode = G.StockCode and G.Warehouse = 'OF' " _
, _
"left join CompanyR.dbo.InvWarehouse H on C.StockCode = H.StockCode and H.Warehouse = '02' " _
, _
"left join CompanyP.dbo.InvWarehouse I on C.StockCode = I.StockCode and I.Warehouse = '01' " _
, _
"left join CompanyP.dbo.InvWarehouse J on C.StockCode = J.StockCode and J.Warehouse = 'DS' " _
, _
"left Join CompanyR.dbo.InvMaster F on C.StockCode = F.StockCode " & _
"where C.StockCode = '" & StckCd & "' And C.Warehouse = '01' " _
)
Related
OBJECTIVE: trying to extract data using union all with join.
CODE:
MYSQL = "SELECT trans_id, item_id, SUM(OrigQty) AS NewOrigQty, SUM(TempQty) AS NewTempQty, " & _
"itm_name, itm_category, itm_details, itm_um, itm_cost " & _
"FROM (" & _
"SELECT T1.trans_id, T1.item_id, T1.itm_qty AS OrigQty, 0 AS TempQty, " & _
"T3.itm_name, T3.itm_category, T3.itm_details, T3.itm_um, T3.itm_cost FROM INVENTORY AS T1 " & _
"LEFT JOIN ITEM AS T3 ON T1.item_id = T3.itm_id " & _
"WHERE T1.trans_id = '" & NewSearchList.lvSearchWindow.SelectedItem.Text & "' " & _
"AND T1.itm_movement = 'TRANSFER IN' AND T1.inv_temporig_status = 'ORIGINAL' " & _
"UNION ALL " & _
"SELECT T2.trans_id, T2.item_id, 0 AS OrigQty, T2.itm_qty AS TempQty, " & _
"t4.itm_name, t4.itm_category, t4.itm_details, t4.itm_um, t4.itm_cost FROM INVENTORY AS T2 " & _
"LEFT JOIN ITEM AS t4 ON T1.item_id = T4.itm_id " & _
"WHERE T2.trans_id = '" & NewSearchList.lvSearchWindow.SelectedItem.Text & "' " & _
"AND T2.itm_movement = 'TRANSFER IN' AND T2.inv_temporig_status = 'TEMPORARY' " & _
") x " & _
"GROUP BY trans_id, item_id, itm_name, itm_category, itm_details, itm_um, itm_cost"
Set rsInventory = dbInventory.Execute(MYSQL)
ERROR: Syntax error in JOIN operation
I want to create a pivot table in the active report to display dbf multiple
files records.
I'm trying to use the following code to create the PIVOT but it's not working, resulting in the error I've shown below.
This is the code:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub openDB()
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open "Provider=VFPOLEDB.1;" & _
"Data Source=D:\Monthly\Colony;" & _
"Collating Sequence=MACHINE"
End Sub
Call openDB
rs.Open "SELECT HEDNAME,DEPCODE,DEPNAME," & _
"ISNULL(A,0) as A, ISNULL(B,0) as B, ISNULL(C,0) as C, ISNULL(G,0) as G " & _
"from(SELECT HED.HEDCODE,HED.HEDNAME,payfil01.DEPCODE,payfil01.DEPNAME,payfil12.PARSSCB," & _
"payfil12.EMPSHFT FROM payfil01 INNER JOIN payfil12 ON payfil01.DEPCODE = payfil12.DEPCODE " & _
"INNER JOIN payfil04 ON payfil01.DEPCODE = payfil04.DEPCODE AND payfil12.EMPTYPE = payfil04.EMPTYPE" & _
" AND payfil12.EMPCODE = payfil04.EMPCODE INNER JOIN payfil05 ON payfil12.DEPCODE = payfil05.DEPCODE" & _
" AND payfil12.EMPTYPE = payfil05.EMPTYPE AND payfil12.EMPCODE = payfil05.EMPCODE Inner Join" & _
"(Select DEPCODE HEDCODE,DEPNAME HEDNAME from payfil01 Where RIGHT(DEPCODE,2) = '00')" & _
" HED On HED.HEDCODE = Left(payfil01.DEPCODE,2) + '-00' where ltrim(rtrim(payfil04.empcode))<>''" & _
" and payfil12.parsscb>0)Final PIVOT ( SUM(PARSSCB) FOR EMPSHFT IN ([A],[B],[C],[G]) )pvt order by HEDCODE asc " & _
", cn, adOpenStatic, adLockReadOnly"
Set SS_Summary.DataControl1.Recordset = rs
SS_Summary.DataControl1.Recordset = rs
SS_Summary.Field35.Text = txtReportPeriod.Text
SS_Summary.Show
SS_Summary.Refresh
End Sub
Moreover it tried to use following query:
SELECT hedname,
depcode,
depname,
Sum(CASE
WHEN [empshft] = 'A' THEN parsscb
ELSE 0
END) AS A,
Sum(CASE
WHEN [empshft] = 'B' THEN parsscb
ELSE 0
END) AS B,
Sum(CASE
WHEN [empshft] = 'C' THEN parsscb
ELSE 0
END) AS C,
Sum(CASE
WHEN [empshft] = 'G' THEN parsscb
ELSE 0
END) AS G
FROM (SELECT HED.hedcode,
HED.hedname,
payfil01.depcode,
payfil01.depname,
payfil12.parsscb,
payfil12.empshft
FROM payfil01
INNER JOIN payfil12
ON payfil01.depcode = payfil12.depcode
INNER JOIN payfil04
ON payfil01.depcode = payfil04.depcode
AND payfil12.emptype = payfil04.emptype
AND payfil12.empcode = payfil04.empcode
INNER JOIN payfil05
ON payfil12.depcode = payfil05.depcode
AND payfil12.emptype = payfil05.emptype
AND payfil12.empcode = payfil05.empcode
INNER JOIN (SELECT depcode HEDCODE,
depname HEDNAME
FROM payfil01
WHERE RIGHT(depcode, 2) = '00') HED
ON HED.hedcode = LEFT(payfil01.depcode, 2) + '-00'
WHERE Ltrim(Rtrim(payfil04.empcode)) <> ''
AND payfil12.parsscb > 0) AS final
GROUP BY hedname,
depcode,
depname
ORDER BY depcode ASC
Screenshot of the error:
It looks like you included some of the Recordset.Open parameters into the SQL string:
rs.Open "SELECT HEDNAME,DEPCODE,DEPNAME," & _
"ISNULL(A,0) as A, ISNULL(B,0) as B, ISNULL(C,0) as C, ISNULL(G,0) as G " & _
"from(SELECT HED.HEDCODE,HED.HEDNAME,payfil01.DEPCODE,payfil01.DEPNAME,payfil12.PARSSCB," & _
"payfil12.EMPSHFT FROM payfil01 INNER JOIN payfil12 ON payfil01.DEPCODE = payfil12.DEPCODE " & _
"INNER JOIN payfil04 ON payfil01.DEPCODE = payfil04.DEPCODE AND payfil12.EMPTYPE = payfil04.EMPTYPE" & _
" AND payfil12.EMPCODE = payfil04.EMPCODE INNER JOIN payfil05 ON payfil12.DEPCODE = payfil05.DEPCODE" & _
" AND payfil12.EMPTYPE = payfil05.EMPTYPE AND payfil12.EMPCODE = payfil05.EMPCODE Inner Join" & _
"(Select DEPCODE HEDCODE,DEPNAME HEDNAME from payfil01 Where RIGHT(DEPCODE,2) = '00')" & _
" HED On HED.HEDCODE = Left(payfil01.DEPCODE,2) + '-00' where ltrim(rtrim(payfil04.empcode))<>''" & _
" and payfil12.parsscb>0)Final PIVOT ( SUM(PARSSCB) FOR EMPSHFT IN ([A],[B],[C],[G]) )pvt order by HEDCODE asc " & _
", cn, adOpenStatic, adLockReadOnly" '<--- THIS PART SHOULD NOT BE IN THE STRING!!!
Note that last line with args for the Open method are accidentally in the string. It should be more like this:
rs.Open "SELECT HEDNAME,DEPCODE,DEPNAME," & _
"ISNULL(A,0) as A, ISNULL(B,0) as B, ISNULL(C,0) as C, ISNULL(G,0) as G " & _
"from(SELECT HED.HEDCODE,HED.HEDNAME,payfil01.DEPCODE,payfil01.DEPNAME,payfil12.PARSSCB," & _
"payfil12.EMPSHFT FROM payfil01 INNER JOIN payfil12 ON payfil01.DEPCODE = payfil12.DEPCODE " & _
"INNER JOIN payfil04 ON payfil01.DEPCODE = payfil04.DEPCODE AND payfil12.EMPTYPE = payfil04.EMPTYPE" & _
" AND payfil12.EMPCODE = payfil04.EMPCODE INNER JOIN payfil05 ON payfil12.DEPCODE = payfil05.DEPCODE" & _
" AND payfil12.EMPTYPE = payfil05.EMPTYPE AND payfil12.EMPCODE = payfil05.EMPCODE Inner Join" & _
"(Select DEPCODE HEDCODE,DEPNAME HEDNAME from payfil01 Where RIGHT(DEPCODE,2) = '00')" & _
" HED On HED.HEDCODE = Left(payfil01.DEPCODE,2) + '-00' where ltrim(rtrim(payfil04.empcode))<>''" & _
" and payfil12.parsscb>0)Final PIVOT ( SUM(PARSSCB) FOR EMPSHFT IN ([A],[B],[C],[G]) )pvt order by HEDCODE asc " & _
, cn, adOpenStatic, adLockReadOnly
I'm reasonably new to SQL and I'm trying to create a string that collects the following:
Code from [Catalogue Info] as c
Description from [Product Information] as p
Weight from p
PPB from p
CP-UK from p
CP-EU from p
1 from an external password protected database C:\mypath\db.accdb as
pl
The code below keeps giving me a Syntax error in From clause. I'm assuming this is something to do with my brackets around the INNER JOINS but I'm not sure.
This code worked perfectly fine before adding in the second INNER JOIN clause (external DB), My WHERE & ORDER BY clauses work fine.
sqlProd = "SELECT c.Code," _
& " p.Description, p.weight, p.[Pack Size], p.PPB, p.[CP-UK], p.[CP-EU]," _
& " pl.1" _
& " FROM ([Catalogue Info] c" _
& " INNER JOIN [Product Information] p" _
& " on c.code = p.code)" _
& " INNER JOIN [;database=C:\mypath\db.accdb;PWD=password123].table_name pl" _
& " on c.code = pl.code" _
& " WHERE c.Sub_Cat_1 = '" & rstSub1!Sub_Cat_1 & "'" _
& " AND c.Sub_Cat_2 = '" & rstSub2!Sub_Cat_2 & "'" _
& " ORDER BY c.Page ASC, c.Page_Position ASC;"
Any thoughts?
Access 2016, Excel 2016, Windows 10
Thanks!
Dom
I believe this should work, as I think you have unnecessary brackets around part of your FROM clause:
sqlProd = "SELECT c.Code," _
& " p.Description, p.weight, p.[Pack Size], p.PPB, p.[CP-UK], p.[CP-EU]," _
& " pl.1" _
& " FROM [Catalogue Info] c" _
& " INNER JOIN [Product Information] p" _
& " on c.code = p.code" _
& " INNER JOIN [;database=C:\mypath\db.accdb;PWD=password123].table_name pl" _
& " on c.code = pl.code" _
& " WHERE c.Sub_Cat_1 = '" & rstSub1!Sub_Cat_1 & "'" _
& " AND c.Sub_Cat_2 = '" & rstSub2!Sub_Cat_2 & "'" _
& " ORDER BY c.Page ASC, c.Page_Position ASC;"
I think I have found a solution to this - it wasn't the bracketing, it was the password I was using.
The password I was using (not in original question) used special characters (#>`{(= which seemed to be giving the syntax error. I changed the password in my Access Database to letters/numbers only and the original code works fine.
Hope this helps anyone else out. Final code:
sqlProd = "SELECT c.Code," _
& " p.Description, p.weight, p.[Pack Size], p.PPB, p.[CP-UK], p.[CP-EU]," _
& " pl.[1]" _
& " FROM [Catalogue Info] as c" _
& " INNER JOIN [Product Information] as p" _
& " on c.code = p.code" _
& " INNER JOIN [;database=C:\mypath\db.accdb;PWD=password123].table_name as pl" _
& " on c.code = pl.code" _
& " WHERE c.Sub_Cat_1 = '" & rstSub1!Sub_Cat_1 & "'" _
& " AND c.Sub_Cat_2 = '" & rstSub2!Sub_Cat_2 & "'" _
& " ORDER BY c.Page ASC, c.Page_Position ASC;"
Thanks for your feedback.
I am using Excel VBA and connecting to PGSQL using OleDB. I have 2 lines of SQL code that work fine on there own, but I now need to merge them.
The 1st query is the master group that finds the people I need for the 2nd query
Const sqlconnection = "Provider=oledb;"
Dim conn As New Connection
conn.ConnectionString = sqlconnection
conn.Open
Dim rs As Recordset
Sheets("Sheet1").Select
Range("A1").Select
Dim GRP As String
GRP = "SELECT h.master_id, p.surname, p.forename1, h.eventdate, h.code " _
& "FROM hist h INNER JOIN person p ON h.master_id=p.entity_id " _
& "AND code LIKE 'C10%' " _
& "ORDER BY h.master_id "
The 2nd query needs to show the results based on the 1st group of people it found.
Dim DATA As String
DATA = "SELECT latest.master_id, p.surname, p.forename1, " _
& "SUBSTRING(latestAP,1,10) eventdate, " _
& "SUBSTRING(latestAP,12,3) TX1, " _
& "SUBSTRING(latestAP,16,3) TX2 " _
& "FROM ( " _
& "SELECT master_id, " _
& "MAX(CAST(eventdate AS VARCHAR(10)) + '.' + RIGHT('00' + TEXT1,3)+ '.' + RIGHT('00' + TEXT2,3)) as latestAP " _
& "FROM ap " _
& "GROUP BY master_id) AS latest " _
& "LEFT JOIN person p ON latest.master_id = p.entity_id " _
& "ORDER BY master_id "
Set rs = conn.Execute(DATA)
With ActiveSheet.QueryTables.Add(Connection:=rs, Destination:=Range("A1"))
.Refresh
End With
rs.Close
Consider adding first query as an additional derived table to second query:
Dim DATA As String
DATA = "SELECT latest.master_id, p.surname, p.forename1, " _
& " SUBSTRING(latestAP,1,10) eventdate, " _
& " SUBSTRING(latestAP,12,3) TX1, " _
& " SUBSTRING(latestAP,16,3) TX2 " _
& "FROM ( " _
& " SELECT master_id, " _
& " MAX(CAST(eventdate AS VARCHAR(10)) + '.' + " _
& " RIGHT('00' + TEXT1,3) + '.' + " _
& " RIGHT('00' + TEXT2,3)) as latestAP " _
& " FROM ap " _
& " GROUP BY master_id) AS latest " _
& "INNER JOIN ( " _
& " SELECT h.master_id, p.surname, p.forename1, "_
& " h.eventdate, h.code " _
& " FROM hist h " _
& " INNER JOIN person p ON h.master_id=p.entity_id " _
& " AND code LIKE 'C10%') AS grp "
& "ON grp.master_id = latest.master_id"
& "LEFT JOIN person p ON latest.master_id = p.entity_id " _
& "ORDER BY master_id "
Please see below code for reference.
I am getting this error on my code below and I already know why, I just don't know how am I going to JOIN the dataset inside SQL.
Error1 Operator '+' is not defined for types 'String' and 'System.Data.DataTable'.
Dim sql3 As String = "select DISTINCT A.CustomerNum, A.CustomerName, A.lngAmkorID AS AmkorID, A.lngAmkorSubID AS SubID, A.strLineCode AS LineCode, A.Device, A.WaferRunNum, A.PkgLd, A.strPO AS PO, A.strWIPLotNumber as WIPLotNo, A.strWIPLotDoubleCheckCode AS WIPDCC, " & _
"J.strOperatorName AS CurrOper, J.strRawDateCode AS eMES_DC, J.strRawTraceCode AS eMES_TC, " & _
"K.ReferenceStnName, " & _
"D.CMMFDT AS MfgDate, " & _
"C.DateCode AS Marked_DC, 'HardcodedDC' = CASE WHEN C.HardCoded <> 0 THEN 'Hardcoded' ELSE '' END," & _
"C.TraceCode as encicledT, 'HardcodedT' = CASE WHEN C.bolTCHardcoded <> 0 THEN 'Hardcoded' ELSE '' END," & _
"C.SCode as encicledU, 'HardcodedU' = CASE WHEN C.bolSCHardcoded <> 0 THEN 'Hardcoded' ELSE '' END," & _
"C.SeqCode as encicledQ, 'HardcodedQ' = CASE WHEN C.bolQCHardcoded <> 0 THEN 'Hardcoded' ELSE '' END," & _
"'Status' = CASE" & _
"WHEN B.Status = 0 THEN 'NEW LOT'" & _
"WHEN B.Status = 1 THEN 'INCOMPLETE MID'" & _
"WHEN B.Status = 2 THEN 'COMPLETE MID'" & _
"WHEN B.Status = 3 THEN 'DC PROCESSED'" & _
"WHEN B.Status = 5 THEN 'MARKED'" & _
"END," & _
"'HOLD' = CASE WHEN B.Hold <> 0 THEN 'ON HOLD' ELSE '' END," & _
"D.CMMKDT AS MarkDate, H.strILNNo AS MESA_ILN, " & _
"J.strStation AS Station, J.strStatus_01 AS Status1, J.strStatus_02 AS Status2, " & _
"'UnitQty' = CASE" & _
"WHEN J.lngShippingOutTotalGoodQuantity <> 0 THEN J.lngShippingOutTotalGoodQuantity" & _
"WHEN J.lngShippingGoodEOH <> 0 THEN J.lngShippingGoodEOH" & _
"WHEN I.lngDieQuantity <> 0 THEN I.lngDieQuantity" & _
"WHEN J.lngCurrentDieGoodEOH <> 0 THEN J.lngCurrentDieGoodEOH" & _
"ELSE 0" & _
"End" & _
"from tblMIDCustomerLot A" & _
"INNER JOIN tblMIDExt B on A.lngAmkorID = B.lngAmkorID AND A.lngAmkorSubID = B.lngAmkorSubID and A.strWIPLotNumber = B.strWIPLotNumber COLLATE SQL_Latin1_General_CP1_CS_AS" & _
"INNER JOIN tblText C on A.lngAmkorID = C.lngAmkorID AND A.lngAmkorSubID = C.lngAmkorSubID and A.strWIPLotNumber = C.strWIPLotNumber COLLATE SQL_Latin1_General_CP1_CS_AS" & _
"LEFT JOIN " + ***ds.Tables("AMDSLot")*** + " D ON A.lngAmkorID = D.CMAMKID" & _
"AND A.lngAmkorSubID = D.CMSUBID" & _
"AND A.strWIPLotNumber = D.CMCLTN COLLATE SQL_Latin1_General_CP1_CS_AS" & _
"AND CAST(C.ReferenceStn AS INT)= D.CMREFS " & _
"LEFT JOIN dbDownload.MesLib.tblCrossRefILNAmkorID H ON A.lngAmkorID = H.lngWipAmkorId AND A.lngAmkorSubID = H.intWipSubId" & _
"LEFT JOIN dbDownload.MesLib.tblSplitAndCombineChangeLog I ON A.lngAmkorID = I.lngToAmkorID AND A.lngAmkorSubID = I.lngToAmkorSubID AND I.strType = 'COMBINE'" & _
"INNER JOIN " + ***ds.Tables("Table1")*** + " J ON A.lngAmkorID = J.lngAmkorID AND A.lngAmkorSubID = J.lngAmkorSubID" & _
"LEFT JOIN P1Common.dbo.tblReferenceStation K ON A.CustomerNum = K.CustomerCode AND A.CustomerName = K.CustomerName and C.ReferenceStn = K.ReferenceStn" & _
"where (C.DateCode <> '' or C.TraceCode <> '' or C.SCode <> '' or C.SeqCode <> '')" & _
"AND C.ReferenceStn <> '*FIRST'" & _
"order by A.strWIPLotNumber, A.strWIPLotDoubleCheckCode"
Dim dataadapter3 As New SqlDataAdapter(sql3, connection)
Thanks in Advance! :)