ms-access


Issue with the TOP 1 query


Is it possible to achieve next thing without using views, but just one single query? I have two tables:
TableA->TanbleB (1-many) ON TableA.Id = TableB.TableAId
I need to update one field in Table A (TableA.Field1) for records in TableA that satisfy condition on one field in tableA (WHERE TableA.Field2=SomeValue)
.
TableA.Field1 will be updated from TableB with value that is last inserted (last inserted value in related records to TableA).
I will put an example:
UPDATE TableA a SET Field1 = (SELECT TOP 1 b.Feild1 * b.Field2 FROM TableB b WHERE b.TableAId = a.id) WHERE field2 = 1
I know Above example doesn't work, but I have many ways tried using INNER JOIN and failed. I had an idea to use something like this:
UPDATE TableA INNDER JOIN ( SELECT ... FROM TABLE B) ON TABLEA.Id= TableB.TableAId SET ....
But the 2ns query should return 1 record for each DISTINCT TableAId, but only the last inserted.
I hope I am making some sense here.
Thanks in advance.
Here is some SQL that will do what you want
UPDATE T1 INNER JOIN T2 ON T1.ID = T2.T1ID SET T1.F2 = [T2].[F2]*[T2].[F3] WHERE (((T1.F1)="ABC") AND ((T2.ID)=DMax("[ID]","[T2]","[T1ID]=" & [T1].[ID])));
This predicated on T1.ID being the primary key for T1 and T2.T1ID being a index field in T2
One of the flaws in Access is that you can't run an "UPDATE" query based on a "SELECT" query, it will usually give the error:
Operation must use an updateable query
The only way around is as you say to create a view of the "SELECT" query and then inner join this on your table, Access is then working with a static recordset and can handle the "UPDATE" query ok
Alternatively you could write a VBA procedure to step through line by line with the Recordset.
Best of luck : )
UPDATE:
SELECT b.TableAId, b.Feild1 * b.Field2 INTO tblView FROM TableB As b WHERE b.field2 = 1

Related Links

microsoft access control source from a table in another group in a report
How to move Root nodes up and down in a treeview Access/VBA
how to speed up with csv and access
Continuous form selection deselected on command button click
Microsoft Access remove part of string
How to get the GUID of a table in microsoft Access
is there TRUNCATE in Access?
A table value places a checkmark in a checkbox on a form
Can you change the default color for selected text in MS Access?
MsAccess Free text in dropdown
msaccess workgroup information file
Composite Key in MS Access 2003
Access 2007 response time
MS Access VBA DoCmd.ApplyFilter IsNull() evaluating false
Access 2010 Query pulling data that matches a label value
How to find broken queries in Access?

Categories

HOME
ms-access
pandas
pdf
homebrew
react-virtualized
alfresco
bookshelf.js
filtering
retrofit
serverspec
fancybox
esper
gorm
ssl-client-authentication
correlation
timeout
event-handling
visual-studio-cordova
dynamics-crm-online
clojurescript
ghc
finite-automata
beyondcompare
reverse-proxy
crosstab
qhull
url-scheme
fluentvalidation
facebook-apps
c++-amp
procdump
javascriptcore
http-status-code-503
titanium-mobile
bytecode-manipulation
event-driven
trim
preconditions
no-www
segment
scrollable
osx-mavericks
hendrix
worksheet
dandelion
darcs
feeds
namecoin
dwscript
bonobo
underscore.js-templating
pcf
skobbler-maps
xml-attribute
mongocsharpdriver
holder.js
pagedlist
sigabrt
packagist
fadeout
dataview
nessus
tform
independentsoft
xpath-1.0
kendonumerictextbox
msys
lib.web.mvc
lemon
sankey-diagram
operation
processmodel
unity3d-gui
device-orientation
website-monitoring
gadt
qcodo
wordpress-theme-customize
openexr
htmlcleaner
dealloc
didselectrowatindexpath
centos5
gridfs
soundtouch
eventual-consistency
comaddin
typoscript2
hamiltonian-cycle
propertyeditor
jspinclude
hosts-file
work-stealing
netdna-api
nsdatecomponents
pydot
mod-auth
subgurim-maps
coredump
yui-datatable
zend-translate
dmx512
nsviewanimation
nintendo-ds
exchange-server-2003
virtual-functions
grid-system
multiple-languages

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App