SQL Statement won't work anymore after small change. VBA - sql

this Statement works totaly fine in ORACLE SQL DEVELOPER, but when I insert it to my vba makro it won't return me my BLOB files from the database. The statement worked before in VBA with a data-number variable instead of referencing to SYSDATE...
Provider is OraOLEDB.Oracle, the Connections string "works/opens" but there is no data to pass my - IF rs.EOF = FALSE Then - I've already tested eof=true but no data comes through....Any ideas ? thx!
select datas
from tdb
INNER JOIN (select datas_id
from tfdz
INNER JOIN (select fb_id
from tfb
INNER JOIN (select pu_id
from tpu
INNER JOIN (select tap.p_id
from tap
INNER JOIN (SELECT po_id
FROM tpo
WHERE tpo.lastdate like (SYSDATE-1)
) sub_tpo
ON tap.po_id = sub_tpo.po_id and tap.lastdate like (SYSDATE-1)
) sub_tap
ON tpu.p_id = sub_tap.p_id
) sub_tpu
ON tfb.pu_id = sub_tpu.pu_id
where tfb.deleated = 0
) sub_tfb
ON tfdz.fb_id = sub_tfb.fb_id
) sub_tfdz
ON tdb.datas_id = sub_tfdz.datas_id
order by sub_tfdz.datas_id asc

It would be good to see how you are passing it, I assume as a string, have you tried.
WHERE tpo.lastdate like (" & Format(DateAdd("d", -1, Now()), "DD/MMM/YYYY") & ")"
You may need to change the format to be compatible Oracle date processing

Related

SQL command in VBA column limitation

Is there a limitation of how many columns can be brought over in one sqlcommand in VBA code? My code is quite long but is used to ensure that I am pulling from the right database at all times. The m2mdata02 will be replaced with code CatalogM2M upon a user's selection in the database. The total number of columns to bring over is 23 columns from 7 tables.
Sqlcommand = “select left(m2mdata02.dbo.jomast.fjobno,5) as job, m2mdata02.dbo.jomast.fjobno, m2mdata02.dbo.jomast.fpartno, m2mdata02.dbo.jomast.fstatus, m2mdata02.dbo.jomast.fact_rel, m2mdata02.dbo.jomast.fddue_date, m2mdata02.dbo.jomast.fprodcl, m2mdata02.dbo.jomast.frel_dt, m2mdata02.dbo.jomast.frouting, m2mdata02.dbo.inmastx.fpartno, m2mdata02.dbo.inmastx.fdescript, m2mdata02.dbo.inmastx.fprice, m2mdata02.dbo.somast.fsono, m2mdata02.dbo.somast.fcompany, m2mdata02.dbo.somast.fcustpono, m2mdata02.dbo.aritem.fcinvoice, m2mdata02.dbo.aritem.fprice, m2mdata02.dbo.aritem.fcsono, m2mdata02.dbo.armast.fcinvoice, m2mdata02.dbo.armast.finvdate, m2mdata02.dbo.shmast.fshipdate, m2mdata02.dbo.shmast.fshipno, m2mdata02.dbo.shmast.fcsono
from m2mdata02.dbo.jomast
left join m2mdata02.dbo.inmastx on m2mdata02.dbo.inmastx.fpartno = m2mdata02.dbo.jomast.fpartno
left join m2mdata02.dbo.somast on m2mdata02.dbo.somast.fsono = m2mdata02.dbo.jomast.fsono
left join m2mdata02.dbo.aritem on m2mdata02.dbo.aritem.FCSONO = m2mdata02.dbo.jomast.fsono
left join m2mdata02.dbo.armast on m2mdata02.dbo.armast.fcinvoice = m2mdata02.dbo.aritem.fcinvoice
left join m2mdata02.dbo.shmast on m2mdata02.dbo.shmast.fcsono = m2mdata02.dbo.somast.fsono
where m2mdata02.dbo.jomast.fprodcl = 'FG11' order by m2mdata02.dbo.jomast.fjobno”
According to MSDN docs, the character limit of String type in VBA is approximately two billion (2^31) Unicode characters. Your SQL query is no where near that limitation. And if using ADO to connect Excel VBA to database, the server engine here being SQL Server decides column limitation and 23 is again no where near maximum capacity.
However, consider using table aliases to cut down on characters, avoid repeating triple period identifiers, and overall improve readability even maintainability as you can adjust table reference easier for each user selection.
select left(j.fjobno,5) as job
, j.fjobno
, j.fpartno
, j.fstatus
, j.fact_rel
, j.fddue_date
, j.fprodcl
, j.frel_dt
, j.frouting
, i.fpartno
, i.fdescript
, i.fprice
, so.fsono
, so.fcompany
, so.fcustpono
, ari.fcinvoice
, ari.fprice
, ari.fcsono
, arm..fcinvoice
, arm..finvdate
, sh.fshipdate
, sh.fshipno
, sh.fcsono
from m2mdata02.dbo.jomast j
left join m2mdata02.dbo.inmastx i on i.fpartno = j.fpartno
left join m2mdata02.dbo.somast so on so.fsono = j.fsono
left join m2mdata02.dbo.aritem ari on ari.FCSONO = j.fsono
left join m2mdata02.dbo.armast arm on arm..fcinvoice = ari.fcinvoice
left join m2mdata02.dbo.shmast sh on sh.fcsono = so.fsono
where j.fprodcl = 'FG11'
order by j.fjobno
By the way, for very long queries, avoid the need to build string in VBA with line concatenation and double quotes, and read directly from a formatted SQL text file:
Dim strSQL As String
' READ SQL QUERY FROM FILE
With CreateObject("Scripting.FileSystemObject")
strSQL = .OpenTextFile("C:\path\to\my\SQL\Query.sql", 1).readall
End With
' REPLACE DB IN STRING
strSQL = Replace(strSQL, "m2mdata02", "someotherdb")

Can't figure out how to add current date to comment

I'm having problems with my sql code, i'm trying to implement a current date to my comment but i can't figure out how + i'm having syntax errors and i don't know what to do anymore. Can someone help me with the date adding to comment?
UPDATE Osalus_projektis AS op
SET op.töötasu = op.töötasu + 100,
op.comment = CONCAT(NOW()'tõusis palk 100 eurot')
FROM Osalus_projektis
INNER JOIN Osaluse_liik AS ol
ON ol.osaluse_liik = op.osaluse_liik
WHERE ol.nimetus = 'nõustaja';
MS Access uses & for string concatenation. And it doesn't support a FROM clause. This may do what you want:
UPDATE Osalus_projektis AS op INNER JOIN
Osaluse_liik AS ol
ON ol.osaluse_liik = op.osaluse_liik
SET op.töötasu = op.töötasu + 100,
op.comment = NOW() & 'tõusis palk 100 eurot'
WHERE ol.nimetus = 'nõustaja';
If there is a problem with the JOIN -- which happens a lot in MS Access -- then you can use an EXISTS clause as well.

Complex JOINS in Access SQL difficult to convert to JET OLEDB

I'm a long time follower of Stack overflow but this is my first post. I'm hoping the community can help.
I have a successful Access Query that returns the required results - Perfect!
HOWEVER, I'm trying to return the same using OLEDB connection to the database within an ASP script. This is all legacy stuff however we are allowing web access to this legacy information.
MS Access (2016) shows Query as this... (works)
SELECT [EventName] & ": " & [RoundCaption] AS RoundTitle, ChunkEntryTable.WinPos
FROM ((EventTable INNER JOIN EventRoundTable ON EventTable.EventId = EventRoundTable.EventId) INNER JOIN ((RoundHeatTable INNER JOIN ChunkTable ON RoundHeatTable.RoundHeatId = ChunkTable.RoundHeatId) INNER JOIN (EventEntryTable INNER JOIN ChunkEntryTable ON EventEntryTable.EventEntryId = ChunkEntryTable.EventEntryId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId) ON EventRoundTable.RoundKeyId = RoundHeatTable.RoundKeyId) LEFT JOIN EventEntryMemberTable ON EventEntryTable.EventEntryId = EventEntryMemberTable.EventEntryId
WHERE (((EventEntryTable.Entry1Id)=[EntryId])) OR (((EventEntryTable.Entry2Id)=[EntryId])) OR (((EventEntryTable.Entry3Id)=[EntryId])) OR (((EventEntryMemberTable.MemberId)=[EntryId]))
ORDER BY EventTable.SortIdx, EventRoundTable.RoundId DESC , EventRoundTable.IsRepechage DESC;
Doing this in OLEDB. Connection string as follows...
<%
' FileName="Connection_ado_conn_string.htm"
' Type="ADO"
' DesigntimeType="ADO"
' HTTP="true"
' Catalog=""
' Schema=""
Dim MM_csresultdb_STRING
MM_csresultdb_STRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xyz.mde;Jet OLEDB:Database Password=xxxxxxxxx;"
%>
Connection works perfectly but I can't seem to get the SQL command to work. I get "No value given for one or more required parameters".
NOTE: I have replaced [EntryID] in 4 places with a valid value and it works perfectly in Access just not outside of Access using OLEDB. Here's what the SQL is I'm using...
SELECT EventTable.EventName & ": " & EventRoundTable.RoundCaption AS RoundTitle, ChunkEntryTable.WinPos FROM
((EventTable INNER JOIN EventRoundTable ON EventTable.EventId = EventRoundTable.EventId) INNER JOIN
((RoundHeatTable INNER JOIN ChunkTable ON RoundHeatTable.RoundHeatId = ChunkTable.RoundHeatId) INNER JOIN
(EventEntryTable INNER JOIN ChunkEntryTable ON EventEntryTable.EventEntryId = ChunkEntryTable.EventEntryId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId)
ON EventRoundTable.RoundKeyId = RoundHeatTable.RoundKeyId)
WHERE ((EventEntryTable.Entry1Id)=4741) OR ((EventEntryTable.Entry2Id)=4741) OR ((EventEntryTable.Entry3Id)=4741)
ORDER BY EventTable.SortIdx, EventRoundTable.RoundId DESC , EventRoundTable.IsRepechage DESC;
FOUND PROBLEM ** See answer below
FOUND PROBLEM ** It's to do with this part of the SQL...
[EventName] & ": " & [RoundCaption] AS RoundTitle
Changed to
[EventName], [RoundCaption] AS RoundTitle
and it works but gives me two separate fields rather than the one concatenated field called "RoundTitle". So I'll join the two result fields during the display output rather than at the query stage.
Whew! That many days to figure out. Thanks to the comments that kinda steered me in that direction of the AS part of the statement.

ConcatRelated() - query or tables

I have similar issue to this question Combine values from related rows into a single concatenated string value.
I've got two queries:
This is what it looks like now without ConcatRelated():
I need to get return:
I tried to use this SQL:
SELECT DISTINCT
Q_Fakt1.FakturaID,
Q_Fakt1.DatumVystavenia,
Q_Fakt1.DatumSplatnosti,
Q_Fakt2.Pismeno,
ConcatRelated(
"pismeno",
"Q_Fakt2",
"FakturaID = '" & [Q_Fakt1]![FakturaID] & "'"
) AS Letters
FROM Q_Fakt1 INNER JOIN Q_Fakt2 ON Q_Fakt1.FakturaID = Q_Fakt2.FakturaID;
Result is 7× popup:
ConcatRelated() Error3464: Data type mismatch in criteria expression.
I did the same with Tables but I have little bit more complicated Relations so...
https://i.stack.imgur.com/TM7Cu.png
SQL:
SELECT DISTINCT
Faktury.FakturaID,
Kategorie.Oznacenie,
Faktury.DatumVystavenia,
FakturujemVam.FakturujemVamID,
FakturyDetaily.FakturujemVam,
[DatumVystavenia]+[splatnostFaktury] AS DatumSplatnosti,
ConcatRelated("Oznacenie","kategorie","FakturaID = '" & [FakturaID] & "'") AS Letters
FROM Kategorie INNER JOIN (Faktury INNER JOIN (FakturujemVam INNER JOIN FakturyDetaily ON FakturujemVam.FakturujemVamID = FakturyDetaily.FakturujemVam) ON Faktury.FakturaID = FakturyDetaily.Faktura) ON Kategorie.KategoriaID = FakturujemVam.Kategoria;
Result is 6× popup:
ConcatRelated() Error3061: Too Few parameters. Excepted 1.
Where did I go wrong? Thank you for Help
That's because you're using string delimiters when you're not using a string.
Remove those delimiters, and it will work fine:
ConcatRelated("Oznacenie","kategorie","FakturaID = " & [FakturaID] ) AS Letters
SOLVED:
STEP 1
Create Query to merge more tables in one
Datasheet View
Design View
STEP 2
Create another Query & Use ConcatRelated()
Design View
SQL:
SELECT
Q_Part_Bill_Num2.NumBill,
Q_Part_Bill_Num2.C_Mark,
ConcatRelated(
"C_Mark",
"Q_Part_Bill_Num2",
"Q_Part_Bill_Num2!NumBill = " & [Q_Part_Bill_Num2]![NumBill]
) AS PartBillNum2
FROM Q_Part_Bill_Num1 INNER JOIN Q_Part_Bill_Num2 ON Q_Part_Bill_Num1.NumBill = Q_Part_Bill_Num2.NumBill;
STEP 3 - Optional
Edit MODULE to delete / change separator ", "
STEP 4
Create One Last Query to Concatenate everything together.
Design View
SQL:
SELECT DISTINCT
Q_Part_Bill_Num1.PartBillNum1,
Q_Part_Bill_Num3.PartBillNum2,
[PartBillNum1] & [PartBillNum2] AS [Full]
FROM
(T_Bills INNER JOIN Q_Part_Bill_Num1 ON T_Bills.Bills_ID = Q_Part_Bill_Num1.Bills_ID)
INNER JOIN (Q_Part_Bill_Num2 INNER JOIN Q_Part_Bill_Num3
ON (Q_Part_Bill_Num2.NumBill = Q_Part_Bill_Num3.NumBill)
AND (Q_Part_Bill_Num2.C_Mark = Q_Part_Bill_Num3.C_Mark))
ON Q_Part_Bill_Num1.NumBill = Q_Part_Bill_Num2.NumBill;
Use DISTINCT to avoid duplicates.
I Hope this will help someone.
Thank you all, for your time :)

Run-time error: Invalid parameter 1 specified for datepart - VBA - SQL

I'm running some code in Excel VBA which queries a database then brings the data into Excel for formatting.
It worked fine the last time I ran it (that old chestnut) but today I've come to run the monthly report and it is throwing up a datepart error as
Invalid parameter 1 specified for datepart
Here is the code:
StrQuery = "SELECT dbo_MANUFACTURER.COMPANY_NAME AS BRAND, dbo_COMPANY.COMPANY_NAME, dbo_AGENTS.SHORT_DESC AS AGENT, dbo_STOCK_SUB_TYPE.SHORT_DESC AS [STOCK TYPE], Replace(Replace(dbo_COMPANY.CURRENCY_ID,'110','EURO'),'1','GBP') AS [CURRENCY]," & _
"Sum(([Unit_Net]*[QTY_SOLD])-[DELIVERY_TOTAL]) AS [NET TOTAL], dbo_SORDER.DATE_CREATED, dbo_SORDER.SORDER_CODE FROM (((((dbo_COMPANY INNER JOIN dbo_SORDER ON dbo_COMPANY.COMPANY_ID = dbo_SORDER.COMPANY_ID) INNER JOIN " & _
"dbo_SORDER_ITEM ON dbo_SORDER.SORDER_ID = dbo_SORDER_ITEM.SORDER_ID) INNER JOIN dbo_STOCK ON dbo_SORDER_ITEM.STOCK_ID = dbo_STOCK.STOCK_ID) INNER JOIN dbo_STOCK_SUB_TYPE ON dbo_STOCK.SSTYPE_ID = dbo_STOCK_SUB_TYPE.SSTYPE_ID) INNER JOIN " & _
"dbo_AGENTS ON dbo_COMPANY.AGENT_ID = dbo_AGENTS.AGENT_ID) INNER JOIN dbo_MANUFACTURER ON dbo_STOCK.MANUF_ID = dbo_MANUFACTURER.MANUF_ID GROUP BY dbo_MANUFACTURER.COMPANY_NAME, dbo_COMPANY.COMPANY_NAME, dbo_AGENTS.SHORT_DESC, " & _
"dbo_STOCK_SUB_TYPE.SHORT_DESC, dbo_SORDER.DATE_CREATED, dbo_SORDER.SORDER_CODE, dbo_COMPANY.CURRENCY_ID, dbo_STOCK.MANUF_ID HAVING (((dbo_STOCK_SUB_TYPE.SHORT_DESC) <> " & "'EMBROIDERY'" & ") And ((Year([DATE_CREATED]) * 12 + " & _
"DatePart(" & "'m'" & ", [DATE_CREATED])) = Year(Date) * 12 + DatePart(" & "'m'" & ", Date) - 1)) ORDER BY dbo_COMPANY.COMPANY_NAME, dbo_SORDER.DATE_CREATED;"
Apologies for the mass of text on long lines.
As mentioned the code throws up the error: Invalid parameter 1 specified for date part. I've tried converting the dates as someone mentioned on a forum post but that hasn't been successful.
If someone has any ideas that would be amazing!
EDIT:
Removing the quotes around the m in datepart does not work in Access. It pops up asking for a value for m
The SQL query I have works fine in Access, I have added it below:
SELECT
dbo_MANUFACTURER.COMPANY_NAME AS BRAND, dbo_COMPANY.COMPANY_NAME,
dbo_AGENTS.SHORT_DESC AS AGENT, dbo_STOCK_SUB_TYPE.SHORT_DESC AS [STOCK TYPE], Replace(Replace(dbo_COMPANY.CURRENCY_ID,'110','EURO'),'1','GBP') AS [CURRENCY], Sum(([Unit_Net]*[QTY_SOLD])-[DELIVERY_TOTAL]) AS [NET TOTAL], dbo_SORDER.DATE_CREATED, dbo_SORDER.SORDER_CODE
FROM
(((((dbo_COMPANY INNER JOIN dbo_SORDER ON dbo_COMPANY.COMPANY_ID = dbo_SORDER.COMPANY_ID) INNER JOIN dbo_SORDER_ITEM ON dbo_SORDER.SORDER_ID = dbo_SORDER_ITEM.SORDER_ID) INNER JOIN dbo_STOCK ON dbo_SORDER_ITEM.STOCK_ID = dbo_STOCK.STOCK_ID) INNER JOIN dbo_STOCK_SUB_TYPE ON dbo_STOCK.SSTYPE_ID = dbo_STOCK_SUB_TYPE.SSTYPE_ID) INNER JOIN dbo_AGENTS ON dbo_COMPANY.AGENT_ID = dbo_AGENTS.AGENT_ID) INNER JOIN dbo_MANUFACTURER ON dbo_STOCK.MANUF_ID = dbo_MANUFACTURER.MANUF_ID
GROUP BY
dbo_MANUFACTURER.COMPANY_NAME, dbo_COMPANY.COMPANY_NAME, dbo_AGENTS.SHORT_DESC, dbo_STOCK_SUB_TYPE.SHORT_DESC, dbo_SORDER.DATE_CREATED, dbo_SORDER.SORDER_CODE, dbo_COMPANY.CURRENCY_ID, dbo_STOCK.MANUF_ID
HAVING
(((dbo_STOCK_SUB_TYPE.SHORT_DESC)<>"EMBROIDERY") AND ((Year([DATE_CREATED])*12+DatePart(m,[DATE_CREATED]))=Year(Date())*12+DatePart(m,Date())-1))
ORDER BY dbo_COMPANY.COMPANY_NAME, dbo_SORDER.DATE_CREATED;
Fundamentally, you are confusing SQL dialects which use the same named function DatePart but with different arguments. Depending on architecture layer of your Excel app, you need to adjust the DatePart function according to the SQL dialect used either: ACE/Jet SQL or SQL Server TSQL.
Excel -> Access -> SQL Server (linked tables)
If Excel connects to Access database and this Access database uses
SQL Server linked tables (Access objects), then you must adhere to ACE/Jet SQL
dialect which requires the first argument of
DatePart
to be a string literal enclosed in double or single
quotes as #Arulkumar shows in answer:
DatePart('m', [DATE_CREATED])
Excel -> SQL Server (ADO)
If Excel connects directly to SQL Server via ADO connection in VBA
then you must adhere to SQL Server TSQL dialect which requires the first argument of DatePart to be a named value not string literal as #GarethD
describes in comment:
DatePart(MONTH, [DATE_CREATED])
DatePart(MM, [DATE_CREATED])
DatePart(M, [DATE_CREATED])
Excel -> Access -> SQL Server (pass-thru)
If Excel connects to Access database calling an Access pass-through query (not linked tables) and this pass-through query (scripted and saved in Access database) connects to SQL Server via ODBC/OLEDB then you must adhere to SQL Server TSQL dialect as described above.
From the nomenclature of your tables, I believe you are connecting to Access with MSSQL linked tables as SQL Server's object qualifiers maintain schema plus table plus field separated by periods:
dbo.COMPANY.COMPANY_NAME
When linked to Access, multiple periods in table names are not allowed so by default are replaced with underscore:
dbo_COMPANY.COMPANY_NAME
The invalid object error means either the Access linked table does not exist in Access database or the table does not exist in connected SQL Server database.
Simply try with the below query
SELECT DATEPART('m', GETDATE())
returns the
Invalid parameter 1 specified for datepart.
MSAccess DatePart, requires double quotes around the setting. So try with double quotes instead of single quote will solve your problem.
So your working query will be:
SELECT dbo_MANUFACTURER.COMPANY_NAME AS BRAND,
dbo_COMPANY.COMPANY_NAME,
dbo_AGENTS.SHORT_DESC AS AGENT,
dbo_STOCK_SUB_TYPE.SHORT_DESC AS [STOCK TYPE],
Replace(Replace(dbo_COMPANY.CURRENCY_ID,'110','EURO'),'1','GBP') AS [CURRENCY],
Sum(([Unit_Net]*[QTY_SOLD])-[DELIVERY_TOTAL]) AS [NET TOTAL],
dbo_SORDER.DATE_CREATED,
dbo_SORDER.SORDER_CODE
FROM (((((
dbo_COMPANY
INNER JOIN dbo_SORDER ON dbo_COMPANY.COMPANY_ID = dbo_SORDER.COMPANY_ID)
INNER JOIN dbo_SORDER_ITEM ON dbo_SORDER.SORDER_ID = dbo_SORDER_ITEM.SORDER_ID)
INNER JOIN dbo_STOCK ON dbo_SORDER_ITEM.STOCK_ID = dbo_STOCK.STOCK_ID)
INNER JOIN dbo_STOCK_SUB_TYPE ON dbo_STOCK.SSTYPE_ID = dbo_STOCK_SUB_TYPE.SSTYPE_ID)
INNER JOIN dbo_AGENTS ON dbo_COMPANY.AGENT_ID = dbo_AGENTS.AGENT_ID)
INNER JOIN dbo_MANUFACTURER ON dbo_STOCK.MANUF_ID = dbo_MANUFACTURER.MANUF_ID
GROUP BY dbo_MANUFACTURER.COMPANY_NAME,
dbo_COMPANY.COMPANY_NAME,
dbo_AGENTS.SHORT_DESC,
dbo_STOCK_SUB_TYPE.SHORT_DESC,
dbo_SORDER.DATE_CREATED,
dbo_SORDER.SORDER_CODE,
dbo_COMPANY.CURRENCY_ID,
dbo_STOCK.MANUF_ID
HAVING (((dbo_STOCK_SUB_TYPE.SHORT_DESC) <> 'EMBROIDERY')
And ((Year([DATE_CREATED]) * 12
+ DatePart("m", [DATE_CREATED])) = Year(Date) * 12
+ DatePart("m", Date) - 1))
ORDER BY dbo_COMPANY.COMPANY_NAME,
dbo_SORDER.DATE_CREATED;