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

JETCOMP.EXE arguments
Running MS Access Programs as exe
how to create a menu bar in Access 2007 Form using VBA
Access not allowing users to delete rows
Printing pictures in Access Forms
printing a record
MS access mdb file has a “repair” status first time it is opened from Access 2007
Viewing ACCDB with Access 2010 runtime when Access 2007 is installed
is it possible to password protect ms-access 2007 file without encryption
Will an MS Access back-end for a client server application have transfer speed problems?
MS-Access, Bound Control on Form, Show user text, but record PK for that text
Access - Design change error message without attempting to make a change
VBA question: debug.print displays the correct data - how to get it out of Immediate window?
create a customised openFileDialog
access 2010 One form add information in table1 and table2
Trying to improve performance by removing DLookUp - Access 2007

Categories

HOME
xamarin
image-processing
activiti
gremlin
kde
analysis
opengl-es-2.0
convolution
cross-validation
izpack
dtrace
event-handling
caml
highlight.js
transformation
predix
conemu
object-detection
tar
sylius
core-text
p-value
facet
bootstrap-duallistbox
bootstrap-material-design
windows-error-reporting
elasticsearch-ruby
windowbuilder
galsim
twilio-api
create-table
fog
perlin-noise
google-api-nodejs-client
suricata
retina-display
android-fingerprint-api
bootstrapper
preconditions
password-encryption
scorm
pubmed
theming
drupal-6
boost-preprocessor
iso8601
blacklist
smart-table
pycaffe
root-framework
android-fonts
django-debug-toolbar
merge-conflict-resolution
yaws
pagerank
vhd
sailfish-os
proj4js
bridge.net
csquery
android-nested-fragment
file-copying
asp.net-web-api-odata
tld
navigationservice
rtmfp
fmod
page-layout
back-stack
chuck
armcc
javaspaces
labwindows
errai
ticoredatasync
online-compilation
cufon
nsdatecomponents
php-parser
mercurial-server
external-accessory
psi
javax.script
netbeans-7.1
jmock
zend-translate
thunderbird-lightning

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