So i have these two query in my access VBA code. The When I replace like with = i end up retrieving no records. the like works perfectly with out a hitch but I can't use it because sometimes it'll pull up the wrong data. What am I doing wrong with the = operator?
Set rsStepCalendar = db.OpenRecordset("Select * from tblStepCalendar " & _
"Where (groupNr = '*" & txtGroupNum.Value & "*' ) " & _
"AND (Cancel = False)", dbOpenDynaset)
Set rs = db.OpenRecordset("Select * from tblContacts " & _
"Where (groupNum = '*" & txtGroupNum.Value & "*' ) " & _
"AND (canceledContact = False)", dbOpenDynaset)
groupNum = '*" & txtGroupNum.Value & "*'
Is looking for the value surrounded by asterisk characters which only have symbolic meaning as "anything" when combined with LIKE.
For = use groupNum = '" & txtGroupNum.Value & "'
You should also escape any user input/use parametrized queries.
Related
I'm unsure whether or not this is the best way to accomplish what I'm attempting to accomplish, but I'm currently attempting to alter the active filters on a listbox using multiple text boxes.
I have one main search box that filters an initial list box, this will also filter a few of my other list boxes that also populate relevant data.
When I look into my other list boxes (not my main one) I have data that I would like to filter further. So, I go into another textbox and input whatever it is I'm attempting to sort out, when I click the second "search button" this is where I get my error. It's a syntax error, but I don't fully understand the syntax behind it.
I'd like for my initial filter to be kept, in addition to my new criteria for searching.
Here's the code behind my initial search button (which this works)
Private Sub Command37_click()
Dim sql As String
Dim sql2 As String
sql = "SELECT People.LName, People.[Phone #], People.State " _
& "FROM People" _
& "WHERE SystemLocation LIKE '*" & Me.SearchTxt1 & "*' " _
& "ORDER by People.LName "
me.List35.RowSource = sql
me.list35.requery
sql2 = "SELECT Orders.Item, Orders.Price, Orders.Department " _
& "FROM People INNER JOIN Orders " _
& "ON People.SystemLocation = Orders.Department " _
& "WHERE Department LIKE '*" & Me.SearchTxt1 & "*' " _
& "ORDER by Orders.Department"
Me.List41.RowSource = sql2
Me.List41.Requery
Here's the code that I was trying to use with the secondary filter
Dim sql2 As String
sql2 = "SELECT Orders.Item, Orders.Price, Orders.Department " _
& "FROM People INNER JOIN Orders " _
& "ON People.SystemLocation = Orders.Department " _
& WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') & (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
& "ORDER by Orders.Department"
Me.List41.RowSource = sql2
Me.List41.Requery
Ideally I'd like to be able to create a "search engine" of sorts.
The syntax error is surrounding your where clause.
Firstly, you are missing a double-quote here:
& WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') & (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
^----- Double quote missing
But the main problem is that you are using the ampersand concatenation operator (&) to represent a logical and statement:
"*') & (Orders.Item LIKE '*"
^----- This should be AND
Instead, you should use the and operator, e.g.:
& "WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') AND (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
However, in this case, the concatenation of form control values isn't required (and opens the potential for SQL injection) since you can directly reference a form value from the Row Source of the list box.
As such, the code can become:
Me.List41.RowSource = _
"SELECT Orders.Item, Orders.Price, Orders.Department " & _
"FROM People INNER JOIN Orders ON People.SystemLocation = Orders.Department " & _
"WHERE " & _
" People.SystemLocation LIKE '*' & Forms![YourForm]!SearchTxt1 & '*' AND " & _
" Orders.Item LIKE '*' & Forms![YourForm]!SearchTxt2 & '*' " & _
"ORDER by Orders.Department"
Me.List41.Requery
Here, change [YourForm] to the name of your form.
I made a SQL statement in the add/update button in the query wizard I changed it back to SQL view to see how the program made me the code and when I copy and paste the same error on the If statement of the btnAdd it throws me a syntax error, but how?
here is the entire code:
Private Sub cmdAdd_Click()
'In the button add we have two options
'1. Insert
'2. Update
If Me.txtID.Tag & "" = "" Then
CurrentDb.Execute "INSERT INTO tblClients ( ClientID, ClientName, Gender, " & _
"City, [Address (Fisical)], [Cellphone/Telephone] ) " & _
"SELECT " & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & "', '" & Me.cboCity & "','" & Me.txtAddress & "','" & Me.txtCellphone & "'"
Else
'Otherwise the data will be updated
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = [me]. [txtName], tblClients.Gender = [me].[cboGender], tblClients.City = [me].[cboCity], tblClients.[Address (Fisical)] = [me].[txtAddress], tblClients.[Cellphone/Telephone] = [me].[txtCellphone] "
WHERE (([ClientID]=[Me].[txtID].[Tag]));
End If
cmdClear_Click
tblClients_subform.Form.Requery
End Sub
it highlights me this row in red:
WHERE (([ClientID]=[Me].[txtID].[Tag]));
It appears that the following code is not on the same line
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = [me]. [txtName], tblClients.Gender = [me].[cboGender], tblClients.City = [me].[cboCity], tblClients.[Address (Fisical)] = [me].[txtAddress], tblClients.[Cellphone/Telephone] = [me].[txtCellphone] "
WHERE (([ClientID]=[Me].[txtID].[Tag]))
So you may want to change it to
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = [me]. [txtName], tblClients.Gender = [me].[cboGender], tblClients.City = [me].[cboCity], tblClients.[Address (Fisical)] = [me].[txtAddress], tblClients.[Cellphone/Telephone] = [me].[txtCellphone] " & _
"WHERE (([ClientID]=[Me].[txtID].[Tag]))"
In addition to Cableload's correct answer where the WHERE statement that was on a new code line was not connected to the previous line by the use of an underscore at the end of the first one, there is still a referncing issue.
You are referencing values in a UserForm like that were columns in a table so it is not finding the value you are looking for. To get the value into the SQL statement you need to come out of the literal string, reference the value, and then continue writing the string (not forgetting to enclose the value with '): -
CurrentDb.Execute "UPDATE tblClients SET " & _
"[ClientName] = '" & Me.txtName & "', " & _
"[Gender] = '" & Me.cboGender & "', " & _
"[City] = '" & Me.cboCity & "', " & _
"[Address (Fisical)] = '" & Me.txtAddress & "', " & _
"[Cellphone/Telephone] = '" & Me.txtCellphone & "' " & _
"WHERE [ClientID]=" & Me.txtID.Tag
I have spread it across multiple lines for ease of reading but obviously you can adjust your actual code however needed.
I would also question [ClientID]=" & Me.txtID.Tag, is the ClientID in the in the txtID.value or the txtID.Tag, they are different places. The value property is the value in the text box, the Tag property is more like a area for metadata that you can populate if needed but is not automatically populated by default.
Finally I'd like to refer you back to an answer to a previous question you had, at the bottom of the answer there was a tip about placing the resultant query into a Access Query in SQL view to get better information on the error, that would have helped you here too. To give further assistance on the 'resultant query'.
In debug mode before the while the CurrentDb.Execute is highlighted but before it is run (using F8 to step through each line until you get there, or placing a breakpoint on that line
Open the the Immediate Window if it is not already open (either Ctrl+G to from the menu bar 'View' > 'Immediate Window')
Copy all related code from the line after the CurrentDb.Execute statement, in this case it would be UPDATE ... .Tag
In the immediate window type a question mark and then paste in the rleated code and press enter
The immediate window will return the resultant string for you to try in a Query in SQL view.
Change the SELECT keyword to VALUES in your INSERT statement.
CurrentDb.Execute "INSERT INTO tblClients ( ClientID, ClientName, Gender, " & _
"City, [Address (Fisical)], [Cellphone/Telephone] ) " & _
"VALUES (" & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & "', '" & Me.cboCity & "','" & Me.txtAddress & "','" & Me.txtCellphone & "')"
And the UPDATE should be this. The issue here was that you were trying to use Form controls in the SQL, but you needed to evaluate the controls first then concatenate their values to your literal string.
I'm wondering if you really need Me.txtID instead of Me.txtID.Tag
So sway that out if it doesn't work.
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = '" & me.txtName & "', tblClients.Gender = '" & me.cboGender & "', tblClients.City = '" & me.cboCity & "', tblClients.[Address (Fisical)] = '" & me.txtAddress & "', tblClients.[Cellphone/Telephone] = '" & me.txtCellphone & "' WHERE (([ClientID]=" & Me.txtID.Tag & "));"
Good day. I'm a little stumped about what is happening in my code. I have a userform which collects txtQntyRecd and cboSupplySource. I calculate the lookupValue. And it works just fine. It successfully places the txtQntyRecd in the correct tblWarehouseLocations.WQuantity location. The code is:
updateQnty = "UPDATE tblSupplySources INNER JOIN ((tblWarehouseLocations " & _
"INNER JOIN tblSupplySource_WarehouseLocation ON tblWarehouseLocations.WLocation_ID = tblSupplySource_WarehouseLocation.SWLocation_ID)) " & _
"ON tblSupplySources.SupplySourceID = tblSupplySource_WarehouseLocation.Supply_Source_ID " & _
"SET tblWarehouseLocations.WQuantity = '" & Me.txtQntyRecd & "'" & _
"WHERE (((tblSupplySource_WarehouseLocation.Supply_Source_ID)= " & Me.cboSupplySource & ") " & _
" AND ((tblWarehouseLocations.WLocation_ID)=" & lookupValue & "))"
CurrentDb.Execute updateQnty, dbFailOnError
What I want to do is add the next quantity to the same location. I get weird results if I change the SET statement to the following:
SET tblWarehouseLocations.WQuantity = tblWarehouseLocations.WQuantity + '" & Me.txtQntyRecd & "'"
If I put 200 in the first statement, I get 200 in my WQuantity field. When I change to the second statement and I try to add 1 to the 200 I get a result of 211. If I add 1 again, the result is 223. Add 1 again, the result is 236.
Could someone explain what is happening and why the results aren't 201, 202 and 203? In the future I will need to subtract quantities from WQuantity as well.
Thanks
You're adding quotes around an integer and appending it as a string. Change it to:
".....
SET tblWarehouseLocations.WQuantity = tblWarehouseLocations.WQuantity + " & val(Me!txtQntyRecd) & "....
...."
I've changed the . to a ! as I think it's still a nice distinction between objects properties and controls, and used the val function as it converts the string number value to the integer value.
This is your query in full:
' When I use values from controls, I like to store them in vars
Dim quantityReceived As integer
quantityReceived = val(Me!txtQntyRecd)
updateQnty = "UPDATE tblSupplySources INNER JOIN ((tblWarehouseLocations " & _
"INNER JOIN tblSupplySource_WarehouseLocation ON tblWarehouseLocations.WLocation_ID = tblSupplySource_WarehouseLocation.SWLocation_ID)) " & _
"ON tblSupplySources.SupplySourceID = tblSupplySource_WarehouseLocation.Supply_Source_ID " & _
"SET tblWarehouseLocations.WQuantity = tblWarehouseLocations.WQuantity + " & quantityReceived & _
" WHERE (((tblSupplySource_WarehouseLocation.Supply_Source_ID)= " & Me.cboSupplySource & ") " & _
" AND ((tblWarehouseLocations.WLocation_ID)=" & lookupValue & "))"
I solved the problem. I created a SELECT query to get the present amount in WQuantity. Now quantityReceived = Me!txtQntyRecd + the present amount. With SET tblWarehouseLocations.WQuantity = " & quantityReceived it works fine. However, if just seems so cumbersome.
' lookupValue gives the index into the tblWarehouseLocations where WQuantity resides
Dim lookupValue As Integer
lookupValue = DLookup("[WLocation_ID]", "[tblWarehouseLocations]", "[Location_Name] = '" & Me.cboWLocation & "'")
'Define SQL Query
strSQL = "select tblWarehouseLocations.WQuantity FROM tblWarehouseLocations WHERE (((tblWarehouseLocations.WLocation_ID)= " & lookupValue & "))"
Set rs = db.OpenRecordset(strSQL)
If IsNull(rs!WQuantity) Then
dbvalue = 0
Else
dbvalue = rs!WQuantity
End If
Dim quantityReceived As Integer
quantityReceived = Val(Me!txtQntyRecd) + dbvalue
updateQnty = "UPDATE tblSupplySources INNER JOIN ((tblWarehouseLocations " & _
"INNER JOIN tblSupplySource_WarehouseLocation ON tblWarehouseLocations.WLocation_ID = tblSupplySource_WarehouseLocation.SWLocation_ID)) " & _
"ON tblSupplySources.SupplySourceID = tblSupplySource_WarehouseLocation.Supply_Source_ID " & _
"SET tblWarehouseLocations.WQuantity = " & quantityReceived & _
" WHERE (((tblSupplySource_WarehouseLocation.Supply_Source_ID)= " & Me.cboSupplySource & ") " & _
" AND ((tblWarehouseLocations.WLocation_ID)=" & lookupValue & "))"
CurrentDb.Execute updateQnty, dbFailOnError
I have this code to update database with SQL but it is not working
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & WHERE OwnerShipMethod = Sheets("InfoStockDes").Range("b" & g) & WHERE StockName = Sheets("InfoStockDes").Range("c" & g) & WHERE Quantity = Sheets("InfoStockDes").Range("d" & g) "' "
cn.Execute strSQL
Call Close_db
This is your code as posted, but then with some extra linebreaks and outlining. Maybe you can spot the obvious errors yourself better now?
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & _
WHERE OwnerShipMethod = Sheets("InfoStockDes").Range("b" & g) & _
WHERE StockName = Sheets("InfoStockDes").Range("c" & g) & _
WHERE Quantity = Sheets("InfoStockDes").Range("d" & g) "' "
cn.Execute strSQL
Call Close_db
In this version I have changed the extra WHERE clauses to AND, and I haev added some obviously missing " and '.
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & "' " & _
"AND OwnerShipMethod = '" & Sheets("InfoStockDes").Range("b" & g) & "' " & _
"AND StockName = '" & Sheets("InfoStockDes").Range("c" & g) & "' " & _
"AND Quantity = '" & Sheets("InfoStockDes").Range("d" & g) & "' "
cn.Execute strSQL
Call Close_db
I guess this shoudl work better allready, if it doesn't please tell us what is not working, what error messages, if any, you are getting, and please show the actual content of strSQL before yuo execute it. That string should contain a straightforward SQL statement, but there might be errors in it, so have a look at that and, if necessary, post it for us to look at :)
I have large amounts of data that I am trying to sort out based on two cascading combo boxes. I get error Microsoft Access can't find the field '|1' referred to in your expression and it points me to:
ElseIf [Forms]![Send To GE]![cboFil] = "LCP" Then
strSQL = "Select * From [To_GE] Where [Community] = " & Chr(34) & Me.cboSubFil.Value & Chr(34) And [LCP] = "& Chr(34) & Me.cboSSubFil.Value & Chr(34)"
Set rst = db.OpenRecordset(strSQL)
It seems that the And should work for this. What's causing this error, and how can I resolve it?
strSQL = "Select * From [To_GE] Where [Community] = " & Chr(34) & Me.cboSubFil.Value & Chr(34) And [LCP] = "& Chr(34) & Me.cboSSubFil.Value & Chr(34)"
Should maybe be
strSQL = "Select * From [To_GE] Where [Community] = " & Chr(34) & Me.cboSubFil.Value & Chr(34) & " And [LCP] = " & Chr(34) & Me.cboSSubFil.Value & Chr(34)
To make things read a little easier though I would recommend escaping your quotes or switching to single quotes in the query
strSQL = "Select * From [To_GE] Where [Community] = '" & Me.cboSubFil.Value & "' And [LCP] = '" & Me.cboSSubFil.Value & "'"