I got a SQL statement where am selecting from an access database in vb but I get this error; "syntax error in query expression select sum(brought_qtty)" when I run my program. I imagine am doing the right thing but seems am not. How can I adjust this select? The code is below:
"select distinct(brought_price) as [Price], select sum(brought_qtty) as [Ordinary] from brought_coffee where " & _
"coffee_grade=O, select sum(brought_qtty) as [Premium] from brought_coffee where" & _
"coffee_grade=P, sum(brought_qtty) as [Total Qtty]" & _
", sum(brought_paid) as [paid], " & _
"sum(brought_bal) as [Balance]" & _
"from brought_coffee, farmer where brought_date=#" & dtc.Text.Trim & "# and farmer_centre='" & cc.Text.Trim & _
"' and farmer.farmer_num=brought_coffee.farmer_num"
The second and third select in the query produce a syntax error... Replace select sum(brought_qtty) with just sum(brought_qtty).
Related
I am very new to SQL and think I have a simple problem but was unable to figure it out from other posts. I have the following code:
INSERT INTO tblShortScores ( TradeNum, FilterNum, Rank, ScoreNum )
SELECT [Forms]![frmOpenTrades]![TradeNum] AS TradeNum, tblFilters.FilterNum, tblFilters.SBBExh AS Rank, tblFilters.SBBExh AS Score
FROM tblFilters
WHERE (((tblFilters.SBBExh) Is Not Null));
but instead of using the literal "SBBExh" in tblFilters.SBBExh, I want to do something like
tblFilters.("S" & [Forms]![frmOpenTrades]![Strategy])
where something like
[Forms]![frmOpenTrades]![Strategy] contains the value "BBExh".
It's in MS Access and I seem unable to find a syntax that works
any help is appreciated
Can't dynamically build field name in query object. Use VBA to construct and execute action SQL, like:
strField = "S" & Me.Strategy
CurrentDb.Execute "INSERT INTO tblShortScores (TradeNum, FilterNum, ScoreNum) " & _
"SELECT " & Me.TradeNum & " AS TradeNum, FilterNum, " & strField & " " & _
"FROM tblFilters WHERE " & strField & " Is Not Null;"
Assumes TradeNum is number type - if it is text, use apostrophe delimiters:
SELECT '" & Me.TradeNum & "' AS .
If SQL injection is a concern review, How do I use parameters in VBA in the different contexts in Microsoft Access?
I am trying to update a column in my table to specify if the record is a duplicate or not. To do this I added a field called 'DuplicateRecord'.
I can't use the query wizard in access as the duplicate option in this only allows for 10 fields to be checked as a duplicate and my table has more than 10 fields.
The below code works for me:
Call Module1.RunSQL("UPDATE myTable " & _
"SET myTable.DuplicateRecord = TRUE " & _
"WHERE myTable.[CompanyID] IN (" & _
"SELECT * FROM " & _
"(SELECT myTable.[CompanyID] " & _
"FROM myTable " & _
"GROUP BY myTable.[CompanyID] " & _
"HAVING COUNT(*) > 1 ) T1 ) ")
However this code just runs off one field and I need it to run off all fields in the table (there are about 15 fields).
As a test I tried using two fields to see if I could get this working using the following:
Call Module1.RunSQL("UPDATE myTable " & _
"SET myTable.DuplicateRecord = TRUE " & _
"WHERE myTable.[CompanyID] AND myTable.[Product] IN (" & _
"SELECT * FROM " & _
"(SELECT myTable.[CompanyID], myTable.[Product], COUNT(*) " & _
"FROM myTable " & _
"GROUP BY myTable.[CompanyID], myTable.[Product] " & _
"HAVING COUNT(*) > 1 ) T1 ) ")
However I get an error message saying "Run-time error '3306' You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field."
I've tried googling the error but I can't seem to solve it as I don't fully understand it.
Does anyone know how I can apply the logic of testing for duplicates? Is there an easier way to do this then how I am currently trying? I will need to do this for the full record (15 fields), so I am a bit conscious that the way I am currently attempting this might not be the best fit.
Microsoft Access subform filter output looks like this:
**([qryPOExamDetail subform].[Line Description]="1"" CONDUIT - EMT")**
The actual value in the field is 1" CONDUIT - EMT. I've converted the above argument to [Line Description]='1"" CONDUIT - EMT' but the dynamic query returns zero records.
I've built a SQL statement to create a dynamic query for export to a comma delimited file via VBA doCmd.TransferText function. I filter on other fields (without "inch" marks) and it works fine. I've searched the internet for an answer and cannot find anything.
How do I get SQL to recognize that 1"" CONDUIT - EMT = 1" CONDUIT - EMT?
Below is the SQL string for creating the dynamic query:
strSQL1 = "SELECT tblOpenCommittment.[Job Number], tblOpenCommittment.[Job Name], tblOpenCommittment.[Order Number], tblOpenCommittment.[Supplier Name]," _
& "tblOpenCommittment.[Order Date],tblOpenCommittment.[Cost Code], tblOpenCommittment.[Line Description], tblOpenCommittment.Qty, tblOpenCommittment.Tax, " _
& "tblOpenCommittment.Price, tblOpenCommittment.Unit, tblOpenCommittment.[Total Line Value],tblOpenCommittment.[Total Line Value]-tblOpenCommittment.[Line Total Amount invoiced]" _
& "AS [Open]FROM tblOpenCommittment WHERE (((tblOpenCommittment.[Job Name])=[Forms]![frmPrintByProject]![txtBoxJobName]) AND ((tblOpenCommittment.[Cost Code])" _
& "Like" & Chr(34) & Chr(42) & Chr(34) & Chr(32) & Chr(38) & Chr(32) & "[Forms]![frmPrintByProject]![txtBoxFrameValue]) AND " & strWashedstrFilter & " )"
The very last argument strWashedstrFilter represents the output of the subform filter listed at the head of my original post. As I've stated earlier it works as long as the value filtered on does not contain ( " ).
I've read this site for years and gotten outstanding help. This is the first time I've ever posted a question. Thank you all in advance for taking the time to comment.
As an example, (as I am not really certain of your data structure or columns) you would use something like this for MS Access:
SELECT "My Quote goes > "" < right there"
So, to compare the values it would be like this:
SELECT ...
WHERE [MyColumn] = "1"" CONDUIT - EMT"
Or (if you prefer)
SELECT ...
WHERE [MyColumn] = '1" CONDUIT - EMT'
You are missing some spaces in the construct so words don't run together. And concatenate references to form controls:
strSQL1 = "SELECT *, [Total Line Value]-[Line Total Amount invoiced] AS [Open] " & _
"FROM tblOpenCommittment " &
"WHERE [Job Name]='" & [Forms]![frmPrintByProject]![txtBoxJobName] & _
"' AND [Cost Code] LIKE '*" & [Forms]![frmPrintByProject]![txtBoxFrameValue] & _
"' AND " & strWashedstrFilter
If code is behind frmPrintByProject, can use Me.:
"WHERE [Job Name]='" & Me.txtBoxJobName & "
I am executing SQL in VB6 and this is the string that I am using, where I define currFA as a number 1. I've been debugging and working at this since it's using inner joins and unions (it's a query I made in Access that I am trying to put into VB6 to run). Right now, I'm getting an operator missing error on the run.
sql = "SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEX" _
& "FROM ([~FIRE_AREAS] INNER JOIN [~ALLTARGETS] ON [~FIRE_AREAS].FULL_ID = [~ALLTARGETS].FA) INNER JOIN ([~ALLRWCBL] INNER JOIN [~ALLCBLEQ] ON" _
& "[~ALLRWCBL].Cable = [~ALLCBLEQ].Cables) ON [~ALLTARGETS].TARGET = [~ALLRWCBL].Cable" _
& "WHERE ((([~FIRE_AREAS].INDEX) = '" & currFA & "'))" _
& "UNION" _
& "SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEX" _
& "FROM [~FIRE_AREAS] INNER JOIN (([~ALLTARGETS] INNER JOIN [~ALLRWCBL] ON [~ALLTARGETS].TARGET=[~ALLRWCBL].Raceway) INNER JOIN [~ALLCBLEQ] ON" _
& "[~ALLRWCBL].Cable=[~ALLCBLEQ].Cables) ON [~FIRE_AREAS].FULL_ID=[~ALLTARGETS].FA" _
& "WHERE ((([~FIRE_AREAS].INDEX) = '" & currFA & "'));"
Any help on the error and syntax improvement tips are greatly appreciated! Still learning!
Thanks!
Your string has no spaces between the lines so when you write:
sql = "SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEX" _
& "FROM ([~FIRE_AREAS] …
That becomes a SQL string like
SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEXFROM ([~FIRE_AREAS] …
Which is obviously invalid SQL because there's no space between INDEX and FROM.
An easy solution is just to put a space before each closing quote (or after each open quote):
sql = "SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEX " _
& "FROM ([~FIRE_AREAS] …
However, If you find yourself writing relatively complex queries like this, I would recommend that you consider converting them to stored procedures or views instead.
Query gives Syntax error missing operator in query '[Relationship Manager]=John DOE'
BCA_Source = " SELECT distinct [Account Data Table less DTA CHD].BCA_ICA, " _
& "[Account Data Table less DTA CHD].[Relationship Manager]" _
& " FROM [Account Data Table less DTA CHD] " _
& " WHERE [Relationship Manager]= " _
& Me.cmb_Mngr.Value,
Thanks,
Sury
Initial guess would be that Me.cmb_Mngr.Value has to be wrapped with escaped quotes.