14. december 2007 - 13:02Der er
28 kommentarer og 1 løsning
Checke om et felt er udfyldt
I tidligere sager her på eksperten er dette foreslået løst således:
I formularens FørOpdatering linkes der til følgende kode
Private Sub Form_Before_update(Cancel as integer)
If IsNull(Me.Kunde_Id) Then MsgBox "SKAL UDFYLDES" DoCmd.GoToControl "Kunde_Id" End If
Men prøv nu at høre her!
I en hver formular designes der en eller anden action knap - foreksempel en gem knap eller en knap der skifter til næste post (og gemmer dermed også det du har indtastet).
Jeg har tidligere lavet en database, hvor jeg checker om felterne er udfyldt, når action knappen aktiveres. Jeg giver en msgbox, hvis et felt ikke er udfyldt og sætter fokus i feltet. Jeg omgår dog dette på snedig vis, hvis formen eller databasen lukkes. Det virker fint.
Nu har jeg en ny database med en masse felter, og her vil jeg gerne give brugeren besked med det samme, når brugeren hopper ud af feltet.
Jeg kan ikke få den viste kodestump til at virke. Problemet er jo netop, at det kun sker, hvis der opdateres noget som helst (og det gør der jo ikke, hvis der ikke er udfyldt noget).
Jeg har prøvet at bruge -Kunde_Id_AfterUpdate -Kunde_Id_Exit -Kunde_Id_LostFocus og kombinere med noget cancel eller noget If dirty then undo. Men jeg kan ikke få det til at virke korrekt.
Hvis jeg checker for Null værdi ved for eksempel Kunde_Id_Exit og giver en MesssageBox, så virker det fint, når jeg TAB’er ud af feltet.
Problem: Problemet opstår når jeg lukker formen med ”krydset” eller aktiverer min egen ”luk-knap” imen jeg står i feltet. Hændelsen ”Kunde_Id_Exit” går jo igang så snart jeg forsøger at aktivere alt andet en selve feltet.
Spørgsmål: Følgende er pseudokode for at illustrere min idé: Kan man lave noget: If FormEvent.Status.IsClosing = True Then Exit sub Else If IsNull(Me.Kunde_Id) Then MsgBox "SKAL UDFYLDES" DoCmd.GoToControl "Kunde_Id" End If End if
Eller kan det slet ikke lade sig gøre at lave noget sådant i Access2003?
Hvis indtastning er obligatorisk, kan du sætte den egenskab direkte i tabellen, således at det ikke kan forlades uden at der er een eller anden værdi ... er det det du vil ?
Hvis du 'aktiverer min egen luk-knap' kan du vel lægge kontrollen på koden til luk-knappen. Og der er vel et eller andet kode på 'klik på krydset' og så kan du lægge kontrollen der?
Nej. Jeg ved godt jeg kan bruge Access egen funktionalitet til kontrol på den måde du beskriver. Mit problem er, at mine brugere ikke forstår den kryptiske besked Microsoft har valgt at komme med. Den siger direkte noget om Null værdi og de ved ikke hvad null det er.
Det jeg skriver nu giver ikke meget karma - undskyld.
Jeg skriver jo netop at jeg har løst problemet før, hvis jeg vælger at lægge kontrollen når action knappen eller luk knappen aktiveres. Jeg vil lave min egen kode/besked, når brugen TAB'er ud af feltet.
Does not work on the flight (Meaning if you just TAB into the field and TAB out again without doing anything else).
Does only work if: -TAB to the field Kunde_Id -fill in data -TAB out of the field -TAB into the field again -Delete the data. -TAB out of the field ==> then the messagebox comes up.
?? If you only have one field´on the form and you havent changed data in that field then you dont have a dirty form. And i fthe form isnt dirty how do you know that the user actually wants to enter/change anything?
But you could also NOT check that the form is dirty if thats what you want.
If Len(Me.txt & "") = 0 Then MsgBox "Enter some data" Me.txt.SetFocus End If
You are pinpointing a problem that I wondered about.
I made the test on the first field in the form - could that be the reason. Anyway it does not help a lot then.
The form should be dirty since I have many fields in the form.
And the user Must enter data in every field - all fields are mandatory.
I think it is because the field is "Empty" if you just TAB trough the field. The code does not react on "Empty". If you fill in data and delete them, then the field is "Null", and the code begins to work correctly.
The form is dirty only when you have started entering data into a field and its still dirty if you remove the entered data. If you have the selection bar visible on the left of the form (record) you will see a pencil if the record is dirty.
Now I'm not 100% sure (will check) but if the form is dirty and you TAB into a field and the out of the field, I would have thought that you would get a lost focus event.
Just TABing through the controls on a form doesnt make a form dirty, you have to start altering/entering data.
Yes, if the form is dirty (data has changed) and you tab into a field and out again you will get a lost focus event.
So you have to decide. Is a user allowed to tab through the fields as long as the form is NOT dirty (changed) without being prompted to enter data? Or do you want to force the user to enter dat, and if so you have to implement code for all fields.
I have written code for all fields all ready, and I want the message to come up if the user just by accident or by purpose TAB trough the field without entering eny data.
But the LostFocus event does not take place, when I just TAB trough the field. ???? I'm lost
Oh, off couse. You told me already how Me.Dirty really works. So I have done as advised, and the message comes up now - even if I TAB trough the field.
But now the original problem still resides:
Problem: Problemet opstår, når jeg lukker formen med ”krydset” eller aktiverer min egen ”luk-knap” imens jeg står i feltet. Hændelsen ”Kunde_Id_Exit” går jo igang så snart jeg forsøger at aktivere alt andet en selve feltet.
I tried with some extra code:
Private Sub Form_Close()
Me.Undo
End Sub
But it does not help, the message comes up anyway.
Well first I find it unnormal to get the suer to enter data when they dont want to, just because they happened to press TAB. It is not user friendly. You could have a ADD record button which the user has to press if they do really want to enter a record.
I'm sure its possible to find a solution if the user tries to close the form, but there is a problem. If the user was actually entering data but hadnt finished entering data in all fields then you actually want to see the message.
So in some cases you want to see the message and in other cases not.
As I wrote the problem deactivating the x button and make an exit button yourself is, that the exit or lost_focus event on the object comes before the On_Click event on the botton.
I think the answer is, that this is not possible in Access2003.
By the way all events on the form comes after the events on the object. The following actions comes after the object Lost_Focus event: 1. Form_Unload 2. Form_Deactivate 3. Form_Close
If Me.Form.OnClose = "" Then If CurNr = "0" Then MsgBox "Du SKAL skrive dit navn i feltet", vbExclamation 'Cancel = True Me.Nummer.SetFocus End If End If
It is allowed to write Me.Form.Onclose but it is not of type Integer Boolean String Which type is it
In which situations do one use Me.Form.OnClose.
In this situation the smart thing about the syntax is, that it is not a separate procedure/function as: Private Sub Form_Close(Cancel As Integer)
So if it shift state as, you can put the value into a variable and make decitions on it.
I can see the problem but dont have a solution for it.
I think you should re-consider what you want to achieve, as mentioned proviosuly trying to force a user to enter data just becuase they happened to press TAB or move into another field with th emouse is not normal.
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.