"Operation must use an updatable query" error in MS Access - ms-access

I am stuck with this update query:
UPDATE [table1] n SET n.kdo = IIf( n.old_kdo IN (SELECT u.id FROM [table2] u WHERE u.id_c<>0),"1","0")
it gives me "Operation must use an updatable query" error (I have all read/write permission).
I have tried bypass it by using:
UPDATE [table1] n SET n.kdo = IIf( n.old_kdo IN (DLookup("id", "table2", "id_c<>0")),"1","0")
it works, but sadly it return first match only.
Anyone have any idea how to make it right? Would be some join query better solution?

Try replacing your subquery by a dFirst(), dCount() or similar function, and extend your criteria accordingly:
UPDATE [table1] n SET n.kdo =
iif(dCount("*", "table2", "id_c<>0 and id = " & n.old_kdo) > 0 ,"1","0")
Access is tricky for joins in Update queries.

Related

MS Access VBA improve script

I have this code in MS Access (I did it in VBA becasue I didn't find the way to create a procedure in Access). What it does is simple, it takes a field form a table and replace the value in another table where the ID is the same.
How can I get this faster? It is currently taking 8 minutes more or less to get it done (processor is always under 20%, so it is not about processor power).
Set rst = CurrentDb.OpenRecordset("Tab_personal_2")
rst.MoveFirst
DoCmd.SetWarnings False
Do Until rst.EOF
DoCmd.RunSQL ("UPDATE Tab_personal_3 SET RFC = '" & rst("RFC") & "' WHERE Id = " & rst("Id"))
rst.MoveNext
Loop
You should be able to do this in a single query:
UPDATE Tab_personal_3 t3
INNER JOIN Tab_personal_2 t2 ON t2.ID = t3.ID
SET t3.RFC = t2.RFC
Of course, you can execute this query through VBA, if you wish. But storing the query allows Access to store the execution plan, and increase the speed on the query.
I believe a single update query will work if the tables share an ID field:
UPDATE Tab_personal_3 INNER JOIN Tab_personal_2 ON Tab_personal_3.ageing_collection = Tab_personal_2.Id SET Tab_personal_3.RFC = Tab_personal_2.RFC;

Query that populates a datasheet in MS Access, and MsgBox if no values returned

I'm trying to create a query that can run from a button in MS Access. The SQL query I've created is below:
PARAMETERS CASNUMBER Text ( 255 );
SELECT DISTINCT Chemical.Chemical_Name, Hazard.Hazard_Code, Hazard.Hazard_Text
FROM Chemical, Chemical_Hazard, Hazard
WHERE Chemical.Chemical_Id = Chemical_Hazard.Chemical_Id
and Chemical_Hazard.Hazard_Id = Hazard.Hazard_Id
and [CASNUMBER] = Chemical.CAS
;
How do I make it so that a MsgBox appears when there are no values returned?
I'd recommend join tables using JOIN instead of WHERE. Also you can avoid using parameter, which in most cases requres VBA code for query execution. Just create query without parameter, add CAS to columns list:
SELECT DISTINCT Chemical.Chemical_Name
,Hazard.Hazard_Code
,Hazard.Hazard_Text
,Chemical.CAS
FROM (
Chemical_Hazard INNER JOIN Chemical ON Chemical.Chemical_Id = Chemical_Hazard.Chemical_Id
)
INNER JOIN Hazard ON Chemical_Hazard.Hazard_Id = Hazard.Hazard_Id
And then check for CAS in code:
If DCount("*", "MyQuery", "CAS=" & lngCASNumber) = 0 then
MsgBox "CAS not found"
End If

Convert query to hibernate criteria query

I have the following Sybase query,
select *
from dbo.translation_style_sheet t1
where t1.create_date = (select max(t2.create_date)
from dbo.translation_style_sheet t2
where t1.file_name = t2.file_name);
I'm trying to convert it to a hibernate criteria query, but haven't been able to figure it out. I'm assuming I need to use a DetachedCriteria to handle this, but not sure how to work with it.
This is what I have thus far.
DetachedCriteria maxCreateDate = DetachedCriteria.forClass(TranslationStyleSheet.class, "translationStyleSheet2")
.setProjection( Property.forName("createDate").max() )
.add( Property.forName("translationStyleSheet2.fileName").eqProperty("translationStyleSheet.fileName") );
List<TranslationStyleSheet> translationStyleSheets = this.session.createCriteria(TranslationStyleSheet.class, "translationStyleSheet")
.add( Property.forName("createDate").eq(maxCreateDate))
.list();
I'm getting the following exception.
org.hibernate.exception.GenericJDBCException
could not execute query
SQL
select this_.translation_style_sheet_id as translat1_20_0_, this_.create_date as create2_20_0_, this_.description as descript3_20_0_, this_.file_content as file4_20_0_, this_.file_extension as file5_20_0_, this_.file_name as file6_20_0_, this_.file_size as file7_20_0_, this_.style_sheet_content as style8_20_0_, this_.style_sheet_type as style9_20_0_ from translation_style_sheet this_ where this_.create_date = (select max(translationStyleSheet2_.create_date) as y0_ from translation_style_sheet translationStyleSheet2_ where translationStyleSheet2_.file_name=this_.file_name)
SQLState
ZZZZZ
Does anybody know what I'm doing wrong?
UPDATE
The error seems to be be happening at the max(translationStyleSheet2_.create_date) as y0_
as. When I remove the as y0_ in the sql statement, I'm able to run the query the query, however I'm not sure how to repair this in hibernate criteria though.
So I had no success getting this to work as a criteria query, but I did have success getting it to work as an HQL query.
HQL solution
this.session.createQuery("from TranslationStyleSheet this "
+ "where this.createDate = (select max(translationStyleSheet2.createDate) "
+ "from TranslationStyleSheet translationStyleSheet2 "
+ "where translationStyleSheet2.fileName=this.fileName)")
.list();
I'm still interested in getting the query to work as a criteria query though.

Update a single table based on multiple tables - appears to works MySql not ACCESS 2003

Mysql Version Works
UPDATE results SET rCARRIER = (
SELECT cellCarrierName
FROM tblImportedTempTable, user, cellCarrier
WHERE
userEmployeeNumber = tblImportedTempTable.EMPLOYEENUMBER
AND userId = results.rUserId
AND results.rPHONENUMBER = tblImportedTempTable.PHONENUMBER
AND CARRIER = cellCarrierId )
I have written this sql that works fine in MySql(above) and fails in access 2003(below) any suggestions? Is one or both of the 2 nonstandard sql? Does Access hav an admin problem?
Sorry the field and table names are diferent this is the ACCESS version.
Access version
UPDATE tblWorkerPhoneNumber SET tblWorkerPhoneNumber.PhoneCarrier = (
SELECT PhoneCarrierType.CarrierName
FROM tblImportedPhoneCarrier, tblWorkerMaster, PhoneCarrierType
WHERE
tblWorkerMaster.EmployeeNumber = tblImportedPhoneCarrier.Emp
AND tblWorkerMaster.WorkerID = tblWorkerPhoneNumber.WorkerID
AND tblWorkerPhoneNumber.PhoneNumber = tblImportedPhoneCarrier.Cell
AND tblImportedPhoneCarrier.CarrierCode = PhoneCarrierType.CarrierID )
Error Message
Operation must use and updateable query
Thanks
In MS Access, something like this:
UPDATE tblWorkerPhoneNumber
INNER JOIN tblWorkerMaster ON tblWorkerMaster.WorkerID = tblWorkerPhoneNumber.WorkerID
INNER JOIN tblImportedPhoneCarrier ON tblWorkerPhoneNumber.PhoneNumber = tblImportedPhoneCarrier.Cell
INNER JOIN PhoneCarrierType ON tblImportedPhoneCarrier.CarrierCode = PhoneCarrierType.CarrierID
SET tblWorkerPhoneNumber.PhoneCarrier = PhoneCarrierType.CarrierName
WHERE tblWorkerMaster.EmployeeNumber = tblImportedPhoneCarrier.Emp
(Might need to change the join conditions; I'm not familiar with your schema)

hibernate, mysql

i have the following hql query:
UPDATE TaskAssessment taskAssessment
SET taskAssessment.activeFlag = false
WHERE taskAssessment IN
(
SELECT taskAssessment2
FROM TaskAssessment taskAssessment2
Where taskAssessment2.activeFlag = true
AND taskAssessment2.patient.id
AND taskAssessment2.needsLevel.careNeed = :careNeed
)
but its giving me errors:
You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause
could anyone help me to correct the query for mysql and hibernate. thanks in advance.
To resolve You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause, rewrite the query to use JOIN instead of IN (in mysql you need to write something like this):
UPDATE TaskAssessment a
INNER JOIN TaskAssessment a2 ON (a2.id = a.id)
SET a.activeFlag = 0
WHERE a2.active_flag = 1 AND
a2.patient_id = :patient_id AND a2.needsLevel_careNeed = :careNeed