Why am I getting error message using SQL in vba code? [closed] - sql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Can you please help me make this code run? I am getting an error
Token ( was not valid
con.Open strCon
Set rs = con.Execute("SELECT concat(q.CHSeries, q.CHNumber) as Chassino,q.Model," & _
"Case q.BRANCH when '6' then 'branch 6' when '2' then 'branch 2' Else 'XX' end Branch," & _
"q.ORDER,q.indt DeofInvoice, q.invn Invoice_no, q.customer Customer," & _
"(select coalesce(sum(amnt*ICVL),0) from lbr2 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts2 where pcno<>0 and q.order=orno)+" & _
"(select coalesce(sum(amnt*ICVL),0) from lbr1 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts1 where pcno<>0 and q.order=orno)" & _
"Internal_Sales," & _
"coalesce(case q.InvoiceCredit when 'F' then (q.RetailwithV-q.V) else -1*(q.RetailwithV-q.V)end,0) External_Sales" & _
"FROM (select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2" & _
"FROM HEADER1 , PARTS1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2" & _
"FROM HEADER1, LBR1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO" & _
"Union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2" & _
"FROM HEADER2 , PARTS2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx, cd2" & _
"FROM HEADER2 , LBR2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO) & q (CHSeries, CHNumber, Model, BRANCH, ORDER, indt, invn, customer, RetailwithV, V, InvoiceCredit)" & _
"where indt ='2019-09-30' order by branch")
For iCols = 0 To rs.Fields.Count - 1
Worksheets("Sheet1").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next```

Since you are not adding newline to your sql, you should at least add space after openig or before closing ".
The sql you posted concats into something messed.
for readibility I would suggest:
Dim sql as String
sql = sql & vbCRLF & "SELECT concat(q.CHSeries, q.CHNumber) as Chassino,q.Model, "
sql = sql & vbCRLF & "Case q.BRANCH when '6' then 'branch 6' when '2' then 'branch 2' Else 'XX' end Branch, "
sql = sql & vbCRLF & "q.ORDER,q.indt DeofInvoice, q.invn Invoice_no, q.customer Customer, "
sql = sql & vbCRLF & "(select coalesce(sum(amnt*ICVL),0) from lbr2 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts2 where pcno<>0 and q.order=orno)+ "
sql = sql & vbCRLF & "(select coalesce(sum(amnt*ICVL),0) from lbr1 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts1 where pcno<>0 and q.order=orno) "
sql = sql & vbCRLF & "Internal_Sales, "
sql = sql & vbCRLF & "coalesce(case q.InvoiceCredit when 'F' then (q.RetailwithV-q.V) else -1*(q.RetailwithV-q.V)end,0) External_Sales "
sql = sql & vbCRLF & "FROM (select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER1 , PARTS1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER1, LBR1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO "
sql = sql & vbCRLF & "Union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER2 , PARTS2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx, cd2 "
sql = sql & vbCRLF & "FROM HEADER2 , LBR2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO) "
sql = sql & vbCRLF & "where indt ='2019-09-30' order by branch "
Set rs = con.Execute(sql)

Related

Dynamically run strings in a loop

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())

VB for MS Access not working when using INNER JOIN

The code isn't working properly, getting a Syntax error in INSERT INTO statement error. Trying to insert data with an inner join from an MS Access form where the user types in responses and selects a button to add the detail to a new table. Based on the information provided I want it to insert the data into another table where it is joined by a table to get the fiscal data.
stSQL = "INSERT INTO PO_Information " & _
"(Partner, PO_Number, PO_Title, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek)" & _
"Select New_PO_Information.Partner, " & _
"New_PO_Information.PO_Number, " & _
"New_PO_Information.PO_Title, " & _
"New_PO_Information.Cost_Center, " & _
"New_PO_Information.Description, " & _
"New_PO_Information.Line_1_Amt, " & _
"New_PO_Information.Line_2_Amt, " & _
"New_PO_Information.Date_Added, " & _
"New_PO_Information.Month_Added, " & _
"New_PO_Information.Year_Added, " & _
"Fiscal_Calendar.FY, " & _
"Fiscal_Calendar.Qtr, " & _
"Fiscal_Calendar.FY_Qtr, " & _
"Fiscal_Calendar.FW, " & _
"Fiscal_Calendar.FWeek, " & _
"From New_PO_Information INNER JOIN Fiscal_Calendar " & _
"ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar"
stSQL = stSQL & _
"From New_PO_Information " & _
"Where ((New_PO_Information.Partner)<>'') " & _
"AND ((New_PO_Information.PO_Number)<>'') " & _
"AND ((New_PO_Information.PO_Title)<>'') " & _
"AND ((New_PO_Information.Cost_Center)<>'') " & _
"AND ((New_PO_Information.Description)<>'');"
Can someone tell me where I went wrong and correct the code, please?
It's probably this comma here: "Fiscal_Calendar.FWeek, " & _
There shouldn't be a comma before the FROM statement. And make sure you only have one FROM statement:
stSQL = "INSERT INTO PO_Information " & _
"(Partner, PO_Number, PO_Title, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek)" & _
"Select New_PO_Information.Partner, " & _
"New_PO_Information.PO_Number, " & _
"New_PO_Information.PO_Title, " & _
"New_PO_Information.Cost_Center, " & _
"New_PO_Information.Description, " & _
"New_PO_Information.Line_1_Amt, " & _
"New_PO_Information.Line_2_Amt, " & _
"New_PO_Information.Date_Added, " & _
"New_PO_Information.Month_Added, " & _
"New_PO_Information.Year_Added, " & _
"Fiscal_Calendar.FY, " & _
"Fiscal_Calendar.Qtr, " & _
"Fiscal_Calendar.FY_Qtr, " & _
"Fiscal_Calendar.FW, " & _
"Fiscal_Calendar.FWeek " & _
"FROM New_PO_Information INNER JOIN Fiscal_Calendar " & _
"ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar"
stSQL = stSQL & _
" Where ((New_PO_Information.Partner)<>'') " & _
"AND ((New_PO_Information.PO_Number)<>'') " & _
"AND ((New_PO_Information.PO_Title)<>'') " & _
"AND ((New_PO_Information.Cost_Center)<>'') " & _
"AND ((New_PO_Information.Description)<>'');"
If I created an append query, here is the code. Just trying to convert this into my SQL statement that I posted earlier.
INSERT INTO PO_Information ( Partner, PO_Number, PO_TItle, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek )
SELECT New_PO_Information.Partner, New_PO_Information.PO_Number, New_PO_Information.PO_TItle, New_PO_Information.Cost_Center, New_PO_Information.Description, New_PO_Information.Line_1_Amt, New_PO_Information.Line_2_Amt, New_PO_Information.Date_Added, New_PO_Information.Month_Added, New_PO_Information.Year_Added, Fiscal_Calendar.FY, Fiscal_Calendar.Qtr, Fiscal_Calendar.FY_Qtr, Fiscal_Calendar.FW, Fiscal_Calendar.FWeek
FROM New_PO_Information INNER JOIN Fiscal_Calendar ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar;

How Can I merge these two Access Sql queries

I want to merge these two queries into one query.
Here are the codes.
it's one table for both, but you see different data from different parts.
Just I want to make one record with only one query.
> DoCmd.RunSQL "insert into Report (id, [date], namep , [NEXTC],
> [Nurse])" & _ " values (" & Chr(34) & x & Chr(34) & " ," & Chr(34) & b
> & Chr(34) & "," & "5," & _ " " & Chr(34) & c & Chr(34) & ", " &
> Chr(34) & n & Chr(34) & ")"
and this one
DoCmd.RunSQL "INSERT INTO Report ( brand, Bag, Acc, Id , NameP)" & _
"Select Top 1 * from" & _
"(SELECT TOP 1 Brand FROM (SELECT * FROM Report WHERE ID=" & _
x & _
") WHERE Brand IS NOT NULL ORDER BY date DESC Union All SELECT top 1 null FROM report WHERE Brand IS NULL) AS Brand," & _
"(SELECT TOP 1 Bag FROM (SELECT * FROM Report WHERE ID=" & _
x & _
") WHERE Bag IS NOT NULL ORDER BY date DESC Union All SELECT top 1 null FROM report WHERE Bag IS NULL) AS Bag," & _
"(SELECT TOP 1 ACC FROM (SELECT * FROM Report WHERE ID=" & _
x & _
") WHERE ACC IS NOT NULL ORDER BY date DESC Union All SELECT top 1 null FROM report WHERE ACC IS NULL) AS ACC," & _
"(SELECT TOP 1 ID FROM Report WHERE ID=" & _
x & _
") AS ID," & _
"( SELECT TOP 1 NameP FROM Report WHERE ID=" & _
x & _
") as NameP;"
Test this, it should work (maybe some parentheses be wrong, check it before use):
DoCmd.RunSQL "insert into Report (id, [date], namep , [NEXTC], [Nurse], brand, Bag, Acc, Id , NameP)" & _
" values (" & Chr(34) & x & Chr(34) & " ," & Chr(34) & b & Chr(34) & "," & "5," & _
" " & Chr(34) & c & Chr(34) & ", " & Chr(34) & n & Chr(34) & ", " & _
"(SELECT TOP 1 Brand FROM (SELECT * FROM Report WHERE ID=" & x & _
") WHERE Brand IS NOT NULL ORDER BY date DESC Union All SELECT top 1 null FROM report WHERE Brand IS NULL) AS Brand " & ", " & _
"(SELECT TOP 1 Bag FROM (SELECT * FROM Report WHERE ID=" & x & _
") WHERE Bag IS NOT NULL ORDER BY date DESC Union All SELECT top 1 null FROM report WHERE Bag IS NULL) AS Bag " & ", " & _
"(SELECT TOP 1 ACC FROM (SELECT * FROM Report WHERE ID=" & x & _
") WHERE ACC IS NOT NULL ORDER BY date DESC Union All SELECT top 1 null FROM report WHERE ACC IS NULL) AS ACC " & ", " & _
"(SELECT TOP 1 ID FROM Report WHERE ID=" & x & ") AS ID," & _
"( SELECT TOP 1 NameP FROM Report WHERE ID=" & x & ") as NameP )"
Your query is not clear but your query should be something like this:
INSERT INTO Report (id, [date], namep , [NEXTC], [Nurse], brand, Bag, Acc, Id , NameP)
SELECT TOP 1 'value1', 'value2', ..., FROM yourTable ...
Using INSERT INTO SELECT
value1, value2 could be statistic but also you can get some value from yourtable

Combine SQL Insert INTO

I am trying to combine 3 SQL statemnets in VBA so that they show up as one record in a table. For some reason VBA throws a too few parameters error. Here is what my debug print statement outputs:
INSERT INTO Totals
([TOTAL VERIFIED FORMULARIES],[TOTAL AVAILABLE FOR IMPORT],[TOTAL SHOULD BE IMPORTED])
SELECT A.cnt,B.cnt,C.cnt
FROM (SELECT Count([FORMULARY ID]) AS cnt
FROM VerifiedFormularies) AS A,
(SELECT Count([FORMULARY ID]) AS cnt
FROM ImportMetricsIDs) AS B,
(SELECT Count([FORMULARY ID]) AS cnt
FROM ShouldImportMetricsIDsTable
WHERE [IMPORT STATUS] = 'Yes') AS C
And here is my code:
totalVerified = "INSERT INTO Totals([TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED]) " & _
"SELECT A.cnt, B.cnt, C.cnt " & _
"FROM ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM VerifiedFormularies " & _
") AS A " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
") as B " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ShouldImportMetricsIDsTable " & _
"WHERE [IMPORT STATUS]= 'Yes' " & _
") AS C "
I have tried to debug it but was unsuccessful. Any help would be greatly appreciated!
I think i got it. You missed last )
totalVerified = "INSERT INTO Totals([TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED]) " & vbcr & _
"SELECT [TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED] " & vbcr & _
"FROM ( " & vbcr & _
"SELECT COUNT([FORMULARY ID]) as cnt " & vbcr & _
"FROM VerifiedFormularies " & vbcr & _
") AS [TOTAL VERIFIED FORMULARIES], " & vbcr & _
"( " & vbcr & _
"SELECT COUNT([FORMULARY ID]) as cnt " & vbcr & _
"FROM ImportMetricsIDs " & vbcr & _
") AS [TOTAL AVAILABLE FOR IMPORT], " & vbcr & _
"( " & vbcr & _
"SELECT COUNT([FORMULARY ID]) as cnt " & vbcr & _
"FROM ShouldImportMetricsIDsTable " & vbcr & _
"WHERE [IMPORT STATUS]= 'Yes' " & vbcr & _
") AS [TOTAL SHOULD BE IMPORTED] " & vbcr & _
")"
I'd suggest to use the same names in query. Note, that data type must be the same!

Access 2010 VBA SQL INSERT with multiple SELECT

The following Code prompts out with, according to google, a kind of random/default error that doenst really lead to the problem. (Query input must contain at least one table or query)
Private Sub ButtonInsertUntergruppe_Click()
Dim sql As String
sql = "INSERT INTO tblArbeitsschritte (Auftrag_FK, Prozesspunkt_FK, Prozesspunkt) " & _
"VALUES (" & _
Forms!frm_GUI!ID_Auftraege & _
", " & _
"(SELECT ID_Prozesspunkt " & _
"FROM tblProzesspunkt " & _
"WHERE Untergruppe_FK = " & _
Me.cbUntergruppe.Column(0) & _
") , " & _
"(SELECT Prozesspunkt " & _
"FROM tblProzesspunkt " & _
"WHERE Untergruppe_FK = " & _
Me.cbUntergruppe.Column(0) & _
"));"
Debug.Print sql
CurrentDb.Execute sql, dbFailOnError
End Sub
This is what the debugger gives me:
INSERT INTO tblArbeitsschritt
(
Auftrag_FK, Prozesspunkt_FK, Prozesspunkt
)
VALUES
(
1,
(SELECT ID_Prozesspunkt
FROM tblProzesspunkt
WHERE Untergruppe_FK = 1)
,
(SELECT Prozesspunkt
FROM tblProzesspunkt
WHERE Untergruppe_FK = 1)
);
Not sure if that syntax is supported. Personally I would do:
INSERT INTO tblArbeitsschritt
(
Auftrag_FK, Prozesspunkt_FK, Prozesspunkt
)
SELECT 1, ID_Prozesspunkt, Prozesspunkt
FROM tblProzesspunkt
WHERE Untergruppe_FK = 1