Avatar billede flemming39 Nybegynder
13. marts 2006 - 17:03 Der er 8 kommentarer og
2 løsninger

Opret ny post

Hej

Jeg har et mystisk problem.
Når jeg afspiller funktion (se længere nede) fra en knap (on klik hændelse) virker det fint men når jeg afspiller samme funktion fra en makro kan jeg i min debug se at det "oprindelige kontaktpersonid ikke fornyes ved acnew og at jeg derfor får duplicate fejl ved inserten.
Er der nogen der kan hjælpe mig med en forklaring?

mvh Flemming

Denne kode er pakket ind i en funktion:

    DoCmd.GoToRecord , , acNewRec
    Forms!kontaktpersoner!Firmanavn = "  "
    Forms!kontaktpersoner!Firmanavn = Null
 
   
    Set con = CurrentDb
    Set rs = con.OpenRecordset("kontaktperson_x_mappe")
        rs.Edit
        rs.AddNew
        rs.Fields![mappenavn] = "Markedsføring"
        rs.Fields![guid_kontaktpersonid] = Forms!kontaktpersoner!KontaktpersonId
        rs.Update
        rs.AddNew
        rs.Fields![mappenavn] = "Aktiver"
        rs.Fields![guid_kontaktpersonid] = Forms!kontaktpersoner!KontaktpersonId
        rs.Update
        rs.AddNew
        rs.Fields![mappenavn] = "Generel korrespondance"
        rs.Fields![guid_kontaktpersonid] = Forms!kontaktpersoner!KontaktpersonId
        rs.Update
        rs.AddNew
        rs.Fields![mappenavn] = "Information"
        rs.Fields![guid_kontaktpersonid] = Forms!kontaktpersoner!KontaktpersonId
        rs.Update
        rs.AddNew
        rs.Fields![mappenavn] = "Prissætning"
        rs.Fields![guid_kontaktpersonid] = Forms!kontaktpersoner!KontaktpersonId
        rs.Update
        rs.AddNew
        rs.Fields![mappenavn] = "Prospekt"
        rs.Fields![guid_kontaktpersonid] = Forms!kontaktpersoner!KontaktpersonId
        rs.Update
        rs.AddNew
        rs.Fields![mappenavn] = "Regnskaber"
        rs.Fields![guid_kontaktpersonid] = Forms!kontaktpersoner!KontaktpersonId
        rs.Update
        rs.AddNew
        rs.Fields![mappenavn] = "Udsendte prospekter"
        rs.Fields![guid_kontaktpersonid] = Forms!kontaktpersoner!KontaktpersonId
        rs.Update
       
        rs.Close
        Set rs = Nothing
Avatar billede fdata Forsker
13. marts 2006 - 17:35 #1
Det lugter af at du kører koden i samme øjeblik, du hopper til den nye post. I så tilfælde er posten stadig i redigeringstilstand og ID'et er derfor ukendt. Posten må gemmes først før koden kan bruge ID'et.
Tilføj en
  Forms!kontaktpersoner.Refresh
efter
  Forms!kontaktpersoner!Firmanavn = Null

Du er i øvrigt meget omhyggelig  ;o)

    Forms!kontaktpersoner!Firmanavn = "  "
    Forms!kontaktpersoner!Firmanavn = Null
En af dem burde være rigeligt.

    rs.Edit
    rs.AddNew
Den første er vist ikke nødvendig.
Avatar billede flemming39 Nybegynder
13. marts 2006 - 17:45 #2
Tak for dit svar og dine gode kommentarer.

Jeg har prøvet at lave en refresh men det ændrer desværre ikke værdien, men giver samme fejl.

Det er meget mystisk: når jeg afspiller funktionen fra en knap virker det men ikke når jeg afspiller funktionen fra en makro.

mvh Flemming
Avatar billede terry Ekspert
13. marts 2006 - 18:37 #3
This is just a guess. When you run the code from a button on the form, then its the forms recordset your adding a record to when you use
DoCmd.GoToRecord , , acNewRec

when you run the code from a macro then this doesnt know which form we want to add a record to.

But why do you want to use a macro when the button works?
Avatar billede flemming39 Nybegynder
13. marts 2006 - 18:54 #4
hi Terry

A customer of mine likes to have some of the "buttons" at the top where I use the "Tilpas" function when I do right click.

You might be right about the makro not knowing the whole story.

regards Flemming
Avatar billede terry Ekspert
14. marts 2006 - 09:19 #5
why dont you make a function in the form and call the function from the macro?
Avatar billede flemming39 Nybegynder
14. marts 2006 - 09:27 #6
Hi Terry

Because of my inexperience I think  :-)

Let me tjeck if I understand you correctly:
Instead of having the function declared outside the form ("global programkode"), you think that it will help to move the function to the form.

Im not able to test before tonight but I will let you know of the result
many regards from Flemming
Avatar billede terry Ekspert
14. marts 2006 - 09:38 #7
Isnt the code you gave in your question in a form, or are you calling the code from the form?

If the code is in a module, then maybe you can still use that, but get the macro to run some code in the form which calls the function in the module.

I'm not sure if this is goin gto help, but I think its worth a try. I have a feeling that its because the macro deosnt know which form we are trying to add a record to.

It may also be enough to set focus on the form (from the macro).

I never use macros's myself
Avatar billede flemming39 Nybegynder
16. marts 2006 - 08:24 #8
Im sure you are right Terry but I cant get it to work just by moving the function to the form.
I have been looking for some code to activate the button on the form, but I haven't been lucky so far.
I think I will give up. The environment acts apperently different when running code from a makro when we are speeking of recordsets belonging to a form and it is a luxurious problem I have here:
I will let the customer keep on using the button at the form.

Thanks for the comments and the help to both of you.

Flemming
Avatar billede fdata Forsker
16. marts 2006 - 19:11 #9
Velbekomme.
Avatar billede terry Ekspert
16. marts 2006 - 19:35 #10
thanks
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