Avatar billede lenton Nybegynder
17. marts 2004 - 19:02 Der er 18 kommentarer og
1 løsning

Insert max+1 (med et twist)

Jeg har en tabel ved navn Kunde:
ID
InstitutionID
Aar
Reference

Jeg vil så lave et query som kan oprette en ny Kunde hvor Institution og Aar er bestemt af mig via en form og da ID styres af autummerering skal Reference bestemmes ud fra Max(Reference) - men hvor det kun er max af de rækker med samme InstitutionID og Aar.
Altså vil hver Institution for hvert Aar have ca. 100 Referencenumre - således at at der vil være mange med Reference nr. 1, men kun én per Insitution pr. år.

Ej ok, kort fortalt her er hvad jeg vil bede Access om, som et eksempel:
Find det højeste Reference nr. hvor Aar = 2004 og Institution = 1.
Dette ville jeg jo fint kunne klare alene, men når det er et insert som skal være max+1 hvordan skriver jeg så det hele i ét query - hvordan hiver jeg evt. resultatet frem fra et tidligere query hvis jeg valgte at dele det op i to queries:
1) select max som beskrevet ovf.
2) Insert max + 1.
Avatar billede terry Ekspert
17. marts 2004 - 19:22 #1
I will try and do it for you if you can send me your dB! NOSPAMeksperten@santhell.dkNOSPAM

remove NOSPAM
Avatar billede lenton Nybegynder
17. marts 2004 - 19:28 #2
But I'm interested in an explanation, not for someone to do it for me. Why can't you explain how?
Avatar billede terry Ekspert
17. marts 2004 - 19:50 #3
because I can end up using ages trying to explain, and I have no idea as to your previous knowledge of Access. But here goes!

You need to make an APPEND (INSERT) query.
Start by making a new query. When you are asked to choose a table press cancel.
Now in the menu choose Query+Append Query. Now choose the table yo want to append to.
Now in the "field" field for the frist column right click and choose build. Now you need to use th eexpression builder to find the form/field where you are entering Aar and Institution. Choose the field you want to take the value from. Now in the "Append To" field of the first column choose the field in the table you are appending to. Repeat this for the other field.

Now the last field (Reference)
You need to make a sub select to select this value and again this needs to reference the fields on your form. This is only an example as I dont have your data.

In the "field" field of the last colum.

Ref:(SELECT Max(reference) FROM Kunde WHERE Aar = [Forms!NameOfForm].[NameOfField] AND Institution = [Forms!NameOfForm].[NameOfField] )+1

And finally you need to set the "Append To" field!

Thats it!
Avatar billede terry Ekspert
17. marts 2004 - 20:09 #4
just puzzled, but why the name lenton? I know (knew) someone by the name of Lenton?
Avatar billede lenton Nybegynder
17. marts 2004 - 20:19 #5
OK,thanks for the answer, but I'm afraid it's reporting the same error my original code with a subselect did: "Reserved error (-3025); there is no message for this error." Great error message, eh? Do you have any idea what this means. I know it's not a data type problem. This whole query works just fine if I manually write the max+1 into the query. So all I was thinking was that sub selects aren't supported like that, but that can't be it either, it seems :(

My real name is Lennart, so for some reason some ppl just call me Lenton and I kinda like it ;)
Avatar billede terry Ekspert
17. marts 2004 - 20:26 #6
Ah! Just thinking about it, Access is VERY BAD at sub selects in UPDATE and INSERT queries! Try changing your sub select to a DMax() function. You can use the expression builder to help you with that.
Avatar billede jensen363 Forsker
17. marts 2004 - 20:28 #7
Jeg tror/ved du vil få problemer med autonummereringsfunktionen, så jeg ville nok arbejde på en ekstra tabel til styring af denne individuelle nummerering pr. Institution, hvorfra du så henter seneste værdi, tillægger 1 og gemmer denne i begge tabeller ... bare et forslag ... ;)
Avatar billede terry Ekspert
17. marts 2004 - 20:39 #8
jensen363>why do you think that? Obviously if we are in a busy multiuser environment then this could give problems, but then using an extra table would not help as far as I can see!
Avatar billede jensen363 Forsker
17. marts 2004 - 20:56 #9
Terry ... bare et forslag, på en allerede udtænkt og gennemprøvet metode ...
... almindelig inspiration ...
Avatar billede terry Ekspert
17. marts 2004 - 21:07 #10
You lost me there!

Lennart
Dmax("Field", "Table", "Criteria")

Criteria is the same as a WHERE but whith WHERE

So it woould be something like that in your sub select
Avatar billede lenton Nybegynder
17. marts 2004 - 21:07 #11
Hah, I replied in the wrong thread, well I'm back:
Ok, how exactly is the syntax for DMax?
This is what I tried: DMax("Reference";"Kunde";"Institution=1")

I haven't got a clue about the "Criteria" syntax.
--
Btw I don't see any problems concerning autonumbering
Avatar billede terry Ekspert
17. marts 2004 - 21:10 #12
does the last comment I gave help with Dmax?
Avatar billede lenton Nybegynder
17. marts 2004 - 21:18 #13
Yes it did! It now works when there is at least one "Kunde" with the selected "Institution" and "Aar". So Creating the first "Kunde" in a new year for an "Institution" will not work with this query - how would I go about this?
Obviously it's because DMax is reporting NULL instead of 0...
Avatar billede terry Ekspert
17. marts 2004 - 21:37 #14
Nz(dmax(...), 0)
Avatar billede terry Ekspert
17. marts 2004 - 21:39 #15
Lennart, remember my reason for asking you to send the dB, we've been at it for over an hour now :o)

but obvioulsy you learn more doing it yourself!
Avatar billede lenton Nybegynder
17. marts 2004 - 21:48 #16
I know :)
Well, latest news is that Nz(dmax(), 0) is not working, it's reporting a syntax error. When I replace the comma with a semicolon it does not complain, but it still doesn't work. Same behaviour as before.

Access is driving me mad!
Avatar billede lenton Nybegynder
17. marts 2004 - 21:54 #17
Hah ok, I finally made a mistake! ;)

It was because there was no corresponding Institution created yet, oops.
Thanks for everything, terry!
Avatar billede terry Ekspert
17. marts 2004 - 21:55 #18
:o)

Make a SELECT using the same dmax and see what it is returning!

Well I'm off to relax a bit before going to my bed. If you still cant get it working then I think there is only one solution :o) but I will first have time when I get home form work tomorrow.

tak for i aften!
Avatar billede terry Ekspert
17. marts 2004 - 21:55 #19
just saw your last comment, GREAT!

Night :o)
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