I need to insert multiple rows and fields in Access 2016 with SQL VBA. I'm doing this thing below -with union -but it doesn't work, it says my sintax with union is wrong. I need to figure out what's wrong otherwise run queries one by one and I have 9 like this!
(EDIT: solution found!)
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier) SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & " union "
query = query & "SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & ""
Here is the answer of what worked, very similar from a code someone showed me below but a differences that changed everything (no error messages and it does insert in the table!). I had to put the selects into a select, still gave me an error and for some reason, when I added the -> as Price and as Quantity it worked. I'm still not sure why it works now, let me know if you have an explanation!
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier ) SELECT * FROM (SELECT qryTotals.JRN as Quantity, qryTotals.[Rate JRN] as Price, qryTotals.MMDCarrier FROM qryTotals where qryTotals.MMDCarrier like '" & Me.Combo0 & "'"
query = query & " UNION SELECT qryTotals.MG , qryTotals.[Rate MG], qryTotals.MMDCarrier From qryTotals where qryTotals.MMDCarrier like '" & Me.Combo0 & "')"
If you're using INSERT INTO ... SELECT with unions, you need to put all unions in a subquery, e.g.:
INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier)
SELECT * FROM (
SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN], qryTotals.MMDCarrier from qryTotals
where qryTotals.MMDCarrier like 'something'
UNION
SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],
qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like 'somethingElse'
) As A
Or, in your VBA implementation:
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier) SELECT * FROM (SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & " union "
query = query & "SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & ") As A"
Note that your query also contains a trailing comma in the INSERT INTO part, which isn't a syntax error in Access SQL, but is one in most other forms of SQL, and still is a bad practice. Also, LONG was probably right, you want those single quotes (but really, you should use parameters instead)
Related
I have a table with a few dozen fields, 18 of which are fields with varying integer values. I'd like to pull the field names and corresponding values for the five fields with the greatest values for a given record. I'll use the below table as example where I'd like to query only the two greatest values for a given record.
Name, FigureA, FigureB, FigureC
John, 40, 73, 81
Luke, 35, 21, 65
I'd like to return the following for, say, John:
FigureB, 73
FigureC, 81
I've gotten this far:
sSQL = "Select t.* " & _
"From (Select 'A' as [FigureA], FigureA " & _
"From Table) as t " & _
"Union All " & _
"Select t.* " & _
"From (Select 'B' as [FigureB], FigureB " & _
"From Table) as t " & _
"Union All " & _
"Select t.* " & _
"From (Select 'C' as [FigureC], FigureC " & _
"From Table) as t"
In MS Access, this is probably simplest with union all:
select top (5) *
from (select t.*
from (select top (1) "field1" as colname, max(field1) as max_val
from t
) as t
union all
select t.*
from (select top (1) "field2" as colname, max(field2)
from t
) as t
union all
. . .
) as t
order by max_val desc;
Some versions of MS Access don't support union all in the from clause. That component might require a view.
I have two tables and I would like to have an sql query to get the minimum value of a field of the union of the maximums of the tables.
Set tmpRS = CurrentDb.OpenRecordset(" SELECT MIN(s.TorqueMax) FROM ( " & _
" (SELECT MAX(ds.TorqueMax) FROM tblUIOpTorqueRangeDS ds)" & _
" UNION " & _
" (SELECT MAX(cv.TorqueMax) FROM tblUIOpTorqueRangeCV cv )) as s")
You can do something like this:
SELECT MIN(s.TorqueMax)
FROM (SELECT MAX(ds.TorqueMax) as TorqueMax
FROM tblUIOpTorqueRangeDS ds
UNION
SELECT MAX(cv.TorqueMax) as TorqueMax
FROM tblUIOpTorqueRangeCV cv
) as s;
I am trying to Count the distinct Number of UserID's in a table for each Weekday (e.g. 545 UserID's on Weekday 1 = Monday, 120 UserID's on Weekday 2 = Tuesday etc.). I am doing this in Access Visual Basic, but the syntax should be universal to SQL. Here is my VB Code:
sSQL = " SELECT Weekday(" & tablename & ".[DATE]) AS WEEKDAY, Count(DISTINCT " & tablename & ".[UserID]) AS User_COUNT"
sSQL = sSQL & " FROM " & tablename
sSQL = sSQL & " GROUP BY Weekday(" & tablename & ".[DATE])"
qdf.SQL = sSQL
The plain SQL Syntax should look like this (edited based on comments & test):
SELECT Weekday(tbl.[Date]) AS WEEKDAY, Count(DISTINCT tbl.[UserID]) AS User_COUNT
FROM tbl
GROUP BY Weekday(tbl.[Date])
..whereas [Date] is a field in tbl formatted as Datetime and [UserID] is a field formatted as Long (with duplicates).
When I try to run the command it tells me "Missing Operator in Query-Syntax.."
Is this a problem of my VB Code or is the SQL Syntax wrong?
MS Access database engine does not support COUNT(DISTINCT ...).
To workaroud it, please see this thread: SQL : how can i count distinct record in MS ACCESS where author suggests to solve issue by using subquery:
SELECT
user_id
, COUNT(*) AS count_distinct_clients
FROM
( SELECT DISTINCT
user_id,
client_id
FROM tbl_sActivity
) AS tmp
GROUP BY
user_id ;
Change the query code to your needs.
[EDIT]
SELECT
wday, COUNT(UserId) AS count_distinct_users
FROM
( SELECT DISTINCT WEEKDAY([Date]) AS wday, UserId
FROM tblName
) AS tmp
GROUP BY
wday;
I am trying to create a paging system and came across a post in SO that included code for achieving this. However, when I run my query, it throws the quoted error. I have double checked code but cannot see error. I am using access 2010 as db. Can someone point out my mistake. Thanks
The SELECT statement includes a reserved word or an argument name that
is misspelled or missing, or the punctuation is incorrect.
Dim Row_Per_Page As Integer = 4
Dim TotRows As Integer = 17
Dim Page_Number As Integer = 2
Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP '" & Row_Per_Page & "' *, Count(*) As '" & TotRows & "' From [Select Top('" & TotRows & "' - (('" & Page_Number & "' - 1) * '" & Row_Per_Page & "'))From Postings Order By [Date] DESC] Order By [Date] ASC", oledbCnn)
It looks to me like you have a problem towards the end of your query, specifically this part.
From Postings Order By [Date] DESC] Order By [Date] ASC"
First you have two Order By's, and second I think you forgot a ', [' before the 'DESC]' at the '[Date] DESC]' of your first Order By.
Edit: That link was very helpful. It's hard to read such a long query like the one you posted on SO when it's just one line long.
Forget what I said before. I think the problem is in bold here.
"Select TOP '" & Row_Per_Page & "' , Count() As '" & TotRows & "' From [Select Top('" & TotRows & "' - (('" & Page_Number & "' - 1) * '" & Row_Per_Page & "'))From Postings Order By [Date] DESC] Order By [Date] ASC"
You're Naming your count field 17, and screwing up the code listed at that site. A select Count query should look like this.
SELECT Count(*) AS TOTAL FROM Table1
That would return a field called 'TOTAL' in Table1 that tells you how many records exist there.
Use the below for your query and it should work.
Select TOP Row_Per_Page *, Count(*) AS TOTAL From [Select TOP (TotRows - ((Page_Number - 1) * Row_Per_Page) From Postings Order By ColumnName DESC] Order By ColumnName ASC
I have the following SQL string:
sSQL_Select = "SELECT distinct local_insurer, subsidiary, location FROM T_WILMA WHERE PARENT=" & lParent_ID & _
" AND ACC_YEAR=" & lAcc_Year_ID & " AND ZOMBIE = FALSE GROUP BY local_insurer ORDER BY " & _
"subsidiary ASC, location ASC;"
Access throws an error because I cannot do this.
What I need is all records with a distinct local insurer, but in alphabetical order of the subsidiary and location fields.
Somehow, I never seem to get the right results.
Any ideas?
Remove the GROUP BY local_insurer from the query. (distinct will ensure you get only unique combinations of values.)
The reason that you're getting the error is because you did not include all the fields in the SELECT in GROUP BY. For example , if you SELECT a,b,c , you need to GROUP BY a,b,c (or c,b,a...) .
sSQL_Select = "SELECT distinct local_insurer, subsidiary, location FROM T_WILMA WHERE PARENT=" & lParent_ID & _
" AND ACC_YEAR=" & lAcc_Year_ID & " AND ZOMBIE = FALSE GROUP BY local_insurer, subsidiary, location ORDER BY " & _
"subsidiary ASC, location ASC;"