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! :)
Related
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' " _
)
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'm wondering how to replace the missing values (while left join).
I mean, if I didn't get a data after the join (ev4.D_EFFET is empty), I would like to put 'Not Applicable'. The NVL function doesn't work, I don't have the 'Not Applicable' as a result.
NVL(TO_CHAR(ev4.D_EFFET), 'Not Applicable') as D_EF
My code is:
RECSET2.Open " select ev1.NO_POLICE, abs(sum(ev1.MT_BRUT)) as Fiscalite, ev1.IS_DEVISE, sup.CD_SUPPORT, ev1.IS_SUPPORT," & _
" ev2.TX_TAUX/100 as TAUX1,ev3.TX_TAUX/100 as TAUX2, srva1.MT_EA as MT_EA1, srva2.MT_EA as MT_EA2, (abs(sum(ev1.MT_BRUT))+srva2.MT_EA) as Brut_fis," & _
" NVL(TO_CHAR(ev4.MT_BRUT),'Not Applicable') as MT, NVL(TO_CHAR(ev4.D_EFFET), 'Not Applicable') as D_EF,ev4.LP_NATUR_FLUX" & _
" from DB_EVENEMENT ev1 left join DB_SUPPORT sup on ev1.IS_SUPPORT=sup.IS_SUPPORT" & _
" left join DB_EVENEMENT ev2 on ev1.NO_POLICE=ev2.NO_POLICE and ev1.IS_SUPPORT=ev2.IS_SUPPORT" & _
" left join DB_EVENEMENT ev3 on ev1.NO_POLICE=ev3.NO_POLICE and ev1.IS_SUPPORT=ev3.IS_SUPPORT" & _
" left join SRVEA.DB_SEA_SUPPORT_HISTO srva1 on ev1.NO_POLICE=srva1.NO_POLICE and ev1.IS_SUPPORT=srva1.IS_SUPPORT" & _
" left join SRVEA.DB_SEA_SUPPORT_HISTO srva2 on ev1.NO_POLICE=srva2.NO_POLICE and ev1.IS_SUPPORT=srva2.IS_SUPPORT" & _
" left join DB_EVENEMENT ev4 on ev1.NO_POLICE=ev4.NO_POLICE and ev1.IS_SUPPORT=ev4.IS_SUPPORT " & _
" where ev1.NO_POLICE='" & Police_donnee & "' and ev1.IS_CLASSE_EVT=365" & _
" and ev1.LP_STATUT_EVT='DONE' and extract(year from ev1.D_EFFET)=2021 and" & _
" ev2.IS_CLASSE_EVT=563 and extract(year from ev2.D_EFFET)>2020" & _
" and ev3.IS_CLASSE_EVT=121 and ev3.N_EXERCICE_CPTA in (2021) and ev3.LP_STATUT_EVT^='ANNUL'" & _
" and extract(year from srva1.D_VALO)=2020" & _
" and extract(month from srva1.D_VALO)=12 and extract(day from srva1.D_VALO)=31" & _
" and srva1.S_TYPE_SUPPORT='TXGAR'" & _
" and extract(year from srva2.D_VALO)=2021" & _
" and extract(month from srva2.D_VALO)=12 and extract(day from srva2.D_VALO)=31" & _
" and srva2.S_TYPE_SUPPORT='TXGAR'" & _
" and srva2.S_TYPE_SUPPORT='TXGAR' and extract(year from ev4.d_effet)=2021 and ev4.IS_CLASSE_EVT=39" & _
" group by ev1.NO_POLICE, ev1.IS_DEVISE, sup.CD_SUPPORT, ev1.IS_SUPPORT,ev2.TX_TAUX/100, ev3.TX_TAUX/100,srva1.MT_EA,srva2.MT_EA, ev4.MT_BRUT, ev4.D_EFFET, ev4.LP_NATUR_FLUX", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
xlRow = Range("Colonne_1").Row + 1 + xlRow
Do While Not RECSET2.EOF
ActiveSheet.Cells(xlRow, Range("Colonne_1").Column).Value = RECSET2("NO_POLICE").Value
ActiveSheet.Cells(xlRow, Range("Colonne_2").Column).Value = RECSET2("CD_SUPPORT").Value
Select Case RECSET2.Fields("IS_DEVISE").Value
Case 46
ActiveSheet.Cells(xlRow, Range("Colonne_3").Column).Value = "EUR"
Case Else
ActiveSheet.Cells(xlRow, Range("Colonne_3").Column).Value = "UC"
End Select
ActiveSheet.Cells(xlRow, Range("Colonne_4").Column).Value = RECSET2("TAUX1").Value
ActiveSheet.Cells(xlRow, Range("Colonne_4").Column).NumberFormat = "0.00%"
ActiveSheet.Cells(xlRow, Range("Colonne_5").Column).Value = RECSET2("TAUX2").Value
ActiveSheet.Cells(xlRow, Range("Colonne_5").Column).NumberFormat = "0.00%"
ActiveSheet.Cells(xlRow, Range("Colonne_6").Column).Value = 0
ActiveSheet.Cells(xlRow, Range("Colonne_6").Column).NumberFormat = "0.00%"
ActiveSheet.Cells(xlRow, Range("Colonne_7").Column).Value = RECSET2.Fields("D_EF").Value
ActiveSheet.Cells(xlRow, Range("Colonne_8").Column).Value = RECSET2("MT").Value
ActiveSheet.Cells(xlRow, Range("Colonne_8").Column).NumberFormat = "#,##0.00€"
ActiveSheet.Cells(xlRow, Range("Colonne_9").Column).Value = RECSET2("LP_NATUR_FLUX").Value
ActiveSheet.Cells(xlRow, Range("Colonne_10").Column).Value = RECSET2("MT_EA1").Value
ActiveSheet.Cells(xlRow, Range("Colonne_10").Column).NumberFormat = "#,##0.00€"
ActiveSheet.Cells(xlRow, Range("Colonne_11").Column).Value = RECSET2("Brut_fis").Value
ActiveSheet.Cells(xlRow, Range("Colonne_11").Column).NumberFormat = "#,##0.00€"
ActiveSheet.Cells(xlRow, Range("Colonne_12").Column).Value = RECSET2("Fiscalite").Value
ActiveSheet.Cells(xlRow, Range("Colonne_12").Column).NumberFormat = "#,##0.00€"
ActiveSheet.Cells(xlRow, Range("Colonne_13").Column).Value = RECSET2("MT_EA2").Value
ActiveSheet.Cells(xlRow, Range("Colonne_13").Column).NumberFormat = "#,##0.00€"
RECSET2.MoveNext
xlRow = xlRow + 1
Loop
RECSET2.Close
Call DECONNEXION_PEGASE
End Sub
Your query wouldn't return any rows for cases where d_effet being NULL because you have the following condition:
WHERE ...
and extract(year from ev4.d_effet)=2021 and ev4.IS_CLASSE_EVT=39
The rows would be eliminated by this condition if D_EFFET is NULL.
Also, your LEFT JOIN to DB_EVENEMENT ev4 is turned into an inner join because of this condition; if you intended to limit this join to those rows with the above condition move the criteria to the ON clause.
I want to run a string dynamically.
I'm trying to run a VBA loop to build a SQL Union for each record after the first. There could be anywhere from 1 record to 100. I want this to be dynamic so I don't have to limit the number of entries.
Example:
If I have 5 records it creates the SQL query with 4 unions. All the same data etc.
I'm trying to do is this:
When someone opens a form they will enter a list of pack numbers, from that they will select the range of offers under each pack number (All Offers, Promo, or Buyer).
The code then builds a union query for each pack number based on the the offer range they selected.
The output is all the data on those Offers under that pack number.
My full code: (I thought it necessary to get the full picture)
Private Sub ReviewButton_Click()
Dim Owner As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qdfPassThrough As QueryDef
Dim strSeasonSQL As String
Dim strSeason As String
Dim strType As String
Owner = GetNamespace("MAPI").Session.CurrentUser.AddressEntry
If Me.NewRecord = True Then
Me!Owner.Value = Owner
End If
Set db = CurrentDb
Set rs = CurrentDb.OpenRecordset("RetailEntry")
'Set rs = CurrentDb.OpenRecordset("SELECT * FROM RetailEntry")
strSeason = [Forms]![Retail_Navigation]![NavigationSubform].[Form]![cboSeason]
strType = rs.Fields("Offer").Value '[Forms]![ReviewButton]![RetailEntry].[Form]![Offer].Value
On Error GoTo 1
1:
'Build Initial Query based on first record and make sure there are records
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'All Offers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If rs.Fields("Offer") = "All Offers" Then
StrSQL = "Set NoCount ON DROP TABLE #catcov; " _
& "SELECT DISTINCT mailyear, offer, description, firstreleasemailed, season_id, offer_type, " _
& "case when description like '%Promo%' then 'Promo' " _
& "Else 'Buyer' end As addtype " _
& "INTO #catcov " _
strSELECT = "FROM supplychain_misc.dbo.catcov; " _
& "SELECT DISTINCT " _
& "a.PackNum " _
& ",a.Description " _
& ",a.CatID " _
& ",DATEPART(QUARTER, FirstReleaseMailed) as Quarter " _
& ",a.RetOne " _
& ",a.Ret2 " _
& ",a.ORIGINALRETAIL " _
& ",a.DiscountReasonCode " _
& ",b.Season_id " _
& ",a.year " _
& ",addtype "
strFROM = "FROM PIC704Current a JOIN #CatCov b ON (a.CatID = b.Offer) and (a.Year = b.MailYear) " _
strWHERE = "WHERE b.Offer_Type In('catalog', 'insert', 'kicker', 'statement insert', 'bangtail', 'onsert', 'outside ad') " _
& " and b.Season_id = '" & strSeason & "' " _
& " and (Case when b.FirstReleaseMailed >= cast(dateadd(day, +21, getdate()) as date) then 1 else 0 end) = 1 "
StrSQL = StrSQL & vbCrLf & strSELECT & vbCrLf & strFROM & vbCrLf & strWHERE
'Promo/Core
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ElseIf rs.Fields("Offer") = "Promo" Or rs.Fields("Offer") = "Buyer" Then
StrSQL = "Set NoCount ON DROP TABLE #catcov; " _
& "SELECT DISTINCT mailyear, offer, description, firstreleasemailed, season_id, offer_type, " _
& "case when description like '%Promo%' then 'Promo' " _
& "Else 'Buyer' end As addtype " _
& "INTO #catcov " _
strSELECT = "FROM supplychain_misc.dbo.catcov; " _
& "SELECT DISTINCT " _
& "a.PackNum " _
& ",a.Description " _
& ",a.CatID " _
& ",DATEPART(QUARTER, FirstReleaseMailed) as Quarter " _
& ",a.RetOne " _
& ",a.Ret2 " _
& ",a.ORIGINALRETAIL " _
& ",a.DiscountReasonCode " _
& ",b.Season_id " _
& ",a.year " _
& ",addtype "
strFROM = "FROM PIC704Current a JOIN #CatCov b ON (a.CatID = b.Offer) and (a.Year = b.MailYear) " _
strWHERE = "WHERE b.Offer_Type In('catalog', 'insert', 'kicker', 'statement insert', 'bangtail', 'onsert', 'outside ad') " _
& " and b.Season_id = '" & strSeason & "' and b.addtype = '" & strType & "' " _
& " and (Case when b.FirstReleaseMailed >= cast(dateadd(day, +21, getdate()) as date) then 1 else 0 end) = 1 "
StrSQL = StrSQL & vbCrLf & strSELECT & vbCrLf & strFROM & vbCrLf & strWHERE
End If
'Build/Loop Unions for each record after the first
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
rs.MoveNext
strType = rs.Fields("Offer").Value
Do Until rs.EOF = True
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'All Offers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If rs.Fields("Offer") = "All Offers" Then
StrUnion = "UNION SELECT DISTINCT " _
& "a.PackNum " _
& ",a.Description " _
& ",a.CatID " _
& ",DATEPART(QUARTER, FirstReleaseMailed) as Quarter " _
& ",a.RetOne " _
& ",a.Ret2 " _
& ",a.ORIGINALRETAIL " _
& ",a.DiscountReasonCode " _
& ",b.Season_id " _
& ",a.year " _
& ",addtype "
strFROMnxt = "FROM PIC704Current a JOIN #CatCov b ON (a.CatID = b.Offer) and (a.Year = b.MailYear) " _
strWHEREnxt = "WHERE b.Offer_Type In('catalog', 'insert', 'kicker', 'statement insert', 'bangtail', 'onsert', 'outside ad') " _
& " and b.Season_id = '" & strSeason & "' " _
& " and (Case when b.FirstReleaseMailed >= cast(dateadd(day, +21, getdate()) as date) then 1 else 0 end) = 1 "
StrSQL2 = StrUnion & vbCrLf & strFROMnxt & vbCrLf & strWHEREnxt
'Promo/Buyer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ElseIf rs.Fields("Offer") = "Promo" Or rs.Fields("Offer") = "Buyer" Then
StrUnion = "UNION SELECT DISTINCT " _
& "a.PackNum " _
& ",a.Description " _
& ",a.CatID " _
& ",DATEPART(QUARTER, FirstReleaseMailed) as Quarter " _
& ",a.RetOne " _
& ",a.Ret2 " _
& ",a.ORIGINALRETAIL " _
& ",a.DiscountReasonCode " _
& ",b.Season_id " _
& ",a.year " _
& ",addtype "
strFROMnxt = "FROM PIC704Current a JOIN #CatCov b ON (a.CatID = b.Offer) and (a.Year = b.MailYear) " _
strWHEREnxt = "WHERE b.Offer_Type In('catalog', 'insert', 'kicker', 'statement insert', 'bangtail', 'onsert', 'outside ad') " _
& " and b.Season_id = '" & strSeason & "' and b.addtype = '" & strType & "' " _
& " and (Case when b.FirstReleaseMailed >= cast(dateadd(day, +21, getdate()) as date) then 1 else 0 end) = 1 "
StrSQL2 = StrUnion & vbCrLf & strFROMnxt & vbCrLf & strWHEREnxt
End If
'Move to next Record and loop till EOF
rs.MoveNext
Loop
'If there are no Records then error
Else
MsgBox "There are no Pack Numbers Entered."
End If
'END QUERY
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Build Retail Bump File Pass Through Query
db.QueryDefs.Delete "qryMaster"
Set qdfPassThrough = db.CreateQueryDef("qryMaster")
qdfPassThrough.Connect = "ODBC;DSN=SupplyChainMisc;Description=SupplyChainMisc;Trusted_Connection=Yes;DATABASE=SupplyChain_Misc;"
qdfPassThrough.ReturnsRecords = True
qdfPassThrough.sql = StrSQL & vbCrLf & StrSQL2
rs.Close
Set rs = Nothing
DoCmd.OpenForm "SubCanButton"
DoCmd.OpenQuery "MasterQuery"
DoCmd.Close acForm, "ReviewButton"
End Sub
First, you do a "union distinct" when you don't include ALL:
UNION ALL
SELECT DISTINCT ...
Thus, as your selected records seem the same, only one will returned.
Second, including ALL or not, your concept doesn't make much sense. Why union a lot of identical records? Even if they hold different IDs only, they seem to be pulled from the same table, which you could with a single query.
Third, casting a date value to a date value does nothing good, so:
cast(dateadd(day, +21, getdate()) as date)
can be reduced to:
dateadd(day, +21, getdate())
I am getting an error message when I run my VBA code. I tried debugging and seems like there is something wrong with my connection string. I am getting an error message "Data source name not found and no default driver specified.
Sub Macro176()
Sheets("176Tk").Select
Dim i As Integer
i = 17
Set oConn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
oConn.Open "Driver={MySQL ODBC 5.1 Driver};" & _
"server=myserveraddress;" & _
"database=mydatabasename; uid= myuserId; pwd=mypassword;" & _
"option=3"
SQLstr = "SELECT SUBSTRING(dbo.collection_points.name, CHARINDEX('-', dbo.collection_points.name) + 2, 3) + 'Tk' AS 'Tank', " & _
"dbo.data_values.actual_date_time AS 'Date', " & _
"dbo.data_values.value_text AS 'Drain Time', " & _
"dbo.data_points.name AS 'Description', " & _
"dbo.data_values.user_id AS 'Field Operator' " & _
"FROM " & _
"dbo.collection_points INNER JOIN " & _
"dbo.area ON dbo.collection_points.area_rec_id = dbo.area.area_rec_id INNER JOIN " & _
"dbo.data_points ON dbo.collection_points.cp_rec_id = dbo.data_points.cp_rec_id INNER JOIN " & _
"dbo.data_values ON dbo.data_points.dp_rec_id = dbo.data_values.dp_rec_id INNER JOIN " & _
"dbo.system_levels ON dbo.area.ownership_sysid = dbo.system_levels.system_id " & _
"WHERE " & _
"((dbo.system_levels.description = 'CX5 Crude Tank Farm' and dbo.area.name = 'Crd TF Draining') OR " & _
"(dbo.system_levels.description = 'CX5 Melvindale Tank Farm' and dbo.area.name = 'Mel Tank Draining') OR " & _
"(dbo.system_levels.description = 'CX5 Tank Farm (CP)' and dbo.area.name = 'CP Tank Draining') OR " & _
"(dbo.system_levels.description = 'CX5 Unifiner Tank Farm' and dbo.area.name = 'LabT.F. Draining')) AND " & _
"((dbo.data_values.value_text <> 'Yes') AND (dbo.data_values.value_text <> 'Complete') AND (dbo.data_values.value_text <> 'Incomplete') AND " & _
"(dbo.collection_points.name NOT like '%Roof%')) AND " & _
"dbo.data_values.nominal_date_time>='2017-01-01 00:00:00' and " & _
"dbo.data_values.nominal_date_time<'2018-12-31 11:59:59' " & _
"ORDER BY dbo.collection_points.name, dbo.data_values.actual_date_time"
rs.Open SQLstr, oConn, adLockOptimistic, adCmdTable
Do Until rs.EOF
Sheets("176Tk").Cells(i, 1).Value = rs.Fields("Tank")
Sheets("176Tk").Cells(i, 2).Value = rs.Fields("Date")
Sheets("176Tk").Cells(i, 3).Value = rs.Fields("Drain Time")
Sheets("176Tk").Cells(i, 4).Value = rs.Fields("Field Operator")
i = i + 1
Loop
rs.Close
oConn.Close
Set rs = Nothing
Set oConn = Nothing
End Sub