How to find a syntax error in my SQL code? - sql

I am trying to compile my code but I get the same error every time:
Syntax error (missing operator ) in query expression
' True Status.Subsystem Not LIKE '''
This is my code :
Sub Import_Loop_Check_list()
Dim strSQL As String
Dim SS_sel As String
Dim rcrd As DAO.Recordset
If IsNull(Cobsubsystem) Then
SS_sel = "True"
Else
If IsNull(Logic1) Then
SS_sel = "Status.Subsystem LIKE '" & Cobsubsystem & "' "
Else
SS_sel = "Status.Subsystem NOT LIKE '" & Cobsubsystem & "' "
End If
End If
strSQL = " SELECT DISTINCT LOOP_JB.Loop_name, [Easyplant Dump query].Subsystem, LOOP_JB.PANEL_FROM, LOOP_JB.ITR_PANEL_FROM, LOOP_JB.ITR_PANEL_FROM_state, LOOP_JB.CABLE_NUM, LOOP_JB.ITR_cable, LOOP_JB.ITR_STATE_Cable, LOOP_JB.Cabinet_JB, LOOP_JB.ITR_Cabinet_JB, LOOP_JB.ITR_STATE_CABINET, Multicors.CABLE_NUM AS Multicore, Multicors.ITR_PANEL_FROM, Multicors.ITR_PANEL_FROM_state, [Cabinet query].PANEL_TO, [Cabinet query].ITR_PANEL_TO, [Cabinet query].ITR_PANEL_TO_state INTO [LOOP_Check] " & _
" FROM (LOOP_JB INNER JOIN ([Cabinet query] RIGHT JOIN Multicors ON [Cabinet query].CABLE_NUM = Multicors.CABLE_NUM) ON LOOP_JB.Loop_name = Multicors.Loop_name) INNER JOIN [Easyplant Dump query] ON LOOP_JB.Loop_name = [Easyplant Dump query].Clean_Tag_Number" & _
" WHERE True " & SS_sel & strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
DoCmd.OpenTable "LOOP_Check"
End Sub

I looked at this again -- something else does not make sense.
You reference Status.Subsystem in the WHERE but there is no table named Status -- Did you not include the full query?
original answer
I think the error message is clear -- you have a strange where statement
WHERE True Status.Subsystem Not LIKE
you probably mean
WHERE Status.Subsystem Not LIKE
so change this line
" WHERE True " & SS_sel & strSQL
to this
" WHERE " & SS_sel & strSQL
Also, it does not right to me -- are you sure you want to do a RIGHT join to Multicors and not a left join? You want a row in your result for every row in the multicors table?

Related

Executing a SQL query from vba results with nothing

Cheers,
I am trying to send a simple SQL query, but I get no results in my recordset as if it simply was not executed.
Just after, I am executing a different query, which results correctly.
When running the same query from SMSS, do brings up the desired results.
Following is the code:
If CheckLogsFromRestartDay = True Then
'This line does NOT seem to work. Nothing is changed in the Recordset
Set objMyRecordset = objMyConn.Execute( _
" SELECT top 1 cast(tValue AS date) AS RestartDate " & _
" FROM settings (nolock) WHERE tkey = 'EngineStartTime' ")
'This line DOES provide an answer correctly
Set objMyRecordset = objMyConn.Execute( _
" SELECT * FROM " & LogTable & " (nolock) where Webpage like 'Engines%'" & _
" ORDER BY id desc")
sTodayMMDD = objMyRecordset!RestartDate
Else
sTodayMMDD = Format(Date, "yyyy-mm-dd")
End If

SQL query works in access but not in excel

I need to get data from a recordset. The SQL query works fine in MS Access and returns exactly the expected values, but when the same query is lunched in VBA Excel, I get the following error:
No value given for one or more required parameters
Do you have any ideas why this problem occurs?
Thank you.
Philippe-Olivier Roussel
Private Sub CBtype_AfterUpdate()
Dim strConnexion As String
Dim connexion As New ADODB.Connection
strConnexion = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Database & ""
connexion.Open strConnexion
Dim rsMarque As New ADODB.Recordset
Dim seltype As String
seltype = CBtype.Value
rsMarque.Open "SELECT DISTINCT tblMarque.marque_nom FROM tblMarque, tblModele WHERE " & _
" tblMarque.marque_id = tblModele.marque_id AND tblModele.marque_id IN " & _
" (SELECT DISTINCT tblModele.marque_id FROM tblModele, tblType " & _
" WHERE tblModele.type_id = tblType.type_id AND tblModele.type_id = " & _
" (SELECT tblType.type_id FROM tblType WHERE " & _
" (tblType.type_nom = " & seltype & ")))", connexion, adOpenStatic
rsMarque.MoveFirst
With UserForm2.CBmarque
.Clear
Do
.AddItem rsMarque!marque_nom
rsMarque.MoveNext
Loop Until rsMarque.EOF
End With
End Sub
This error message looks like an output from the DBMS rather than Excel
I think you might be missing the apostrophe before and after your string variable. See if
" (tblType.type_nom = '" & seltype & "')))" works (I'm assuming the column you're querying is varchar type, since you declared seltype as string)

sql where clause variable

I am using VBA and SQL. My VBA looks like
Dim SystemLookup As String
Dim SqlString As String
If IsNull(Me!SystemLookup) Then
Me!SystemLookup = ""
Else
SystemLookup = Me!SystemLookup
End If
SqlString = "SELECT TblField.*, TblOrigin.SystemID" _
& " FROM TblField INNER JOIN TblOrigin ON TblField.OriginID = TblOrigin.OriginID " _
& " WHERE (((TblOrigin.SystemID) = " & systemlookup )) ""
Me.RecordSource = SqlString
End Sub
I have my bracketing on the where clause is wrong but I am not sure how to fix. Any sugguestions (Systemlookup is a variable that is being passed from a form)
You need to put the brackets inside the string, not besides the variable.
Try this:
SqlString = "SELECT TblField.*, TblOrigin.SystemID" _
& " FROM TblField INNER JOIN TblOrigin ON TblField.OriginID = TblOrigin.OriginID " _
& " WHERE (((TblOrigin.SystemID) = " & systemlookup & ")) "
You are missing an ampersand and a double quote and have an extra opening bracket. The brackets are not needed in this simple query.
SqlString = "SELECT TblField.*, TblOrigin.SystemID" _
& " FROM TblField INNER JOIN TblOrigin ON TblField.OriginID = TblOrigin.OriginID " _
& " WHERE TblOrigin.SystemID = " & systemlookup & ";"
You would bracket WHERE clauses when you need to group conditions.
For example if you wanted to know if a product was shipped between two dates or if the product wasn't shipped then you would bracket it.
All three of these examples are valid:
WHERE ([Shipped Date]>=#10/28/2017# AND [Shipped Date]<=#10/30/2017#) OR [Shipped Date] is Null
WHERE ([Shipped Date]>=#10/28/2017# AND [Shipped Date]<=#10/30/2017#) OR ([Shipped Date] is Null)
WHERE (([Shipped Date]>=#10/28/2017# AND [Shipped Date]<=#10/30/2017#) OR ([Shipped Date] is Null))

Run Time error 3061 Too Few parameters. Expected 6. Unable to update table from listbox

All,
I am running the below SQL and I keep getting error 3061. Thank you all for the wonderful help! I've been trying to teach myself and I am 10 days in and oh my I am in for a treat!
Private Sub b_Update_Click()
Dim db As DAO.Database
Set db = CurrentDb
strSQL = "UPDATE Main" _
& " SET t_Name = Me.txt_Name, t_Date = Me.txt_Date, t_ContactID = Me.txt_Contact, t_Score = Me.txt_Score, t_Comments = Me.txt_Comments" _
& " WHERE RecordID = Me.lbl_RecordID.Caption"
CurrentDb.Execute strSQL
I am not sure but, you can try somethink like that
if you knom the new value to insert in the database try with a syntax like this one
UPDATE table
SET Users.name = 'NewName',
Users.address = 'MyNewAdresse'
WHERE Users.id_User = 10;
Now, if you want to use a form (php)
You have to use this
if(isset($_REQUEST["id_user" ])) {$id_user = $_REQUEST["id_user" ];}
else {$id_user = "" ;}
if(isset($_REQUEST["name" ])) {$name= $_REQUEST["name" ];}
else {$name = "" ;}
if(isset($_REQUEST["address" ])) {$address= $_REQUEST["adress" ];}
else {$adress= "" ;}
if you use mysql
UPDATE table
SET Users.name = '$name',
Users.address = '$adress'
WHERE Users.id_User = 10;
i don't know VBA but I will try to help you
Going on from my comment, you first need to declare strSQL as a string variable.
Where your error expects 6 values and access doesn't know what they are. This is because form objects need to be outside the quotations of the SQL query, otherwise (as in this case) it will think they are variables and obviously undefined. The 6 expected are the 5 form fields plus 'strSQL'.
Private Sub b_Update_Click()
Dim db As DAO.Database
dim strSQL as string
Set db = CurrentDb
strSQL = "UPDATE Main" & _
" SET t_Name = '" & Me.txt_Name & "'," & _
" t_Date =#" & Me.txt_Date & "#," & _
" t_ContactID =" & Me.txt_Contact & "," & _
" t_Score =" & Me.txt_Score & "," & _
" t_Comments = '" & Me.txt_Comments & "'," & _
" WHERE RecordID = '" & Me.lbl_RecordID.Caption & "';"
CurrentDb.Execute strSQL
end sub
Note how I have used double quotes to put the form fields outside of the query string so access knows they aren't variables.
If your field is a string, it needs encapsulating in single quotes like so 'string'. If you have a date field it needs encapsulating in number signs like so #date# and numbers/integers don't need encapsulating.
Look at the code I have done and you can see I have used these single quotes and number signs to encapsulate certain fields. I guessed based on the names of the fields like ID's as numbers. I may have got some wrong so alter where applicable... Or comment and I will correct my answer.

query syntax error...

I have the following coding for a button. My problem is that the Query "SQLStory" is comming up with an error that it is missing a semi colon.
The combobox contains the item name and is ordered by the product ID and the SQLStory is supposed to move all items from the TblTotalSale to the table TblSaleStore. Any Ideas where the error is?
Private Sub StockOK_Click()
Dim SQLDelete1 As String
Dim SQLDelete2 As String
Dim SQLUpdate As String
Dim SQLStory As String
SQLDelete1 = "DELETE * FROM TblStock WHERE TblStock.ProductID = " & CboStockItem.Value
SQLDelete2 = "DELETE * FROM TblTotalSale WHERE TblTotalSale.ProductID = " & CboStockItem.Value
SQLUpdate = "INSERT INTO TblStock (ProductID, StockLevel) VALUES ( " & Me.CboStockItem.Value & "," & Me.TxtStockValue & " )"
SQLStory = "INSERT INTO TblSaleStore (ProductID) VALUES (TblTotalSale.ProductID) FROM TblTotalSale WHERE TblTotalSale.ProductID = " & Me.CboStockItem.Value
If IsNull(Me.TxtStockValue) Then MsgBox "Please Select An Item To Update Stock And Ensure A Value Has Been Entered" Else:
DoCmd.RunSQL SQLDelete1
DoCmd.SetWarnings False
DoCmd.RunSQL SQLStory
DoCmd.RunSQL SQLDelete2
DoCmd.RunSQL SQLUpdate
DoCmd.SetWarnings True
End Sub
Another problem I am having with this code is that the block of doCmd was happening whether the txt box TxtStockValue was null or not, and I only want them to happen if the box is not null... Any Ideas on that part either?
Thanks
Sam
Values are for just that, values such as 'abc' or 123, you need SELECT:
SQLStory = "INSERT INTO TblSaleStore (ProductID) " _
& "SELECT (TblTotalSale.ProductID) FROM " _
& "TblTotalSale WHERE TblTotalSale.ProductID = " _
& Me.CboStockItem.Value
But the above is odd, because you already have the ID in the combo, so, as I said in your previous post on the topic:
SQLStory = "INSERT INTO TblSaleStore (ProductID) " _
& "VALUES ( " & Me.CboStockItem.Value & " )"
Also, it was suggested to you that you should use debug.print when using SQL, this would allow you to view the SQL and paste it into the query design window to see if it worked. The debug.print line can be commented out when everything is working. When you are unfamiliar with SQL, there is a lot to be said for using the query design window to build your queries. You can then cut the SQL from SQL View and add quotes etc.
EDIT re Question Part 2
Dim db As Database
Set db = CurrentDB
If IsNull(Me.TxtStockValue) Then
MsgBox "Please Select An Item To Update Stock " _
& "And Ensure A Value Has Been Entered"
Else
db.Execute SQLDelete1, dbFailOnError
''DoCmd.SetWarnings False
db.Execute SQLStory, dbFailOnError
db.Execute SQLDelete2, dbFailOnError
db.Execute SQLUpdate, dbFailOnError
''DoCmd.SetWarnings True
End If