Updating a table in MS_Access using SQL - sql

I have a few databases and I need to insert a string value in front of some of the values. I have the following code:
UPDATE DB_AlarmTest SET DB_AlarmTest.Address = "DB40," & [Address]
WHERE DB_AlarmTest.Address
NOT LIKE '%DB40%';
I dont want my adresses to come out like this: "DB40,DB40,DB40,2.0" If i execute the query more than once, so I added the " NOT LIKE '%DB40%' " part
Can someone tell my why this is not working?
Thanks in advance!

With the usual ANSI options in MS Access, the wildcard is *, not %, so:
UPDATE DB_AlarmTest
SET DB_AlarmTest.Address = "DB40," & [Address]
WHERE DB_AlarmTest.Address Not Like "*DB40*"

Related

ACCESS update with subquery

I have a table like this:
Now I'm trying to write in the column "SUMAMOUNT" of the table the sum of amount per "CODE" and "IBAN" but i can't reach this.
I'd want something like this:
I'm using this query but it doesn't work:
update tabella
set sumamount = (select sum(t2.amount)
from tabella as t2
where t2.code = tabella.code and t2.iban = tabella.iban
);
The precedent query gives me this result:
Can you help me? I'm using MS ACCESS.
Thank you in advance!
EDIT: Screenshot of the error:
I can't even try to run it because he ask me to save it. When I try to save, access gives me this error.
Consider domain aggregate, DSum, which allows an updateable query. Below assumes code and iban are text types and therefore requires single quote enclosures.
UPDATE tabella t
SET t.sumamount = DSUM("amount",
"tabella",
"code = '" & t.code & "' AND iban = '" & t.iban & '");
(By the way, best practice in databases is to avoid saving calculations in tables. Save resources and simply run queries on data as needed.)

Access VBA update a column to concatenate

I was trying to update a column from a table in my Access database with following VBA code but it did not work as nothing happpened to that column. I would like to concatenate 5 columns into one. This update statement would work in SQL server but obviously not here.
currentdb.Execute "update sys_AAAA_AAAA set ABI=AAT+AAU+AAV+AAW+AAX"
Any advice? Thanks.
Perhaps some fields are Null?
Try using the string "concatenator" preferred by Access, ampersand:
CurrentDb.Execute "update sys_AAAA_AAAA set ABI = AAT & AAU & AAV & AAW & AAX"

Convert SQL Server Query to MS Access to Update Table

Could anyone help with this. I don't use Access often, but this process I'm building needs to utilize Access for the business to use. I have the following code, which will not work in Access. I keep getting the error 'Syntax Error (missing operator) in query expression'. Would anyone be able to convert this query into something Access will accept.
UPDATE Auto_DailyDiary_PrimaryTbl a
SET a.TotalDiary = sub.TotalDiary
FROM
(
SELECT CaseEEID, Count(CaseID) as TotalDiary
FROM dbo_Case
WHERE CaseStatus <> 6
GROUP BY CaseEEID
) sub
WHERE sub.EEID = a.EEID AND a.DiaryDt = Date()
Pretty sure Access doesn't like having a joined group by subquery in an update query.
Maybe try something like this?
UPDATE Auto_DailyDiary_PrimaryTbl
SET TotalDiary =
DCOUNT('CaseID', 'dbo_Case',
"CaseStatus <> 6 AND " &
"CaseEEID = '" & Auto_DailyDiary_PrimaryTbl.EEID & "'")
WHERE DiaryDt = Date()

How do I run an SQL update query using a like statement

I am trying to update a field in a table using an SQL update query where there is a like statement referencing a value in another table. They syntax unfortunately is not working. Below is my code. In short, I am trying to put a '1' in the field 'Query07ParolaChiave' in the table 'tblSearchEngine01' when the value located in table 'tblsearchengine07' is present in the field 'tblMasterListOfEventsNotes' located in the table 'tblSearchEngine01'. I think my code is almost complete but there is a syntax issue which i cant find.
st_sql = "UPDATE tblSearchEngine01, tblSearchEngine07 SET tblSearchEngine01.Query07ParolaChiaveSelect = '1' WHERE ((([tblSearchEngine01].[tblMasterListOfEventsNotes]) Like " * " & [tblsearchengine07].[ParolaChiave] & " * "))"
Application.DoCmd.RunSQL (st_sql)
I suggest you 2 solutions :
This one is using EXISTS functions, and will check for each row in tblSearchEngine01 if there is a matching value in tblsearchengine07
UPDATE
tblSearchEngine01
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
WHERE
EXISTS (SELECT 1
FROM tblsearchengine07
WHERE [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*')
This one is more performant because it uses JOIN
UPDATE
tblSearchEngine01
INNER JOIN tblsearchengine07
ON [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*'
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
I read something like in ADO/VBA, you have to use % instead of * as the wildcard.
You can have more information on wildcard and LIKE comparator here
UPDATE
Why the '1' after select in your first solution?
EXISTS (SELECT 1 ... is better for performance because it return only the number 1 instead of fields, anyway EXISTS just stop the excecution after 1 element found.
'Performant' means more consuming in regards to space and memory?
JOIN is more performant in term of time of execution, RDBMS are far better at joining tables than using subquery, in some rare case, it's more interesting to use the 1st solution.
Also, any initial thoughts as to why my original solution (coming straight from an Access Query which works) does not function?
I cannot really know but perhaps it's because of " * ", because you are saying SPACE + * + SPACE + VALUE + SPACE + * + SPACE. For ex : 'John' LIKE ' John '
May be with "*" instead of " * " could solve it...
I have no other track, I'm not Access sql developper, I usually play around Sql server/Oracle/mySql, hope it helped. ;)
Try to change your like this way:
... Like '*" & tblsearchengine07.parolachiave & "*'))"
The like statement go into the WHERE clause.
If you do want to use LIKE without you care about caps letters, then you can use it like this:
LIKE COLUMN_NAME = '%WhatYouLike%'
My suggestion is:
Use a table variable (#Table) with a unique/primary key coming from the table to be updated.
SELECT all the data to be updated (you can add the like statement here) and then INSERT that in the created table variable.
Construct the UPDATE statement with an INNER JOIN to the table variable matching with the unique/primary key.
I know this may take a lot of steps but believe me these are more efficient than using a black list approach.

SQL Like statement not working in Visual Basic

Dim strText As String = tbRefine.Text
Dim sql As String = "SELECT user_name,forename,surname,game_cash,reg_group FROM tblGame WHERE user_name LIKE '" + strSearchText + "' & '*'"
Dim dsRefine As New DataSet
GetDataset(sql, "tblGame", dsRefine)
MsgBox(dsRefine.Tables("tblGame").Rows(0).Item(2).ToString)
This is not working, it crashes and says there is nothing in the dataset.
I know the dataset function works as its worked successfully before.
When i print out the sql statement into microsoft access it works fine. What am i doing wrong
Try this:
"SELECT user_name,forename,surname,game_cash,reg_group
FROM tblGame
WHERE user_name LIKE '%" + strSearchText + "%'"
Try to use the RTRIM() function in your line:
Dim sql As String = "SELECT user_name,forename,surname,game_cash,reg_group
FROM tblGame
WHERE RTRIM(user_name) LIKE '" + strSearchText + "' & '*'"
What about leading or trailing % symbols in your like?
At the moment you will end up with a where clause like:
LIKE 'searchtext''*'
which looks a bit odd (I assume SQL server?).
It's wiser to use SQL parameters as your method is open to SQL injection. The link below will help with how to format the SQL statement. I would also suggest doing it via a store procedure, but hats optional...
http://forums.asp.net/t/1256985.aspx
I think there's one more thing to be mentioned:
the "*" wildcard character works for the "Like" operator in VB/VBA/MS-Access, but not in T-SQL.
The correct wildcard character for the "Like" operator in T-SQL is "%".
That's why this T-SQL statement:
Select... WHERE ... LIKE 'sText*'
returned no data without any syntax error in MS-SQL(using T-SQL), but works in MS-Access.