Avatar billede petersen7913 Forsker
14. januar 2008 - 11:10 Der er 22 kommentarer og
1 løsning

Hvordan kan jeg lave en requery, så data også opdaters i formular

Jeg har en master tabel og en detail tabel. Master tabellen har et felt, der består af sum(felt-fra-detail).

Jeg vil gerne vise den nye sum, når der er sket ændringer i detail formularen. Jeg har forsøgt at lave et felt (dummy) i detali formularfoden, som kunne holde summen. Men jeg får en fejl, fordi dummy'en kun vil accceptere me-rækken i detail.

Kan jeg lave en form for 'update master felt select sum()...)' eller hvad kan jeg gøre?

Det er et ret generelt problem for mig.
Avatar billede terry Ekspert
14. januar 2008 - 11:21 #1
me.requery
Avatar billede terry Ekspert
14. januar 2008 - 11:28 #2
In the sub forms after updated event

Docmd.RunSQL "UPDATE [Master] set SomeField = Dsum("DetailField", "DetailTable")"

Or something like that
Avatar billede terry Ekspert
14. januar 2008 - 11:30 #3
And you may need to include the "WHERE" parameter in the dsum if the detail records are related (which they very likeley will be) to the main form record
Avatar billede terry Ekspert
14. januar 2008 - 13:43 #4
Concerning the comment you placed in you rother question ..

OrderID, sum(price pr. orderline), .....

When you have calculated fields in a query it will make the data read only.

You can get around this by having a field in the main table like you suggest here, or you could try having an unbound field on the main form an deither set this in code or maybe using th eexpression builder (right click in the Control source property and then choose build)
Avatar billede petersen7913 Forsker
14. januar 2008 - 13:58 #5
So if I have an unbound field in the main form how do I set the value in code? I have tried the expression builder but it doens't work. I have tried something like

= sum(detail-form.field)

but I just get an error.
Avatar billede terry Ekspert
14. januar 2008 - 14:31 #6
It simportant that the syntax is correct.

I use this and it works
=DSum("[Antal]";"[OrdreLinier]";"[OrdreID] = " & [OrdreID])
Avatar billede petersen7913 Forsker
14. januar 2008 - 14:33 #7
Back to the Docmd.RunSQL "UPDATE [Master] set SomeField = Dsum("DetailField", "DetailTable")"

I'm getting closer, but have problems with the where ....

I guess it should be something like "[detailfield] = ????" but should it be the masterforms field? I am in the subform on an after_update.
Avatar billede terry Ekspert
14. januar 2008 - 14:51 #8
Your updateing the master table so you have to use a field in the master table. The master and detail tables are related so it will be (normally) the primary key field in the main table.



..;"OrderID = " & [OrderID])

THis bit is the field from the detail form which I also assume has the saem name as the primary key in the main order table.
& [OrderID])
Avatar billede petersen7913 Forsker
14. januar 2008 - 15:37 #9
Oh yes syntax is important :-)

So now I have a string like this:

Txt = "UPDATE [Order] set OrderSum = DSum("[Antal]";"[OrdreLinier]";"[OrdreID] = " & [OrdreID])

but there's a problem whit the ".
Can you help me (again)?
Avatar billede terry Ekspert
14. januar 2008 - 18:27 #10
If you can send the dB to me its maybe easier for me to see the problem.

ekspertenATsanthell.dk

AT = @
Avatar billede terry Ekspert
14. januar 2008 - 18:28 #11
You could also try

Txt = "UPDATE [Order] set OrderSum = " & DSum("[Antal]";"[OrdreLinier]";"[OrdreID] = " & [OrdreID])
Avatar billede terry Ekspert
14. januar 2008 - 18:33 #12
and you should still look at the open questions you have, otherwise some users wont be prepared tohelp you in future.
Avatar billede terry Ekspert
14. januar 2008 - 19:24 #13
have you got it working?
Avatar billede petersen7913 Forsker
14. januar 2008 - 19:34 #14
I have tried to close the questions, but some don't have this 'accept' so how do I close?

I think the problem is in the string itself. When I type it exactly as

Txt = "UPDATE [Order] set OrderSum = DSum("[Antal]";"[OrdreLinier]";"[OrdreID] = " & [OrdreID])

it is not accepted as a string. It is wrong use of "? perhaps ' instead?

So no, it's not working :(
Avatar billede terry Ekspert
14. januar 2008 - 19:43 #15
You only need to close when your finished with the question, either when youhave received an anwer whichworks, or you find a solution yourself, or maybe when no answers have been helpeful. In this case you can just place an answer yourself and accpet it, then you get your points back.

Also if users have only placed a comment then you cant accept it, you need to get an anwer first. If your happy with any of the comments then you can get the user to place an answer, or again you can place on yourself to accept.

Can you send me your dB?

14/01-2008 18:27:19
Avatar billede terry Ekspert
15. januar 2008 - 17:59 #16
Thanks, hope you could use the answer.
Avatar billede petersen7913 Forsker
15. januar 2008 - 18:27 #17
Yes, part of the problem is solved and then ... a new one ;-)

So now I can update, but it seems the update is only in the database, not on screen. Can I make a requery/refersh??? on the master formula from the after-update in the subformula?
Avatar billede terry Ekspert
15. januar 2008 - 18:31 #18
You can try something like this (not tested) just after the SQL update

Me.parent.requery
Avatar billede petersen7913 Forsker
15. januar 2008 - 21:09 #19
I have tried and it doesn't work :( I have a query with the orderID and sum(orderlines) so I can check what happens.

I run the DoCmd.RunSQL "update ... DSUM(...)" but it doesn't save the update. (In Oracle I would use either af 'post' or a 'commit')

The new value in the subformula IS updated OK.

I have used this code in another database. There the fun is, that all! resords are updated, as if the where-clause was empty. So lots of fun ;-)
Avatar billede terry Ekspert
15. januar 2008 - 21:21 #20
You said 15/01-2008 18:27:17
"So now I can update, but it seems the update is only in the database, not on screen."

Is the dB updated?
Do you get an error or doesnt it update the form?
Avatar billede petersen7913 Forsker
15. januar 2008 - 21:55 #21
I don't get an error, and the field is not updated. The field in the subformula is updated.

Ie. OrderLines.Price is updated but Orders.TotalPrice is not.
Avatar billede terry Ekspert
16. januar 2008 - 07:59 #22
"..and the field is not updated."

Thge field on the form or in the dB?
Avatar billede terry Ekspert
16. januar 2008 - 08:21 #23
Can you send me your dB?

14/01-2008 18:27:19
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester