30. august 2010 - 10:06 Der er 8 kommentarer og
1 løsning

BeforeUpdate(Cancel as Integer) Cancel = True slipper ikke focus.

Jeg proever i en Access 2007 application at designe en formular til at updatere en tabel med adresser paa organisationer.  Formularen har en textbox 'txtOrganisation' og en knap 'cmdUpdate'.  Efter at brugeren i en anden formular har valgt en organisation aabnes formularen med navnet paa den valgte organisation i 'txtOrganisation' og med focus deri.  Jeg vil saa at naar brugeren aendrer navnet i 'txtOrganisation' saa opdateres tabellen ikke umiddelbart men foerst naar der trykkes paa 'cmdUpdate' (efter at der er foretaget visse andre handlinger.) Jeg har derfor sat BeforeUpdate(Cancel as Integer) eventen til Cancel = True.  Det virker saaledes at vaerdien i tabellen ikke aendrer sig naar jeg aendrer vaerdien i 'txtOrganisation'.  Men saa vil textboxen ikke slippe focus.  Hvis efter at have forandret vaerdien i 'txtOrganisation' saa klikker paa 'cmdUpdate' eller paa en anden control i formularen sker der ingenting, men curseren bliver ved med at blinke i 'txtOrganisation'.  Hvis jeg efter at formularen er aaben undlader at aendre navnet i 'txtOrganisation' saa kan jeg uden problemer klikke paa knappen.  Fordi jeg har brug for OldValue egenskaben er jeg noedt til at binde 'txtOrganisatie' til tabellen (eller er jeg?)

Saa jeg soeger efter en maade hvor jeg i en tekstbox der er bundet til et felt i en tabel kan aendre vaerdien uden at vaerdien aendrer sig i tabellen indtil jeg har aktiveret en anden control.  Alternativt soeger jeg en maade hvor jeg i en ikke-bunden tekstbox kan bevare en 'oldvalue'.
Avatar billede terry Ekspert
30. august 2010 - 10:34 #1
I assume that you mean the before update event for the textbox?

Data in the table should get altered until the AFTER UPDATE event so I'm a bit puzzled as to why you indicate that it does.

The befor eupdate event can be used to evaluate the entered data and if in case of an error you can set the cancel update to true so that focus stays in that field until it is corrected.
Avatar billede terry Ekspert
30. august 2010 - 10:36 #2
30. august 2010 - 10:59 #3
terry, thanks for quick reaction.  Yes, it is the BeforeUpdate event for the textbox I am talking about.  I agree that it is not during the BeforeUpdate event that the record in the table is updated, but I want to prevent the table from being updated, and for that I use the BeforeUpdate event.

And you confirm my experience, that focus stays in the textbox.

The link you gave shows that if I include "Me.txtOrganisatie.Undo" in the code then I can move the focus, but then the value reverts to the old value.

What I am trying to achieve is (1) The user enters a new value in txtOrganisatie. (2) This new value stays in the textbox for later use, (3) but the record in the table is not updated.  (4) Later the record in the table may or may not have to be updated depending on other actions and then after for example an OnClick event on t button.

How am I to achieve this?
Avatar billede terry Ekspert
30. august 2010 - 11:37 #4
If you need to validate a specific field then do it in the controls before update event.


If you need to validate more than one field before commiting the data to the dB then I would do it in the forms before update event. Then set cancel to true if one or more fields are invalid.


You should read that link I gave it contains information as to when changes are commited to the db and you mnay need to take these into account.
30. august 2010 - 12:08 #5
I was hoping a quick fix existed.  I may have to do some more brainstorming on the whole problem and then come back.

(One of the issues is this:  if the adress belonging to a name is changed and there are several persons living on the adres, such as father, mother, and children, then it needs to be checked whether the whole family moves and we need an UPDATE with the new adres replacing the old adress, or it is one of the children that moves out and we need an INSERT with the new adress added.  And if a telephone number is changed it needs to be checked whether the person changes telephonenumber or gets one additional one.)

(As for when changes are committed I am presently studying "Access 2007 VBA"  by Scott B. Diamond, and he goes into quite some details on these matters (and I read the link you gave me, thanks.))

A rethink and then I come back.
01. september 2010 - 13:29 #6
So you sent me in another direction.  I am starting slow, just with the name of the organisation.  I have in the table an aritifial organisation with the name (New) - it is in brackets so that it will come up first in a list sortet alfatetically.  If the user wants to change the name of an existing organisation he choses that name in the form1 which brings up the name in a textbox in form2.  A change to that textbox results in an update to the database.  If the user wants to add a new organisation he choses (New) which brings up (New) in the textbox.  When the user changes (New) to the name of the new organisation I wanted not to update the table, thereby overwriting (New) but to insert the new name.  The way I have now solved it is this:  Before update I save the textbox's old text to a variable s.  Then I let (New) overwrite in the table.  Then in the form close event I test whether s = (New) and if so I insert a new organisation with the name (New).

Please create a 'svar' for having my rethink.  Maybe you also want to comment on this method?
Avatar billede terry Ekspert
01. september 2010 - 18:35 #7
Christian, give yourself the points I havent been too helpful here.
01. september 2010 - 18:38 #8
I disagree, but anyway..
Avatar billede terry Ekspert
01. september 2010 - 19:03 #9
I think I understand the idea with using (New) but its difficult to comment on it as I dont have enough understanding of your database or requirements.

I read your commone at #5 again and maybe it would be possible to prompt the user if they try altering an address where one or more people live as to whether or not the whole family or only some of them are moving to a new address. Then depeneding on what they answer you can react accordingly.

All family: Update
Some of:    Open a from where its possible to choose those who are moving and then insert new address and undo changes to old record.
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