vba sql statement error "missing operator" - sql

I can't get it work in VBA althought it works within access.
Which is the mistake?
Private Sub Command517_Click()
Dim SQL As String
SQL = "SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], MacroAttività.ID," & _
" MacroAttività.Descrizione, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note,operatore.Nome, Tipologia.Tipologia " & _
" FROM Tipologia INNER JOIN (operatore INNER JOIN (MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività]) " & _
" ON operatore.ID = MacroAttività.Leader) ON Tipologia.ID = MacroAttività.Tipologia WHERE " & _
"(((Tags.Tag) = [Forms]![MacroAttività]![Text511]) " & _
" UNION ALL SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], " & _
" MacroAttività.Descrizione, MacroAttività.ID, " & _
" MacroAttività.Leader, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note, MacroAttività.Tipologia " & _
" FROM MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività] " & _
" WHERE (((Tags.Tag)=[Forms]![MacroAttività]![Text513])); "
DoCmd.RunSQL SQL
End Sub

Form variables have to be read from outside of the SQL statement, so my solution closes the SQL statement adds the form variable and then reopens the SQL quotation. I have added string qualifiers(single quotes) around the form variables; assuming they are strings by the use of "text".
varSQL = "SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], MacroAttività.ID," & _
" MacroAttività.Descrizione, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note,operatore.Nome, Tipologia.Tipologia " & _
" FROM Tipologia INNER JOIN (operatore INNER JOIN (MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività]) " & _
" ON operatore.ID = MacroAttività.Leader) ON Tipologia.ID = MacroAttività.Tipologia WHERE " & _
"(((Tags.Tag) ='" & [Forms]![MacroAttività]![Text511] & "')" & _
" UNION ALL SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], " & _
" MacroAttività.Descrizione, MacroAttività.ID, " & _
" MacroAttività.Leader, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note, MacroAttività.Tipologia " & _
" FROM MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività] " & _
" WHERE (((Tags.Tag)='" & [Forms]![MacroAttività]![Text513] & "')); "
SQL is a reserved word so change your SQL variable name to something like varSQL. (Update: not a reserved word)
currentdb.execute and domcd.runSQL only work for action queries as far as I am aware. you cant execute a select query using these methods. try the following:
dim rs as recordset
dim varSQL as string
varSQL = "SELECT..."
set rs=currentdb.openrecordset(varSQL,dbOpenDynaset)
me.[text1] = rs.fieldname
'send recordset values to form
rs.close

Related

SQL UPDATE function syntax error

I am attempting to write an UPDATE function based on values that will be input into dropdown boxes and text boxes by a user
My code is as follows:
Private Sub cmdUpdate_Prices_Click()
Dim SQL_string As String
SQL_string = _
"UPDATE tblPrice " & _
"SET [P/N] = " & Me.txtPartNumber.Column(0) & ", " & _
"Type = " & Me.txtPriceType.Column(0) & ", " & _
"Region = " & Me.txtRegion.Column(0) & ", " & _
"Price = " & Me.txtPrice.Value & ", " & _
"WHERE ID = " & Me.txtID.Value & ";"
DoCmd.RunSQL SQL_string
End Sub
I keep getting a syntax error in the UPDATE function and I'm not sure where it is.
Any help is appreciated. Thanks!

SQL Query works in MS-Access but cannot implement it in vb.net form

The block of code immediately following (given to me by a stackoverflow solver) works perfectly in MS-Access. I'm trying to convert it to work in a vb.net form accessing the very same MS-Access database. I get an error and cannot see my mistake. Are there any vb.net coders that can see what I'm doing wrong. The first block of code works in MS-Access and is the code I'm trying to convert. And the second block of code is my conversion attempt.
SELECT at.animalID, amt.milestoneType
FROM
animals_Table at
LEFT JOIN
(
SELECT animalID, milestoneType
FROM animalMilestones_Table
WHERE milestoneType = 'Intake'
) amt
ON at.animalID = amt.animalID
Now, my conversion attempt:
dim selectAnimal as string
selectAnimal = "SELECT at.animalID, amt.milestoneType" & _
" FROM animals_Table at" & _
" LEFT JOIN" & _
" (" & _
" SELECT animalID, milestoneType" & _
" FROM animalMilestones_Table" & _
" WHERE milestoneType = '" & "Intake" & "'" & _
" ) amt" & _
" ON at.animalID = amt.animalID"
The error code I get is
!ErrorInfo.GetDescription failed with E_FAIL(0x80004005)
It appears that ACE.OLEDB doesn't like at as a table alias. Try this instead
Dim selectAnimal As String
selectAnimal = "SELECT atbl.animalID, amtbl.milestoneType" & _
" FROM animals_Table atbl" & _
" LEFT JOIN" & _
" (" & _
" SELECT animalID, milestoneType" & _
" FROM animalMilestones_Table" & _
" WHERE milestoneType = '" & "Intake" & "'" & _
" ) AS amtbl" & _
" ON atbl.animalID = amtbl.animalID"

Error SQL in VBA Access 2010

I'm trying to write a query in MS Access 2010 in order to use it to print a report, but it gives me "missing parameter" error in "set qd" line, hereunder is the code i wrote, can you please help me and tell me what is wrong with my code:
`Private Sub Command5_Click()
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strSql As String
Dim strFrom, strTo As String
strFrom = [Forms]![FrmPrintSelection]![txtFrom]
strTo = [Forms]![FrmPrintSelection]![txtTo]
strSql = "SELECT tblInvoiceHead.CustomerNumber,
tblCustomers.AccountName,tblCustomers.Address,
tblCustomers.Phone1, tblCustomers.Phone2," _
& "tblCustomers.Mobile1, tblCustomers.Mobile2, tblInvoiceHead.InvoiceNumber,
tblInvoiceHead.InvoiceDate, tblInvoiceHead.TotalInvoice," _
& "tblInvoiceHead.CashDiscount, TblInvoiceDetails.Item, TblInvoiceDetails.Unit,
TblInvoiceDetails.Qtn, TblInvoiceDetails.Price," _
& "TblInvoiceDetails.[Discount%], TblInvoiceDetails.CashDiscount,
TblInvoiceDetails.NetUnitPrice, TblInvoiceDetails.TotalPrice, tblInvoiceHead.InvoiceType" _
& "FROM (tblCustomers INNER JOIN tblInvoiceHead ON tblCustomers.AccountNumber =
tblInvoiceHead.CustomerNumber) INNER JOIN TblInvoiceDetails" _
& "ON tblInvoiceHead.InvoiceNumber = TblInvoiceDetails.InvoiceNumber" _
& "WHERE (((tblInvoiceHead.InvoiceNumber) Between " & strFrom & " And " & strTo & "))"
Set qd = CurrentDb.CreateQueryDef("RepInv", strSql)
Set rs = qd.OpenRecordset
'DoCmd.OpenQuery "repinv", strSql
Reports!repinvoicetest.RecordSource = "repinv"
DoCmd.OpenReport "repinvoicetest", acViewPreview
End Sub
`
Usually the error "missing parameter" means that you spelled one of your columns wrong. If you take your sql and paste it into a new query (temp, don't save) and run it, the misspelled column will pop up a window asking you to provide a value for that "parameter" (because MSAccess is assuming that you never would misspell a column name).
In your query above, you might have copy/pasted it wrong, but if not, then you don't have enough spaces between your words as you continue them on the next line. For instance, your SQL string would end up having some stuff in it like "InvoiceTypeFROM", because you didn't have an extra (necessary) space in there.
Try this query instead:
strSql = "SELECT tblInvoiceHead.CustomerNumber, " _
& " tblCustomers.AccountName,tblCustomers.Address, " _
& " tblCustomers.Phone1, tblCustomers.Phone2, " _
& " tblCustomers.Mobile1, tblCustomers.Mobile2, tblInvoiceHead.InvoiceNumber, " _
& " tblInvoiceHead.InvoiceDate, tblInvoiceHead.TotalInvoice, " _
& " tblInvoiceHead.CashDiscount, TblInvoiceDetails.Item, TblInvoiceDetails.Unit, " _
& " TblInvoiceDetails.Qtn, TblInvoiceDetails.Price, " _
& " TblInvoiceDetails.[Discount%], TblInvoiceDetails.CashDiscount, " _
& " TblInvoiceDetails.NetUnitPrice, TblInvoiceDetails.TotalPrice, " _
& " tblInvoiceHead.InvoiceType " _
& " FROM (tblCustomers INNER JOIN tblInvoiceHead " _
& " ON tblCustomers.AccountNumber = tblInvoiceHead.CustomerNumber) " _
& " INNER JOIN TblInvoiceDetails " _
& " ON tblInvoiceHead.InvoiceNumber = TblInvoiceDetails.InvoiceNumber " _
& " WHERE (((tblInvoiceHead.InvoiceNumber) Between " & strFrom & " And " & strTo & "))"
Notice how I added a lot of unncessary spaces at the begining and end of each line. All of those extra spaces will be ignored. However, if there are too few, then you will get errors. It is a simple trick that I stick-with.

VBA sql string error: missing operator

Writing an sql string in vba and getting syntax error for a missing operator. I thought the error was with the inner joins so I tried taking them out but still same error.
Here is the query string:
sql1 = "SELECT WorkOrder.ProjectID, tref_dep.department, live_project.project_codename " _
& "FROM WorkOrder " _
& "INNER JOIN tlive_project " _
& "ON tlive_project.project_id = WorkOrder.ProjectID " _
& "INNER JOIN tref_dep " _
& "ON tref_dep.dep_id = WorkOrder.ToDepartment " _
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& " CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"
I am probably missing something simple but any help is greatly appreciated!
You have forgotten a logic operator before CONTAINS(..) :
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& "AND CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"
VBA requires parenthesis for 2 or more JOIN clauses.
sql1 = "SELECT WorkOrder.ProjectID, tref_dep.department, live_project.project_codename " _
& "FROM (WorkOrder " _
& "INNER JOIN tlive_project " _
& "ON tlive_project.project_id = WorkOrder.ProjectID) " _
& "INNER JOIN tref_dep " _
& "ON tref_dep.dep_id = WorkOrder.ToDepartment " _
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& " CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"

How do I delete files from a table in MSDE 2000 that is selected by 3 joins?

I have a VB6 program that uses a n Access backend. The query that I am currently using is
sQuery = "DELETE tblResultNotes.* " & _
"FROM (tblJobs INNER JOIN tblResults ON tblJobs.JobID=tblResults.JobID) INNER JOIN tblResultNotes ON tblResults.ResultID=tblResultNotes.ResultID " & _
"WHERE (tblJobs.CreateDate)< #" & strDate & "# " & _
"AND tblResults.StartTime < #" & strDate & "#;"
I have changed my backend to MSDE 2000 and now this query is giving me a syntax error near '*'. Could someone help me out?
Thanks,
Tom
Try changing your SQL to this:
sQuery = "DELETE FROM tblREsultNotes " & _
"FROM " & _
" tblJobs" & _
" INNER JOIN tblResults ON tblJobs.JobID=tblResults.JobID" & _
" INNER JOIN tblResultNotes ON tblResults.ResultID=tblResultNotes.ResultID" & _
"WHERE tblJobs.CreateDate < '" & strDate & "'" & _
"AND tblResults.StartTime < '" & strDate & "'"
Note the date delimiter change to ' instead of #.