Avatar billede klk Nybegynder
14. december 2007 - 13:02 Der 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?
Avatar billede jensen363 Forsker
14. december 2007 - 13:08 #1
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 ?
Avatar billede petersen7913 Forsker
14. december 2007 - 13:11 #2
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?
Avatar billede klk Nybegynder
14. december 2007 - 13:14 #3
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.
Avatar billede klk Nybegynder
14. december 2007 - 13:17 #4
Kommentar til Petersen7913.

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.

Iøvrigt tak for hurtigt svar!!
Avatar billede klk Nybegynder
14. december 2007 - 13:19 #5
Jeg bør tilføje at Kunde_Id er et felt i formen.
Avatar billede terry Ekspert
14. december 2007 - 13:23 #6
I would put the form validation in the forms before update event.


If Len(Me.txt) = 0 Then
    MsgBox "Enter some data in txt"
    Cancel = True
End If

and you could also choose to put the cursor in the field

Me.txt.SetFocus
Avatar billede terry Ekspert
14. december 2007 - 13:24 #7
If you have more than one field validation then you also need to

Exit sub

Unless you want to tell the user that other fields also have problems
Avatar billede klk Nybegynder
14. december 2007 - 13:27 #8
For Terry:
Does that work if:
The user TABs out of the field without filling enything in.

I am like you sure it works correct it the user close the form (the message does not come up).
Avatar billede terry Ekspert
14. december 2007 - 13:28 #9
Private Sub txt_LostFocus()

If Me.Dirty Then

If Len(Me.txt & "") = 0 Then
    MsgBox "Enter some data"
    Me.txt.SetFocus
End If
Avatar billede terry Ekspert
14. december 2007 - 13:29 #10
If Me.Dirty Then

If Len(Me.txt & "") = 0 Then
    MsgBox "Enter some data"
    Me.txt.SetFocus
End If

end if
Avatar billede klk Nybegynder
14. december 2007 - 13:30 #11
That was some interesting stuff!! I'll try imediately
Avatar billede klk Nybegynder
14. december 2007 - 13:58 #12
No!

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.
Avatar billede terry Ekspert
14. december 2007 - 14:12 #13
??
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
Avatar billede terry Ekspert
14. december 2007 - 14:13 #14
I'm off home for the weekend, will take a look later.
Avatar billede klk Nybegynder
14. december 2007 - 14:27 #15
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.
Avatar billede terry Ekspert
14. december 2007 - 17:13 #16
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.

Is it possible to see your dB?

ekspertenATsanthell.dk

AT = @
Avatar billede terry Ekspert
14. december 2007 - 17:24 #17
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.
Avatar billede klk Nybegynder
16. december 2007 - 21:46 #18
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
Avatar billede terry Ekspert
16. december 2007 - 22:21 #19
I can tale a look if you like
14/12-2007 17:13:06
Avatar billede klk Nybegynder
17. december 2007 - 11:36 #20
I have made a quit stupid test database, that shows the problem very properly. I have send it to you
Avatar billede terry Ekspert
17. december 2007 - 14:33 #21
Hi Karsten
The LostFocus eventworks as it should. The reason why the message doeasnt get shown is because the form is not dirty (no data changed).

If you always want the message to get shown, even when no data is changed then dont use

If Me.Dirty Then
Avatar billede klk Nybegynder
17. december 2007 - 15:24 #22
Hi Terry

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.
Avatar billede terry Ekspert
17. december 2007 - 15:50 #23
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.
Avatar billede terry Ekspert
17. december 2007 - 15:52 #24
You could also deactive the X (close form) button which gives you more control over what you want to do.
Avatar billede klk Nybegynder
17. december 2007 - 16:20 #25
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
Avatar billede klk Nybegynder
17. december 2007 - 18:11 #26
Does anyone know the meaning of this:

    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.
Avatar billede terry Ekspert
18. december 2007 - 08:40 #27
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.
Avatar billede klk Nybegynder
21. december 2007 - 09:09 #28
Jeg accepterer svaret Terry - Tak!
Avatar billede terry Ekspert
21. december 2007 - 14:40 #29
selv tak
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