fields empty are not empty??? (1 Viewer)

mloucel

Member
Local time
Today, 01:59
Joined
Aug 5, 2020
Messages
153
Hello Gurus:

I have a weird issue [not life and dead], I have a database that when I EDIT the record on a form, the user has to press a button to save, I do that so that I have the opportunity to check some fields that the end user MUST fill so I do a quick : "If isnull(XYZ) ", which works many times, but somehow, sometimes it doesn't see the field empty, even though it is empty, just for fun I did a test, I entered a new record and on purpose I left 3 fields empty, my routine detected perfectly 2 of them and somehow the 3rd one, just simply ignore and saved the record, so I went to my EDIT FORM, recall the record and without doing anything I clicked SAVE, and it did, so I recall the record again, I add spaces, saved again, recall and hit the delete key a few times [40 or 50 in a 15 chr field], then hit save, and it did it again, then I add 1 letter, save no problem as it should, then open again and delete the letter, this time gave me the warning..
So I added " If isnull(XYZ) or XYZ="" ", and somehow the error never happened again, coincidence ?, I removed the = "", and after 4 or 5 new records it happened again, I have tried with other fields and get the same issue, so is not just 1 field I have detected at least 2 more.
This only happens when there is a new record, using "DoCmd.GoToRecord , , acNewRec" something is placed in a field and is invisible, but when I use the XYZ="" then the problem is solved, i am forced to use that, but I do not understand why isnull() does not work.

Any Ideas?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:59
Joined
May 21, 2018
Messages
8,529
Set this to NO
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:59
Joined
Sep 21, 2011
Messages
14,301
Hello Gurus:

I have a weird issue [not life and dead], I have a database that when I EDIT the record on a form, the user has to press a button to save, I do that so that I have the opportunity to check some fields that the end user MUST fill so I do a quick : "If isnull(XYZ) ", which works many times, but somehow, sometimes it doesn't see the field empty, even though it is empty, just for fun I did a test, I entered a new record and on purpose I left 3 fields empty, my routine detected perfectly 2 of them and somehow the 3rd one, just simply ignore and saved the record, so I went to my EDIT FORM, recall the record and without doing anything I clicked SAVE, and it did, so I recall the record again, I add spaces, saved again, recall and hit the delete key a few times [40 or 50 in a 15 chr field], then hit save, and it did it again, then I add 1 letter, save no problem as it should, then open again and delete the letter, this time gave me the warning..
So I added " If isnull(XYZ) or XYZ="" ", and somehow the error never happened again, coincidence ?, I removed the = "", and after 4 or 5 new records it happened again, I have tried with other fields and get the same issue, so is not just 1 field I have detected at least 2 more.
This only happens when there is a new record, using "DoCmd.GoToRecord , , acNewRec" something is placed in a field and is invisible, but when I use the XYZ="" then the problem is solved, i am forced to use that, but I do not understand why isnull() does not work.

Any Ideas?
That is what the Form's Before_Update event is for?
 

Mike Krailo

Well-known member
Local time
Today, 04:59
Joined
Mar 28, 2020
Messages
1,044
Absolutely what Gasman said. Use the forms before update to prevent user from saving records with bad data. Then use that property that MajP suggested to disallow zero length strings. The point though is not to put your validation code in the "on click" event of your save button. Don't do that, otherwise Pat Hartman will be on you like white on rice. 😄
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:59
Joined
Feb 19, 2002
Messages
43,275
Here's two videos along with a sample database to help you to learn about form events. The views are about why you should use the BeforeUpdate event for validation.

 

mloucel

Member
Local time
Today, 01:59
Joined
Aug 5, 2020
Messages
153
Good old Richard..., I
Can you elaborate [ If somecontrol & "" = "" ], sorry I am still learning and I do not have much experience.
 

mloucel

Member
Local time
Today, 01:59
Joined
Aug 5, 2020
Messages
153
That is what the Form's Before_Update event is for?
Thanks, but I a work in progress, sorry but I understand the concept of the before update event, but in my head I don't know how to apply it, my LOGIC says that when the user clicks the save button I can, check and recheck as many times as needed, but in the event, I'm lost.., really lost, I would love to make better code and use your suggestions, of course you guys know more than me, I imagine eventually I will find and example of wht you are suggesting and I can learn, in the meantime I will have to stick to my button.

Thank you @Gasman
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:59
Joined
May 21, 2018
Messages
8,529
If isnull(XYZ) or XYZ=""
This can be written as one check
Code:
if (XYZ & "") = ""

That is because if it is Null then Null & "" = ""
if it already is "" then "" & "" = ""
So that check works for both cases
A third option is
Code:
if trim(xyz & "") = ""
This checks for a a third case too. It is rare but possible to get a space, " " which is something different than a ZLS, "". So the above checks for Null, "", or " ".
That is because trim(" " & "") = ""
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:59
Joined
Sep 21, 2011
Messages
14,301
Thanks, but I a work in progress, sorry but I understand the concept of the before update event, but in my head I don't know how to apply it, my LOGIC says that when the user clicks the save button I can, check and recheck as many times as needed, but in the event, I'm lost.., really lost, I would love to make better code and use your suggestions, of course you guys know more than me, I imagine eventually I will find and example of wht you are suggesting and I can learn, in the meantime I will have to stick to my button.

Thank you @Gasman
Watch the videos mentioned in post #6
 

mloucel

Member
Local time
Today, 01:59
Joined
Aug 5, 2020
Messages
153
This can be written as one check
Code:
if (XYZ & "") = ""

That is because if it is Null then Null & "" = ""
if it already is "" then "" & "" = ""
So that check works for both cases
A third option is
Code:
if trim(xyz & "") = ""
This checks for a a third case too. It is rare but possible to get a space, " " which is something different than a ZLS, "". So the above checks for Null, "", or " ".
That is because trim(" " & "") = ""
Darn... best explain ever.. thanks man, I use the TRIM option seems to be the most sensible, working like a charm.

THANKS SO MUCH..
 

Users who are viewing this thread

Top Bottom