LEN function (1 Viewer)

radek225

Registered User.
Local time
Today, 11:22
Joined
Apr 4, 2013
Messages
307
I want to find out that, that fields [kolory p] and [kolory t] in my table are filled or not.
So I built the code:
Code:
Private Sub Naklad_skladki_AfterUpdate()
Dim rst As Recordset
Dim strSQL As String
Dim przod As Boolean
Dim tyl As Boolean

strSQL = "SELECT [tblGoraZleceniaNowa].[id_gora_zlecenia], [tblGoraZleceniaNowa].[kolory P], [tblGoraZleceniaNowa].[kolory T] FROM tblGoraZleceniaNowa where tblGoraZleceniaNowa.id_zlecenia=" & Me!ID_Zlecenia

Set rst = CurrentDb.OpenRecordset(strSQL)

Do While Not rst.EOF
    If Len(Nz(rst![Kolory P], 0)) <> 0 Then
        przod = True
    End If
    If Len(Nz(rst![Kolory T], 0)) <> 0 Then
        tyl = True
    End If
    
    rst.MoveNext
    Loop
rst.Close
   MsgBox przod
   MsgBox tyl
   
End Sub

Don't know why, but even if rst![kolory P] or rst![kolory t] are empty, "przod" and "tyl" are true

I've checked it, and ms access show me number 1 when fields are empty from expression Len(Nz(rst![Kolory P], 0)) or Len(Nz(rst![Kolory T], 0)). Why? There should be "0" and "false" in my code.
 

pr2-eugin

Super Moderator
Local time
Today, 19:22
Joined
Nov 30, 2011
Messages
8,494
Just logically thinking Len(0) is 1 not 0, so even if it Null your condition will never fail. Try,
Code:
If Len(rst![Kolory T][COLOR=Red][B] & vbNullString)[/B][/COLOR] <> 0 Then
 

CazB

Registered User.
Local time
Today, 19:22
Joined
Jul 17, 2013
Messages
309
Sorry if this doesn't make sense, but:

if you are using Nz (expression, 0) then the length will never be 0, will it? If the 'expression' is blank, won't it put in 0 anyway, which means the length will be 1?

so

Code:
If Len(rst![Kolory P]) <> 0 Then
        przod = True
    End If
etc
 

pr2-eugin

Super Moderator
Local time
Today, 19:22
Joined
Nov 30, 2011
Messages
8,494
if you are using Nz (expression, 0) then the length will never be 0, will it?
Totally agree ! However, this code,
Code:
If Len(rst![Kolory P]) <> 0 Then
        przod = True
    End If
Will throw an error. As Len function cannot deal with Null, so the OP's intention to use an Nz function. You could do this.
Code:
If Len([COLOR=Red][B]Nz([/B][/COLOR]rst![Kolory P][COLOR=Red][B], vbNullString)[/B][/COLOR]) <> 0 Then
    przod = True
End If
 

CazB

Registered User.
Local time
Today, 19:22
Joined
Jul 17, 2013
Messages
309
sorry, yes... forgot that! serves me right for answering whilst also being on the phone... thanks for pointing out my schoolgirl error, lol....
 

smig

Registered User.
Local time
Today, 21:22
Joined
Nov 25, 2009
Messages
2,209
If rst![kolony p] & "" = "" then....

This will work both for null and for empty string

Dont put spaces and special characters into names so you can omit the square brackets
 

MarkK

bit cruncher
Local time
Today, 11:22
Joined
Mar 17, 2004
Messages
8,181
Also, you don't need "If". When the expression is True, the variable is True. When the expression is False, the variable is False. So just assign the expression directly to the variable . . .
Code:
przod = Nz(rst![Kolory P], "") <> ""
. . . but also, in the original code you posted, each loop overwrites the value of the variable in the previous loop . . .
Code:
Do While Not rst.EOF
[COLOR="Green"]    'get the values[/COLOR]
    przod = Nz(rst!KoloryP, "") <> ""
    tyl = Nz(rst!KoloryT, "") <> ""

[COLOR="Green"]    'show the values[/COLOR]
    msgbox przod & ", " & tyl

[COLOR="Green"]    'move next and loop, erases the previous values[/COLOR]
    rst.MoveNext
Loop
Your code only shows you the values for the last record.
 

smig

Registered User.
Local time
Today, 21:22
Joined
Nov 25, 2009
Messages
2,209
Also, you don't need "If". When the expression is True, the variable is True. When the expression is False, the variable is False. So just assign the expression directly to the variable . . .
Code:
przod = Nz(rst![Kolory P], "") <> ""

Markk,
Don't you think this one is better as it's not using a function ?
Code:
przod = rst![kolory p] & "" <> ""
 

MarkK

bit cruncher
Local time
Today, 11:22
Joined
Mar 17, 2004
Messages
8,181
I think Nz() makes it more clear at a glance that we are expecting Nulls, and in this case our code is largely designed to convert nulls to booleans, so to me it contributes to clarity. But this kind of thing too, to me, is in the eye of the beholder.

Some people argue that keeping the If...End If construct . . .
Code:
If someExpression = True then 
    someVariable = True
End If
. . . is also more clear than . . .
Code:
someVariable = someExpression
. . . and that's a fair point too. These are somewhat religious arguments, or issues of style--not meaning, so I would not defend my own preference as anything other than a preference.

To me, the ugliest thing here . . .
Code:
przod = rst![kolory p] & "" <> ""
. . . is the space in the fieldname and the square brackets.

Inside a With block it could look like this . . .
Code:
przod = Nz(!koloryP, "") <> ""
przod = !koloryP & "" <> ""
[COLOR="Green"]'or even[/COLOR]
przod = Len(!koloryP & "")
. . . but, at a glance, which one checks for Null? Right, all of them.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:22
Joined
Jan 20, 2009
Messages
12,852
Your code only shows you the values for the last record.

Neglecting the issue with the Nz, the code doesn't show the last record but rather it will end with the variable as True if any of the records are Not Null because there is nothing to return them to False.
 

smig

Registered User.
Local time
Today, 21:22
Joined
Nov 25, 2009
Messages
2,209
I think Nz() makes it more clear at a glance that we are expecting Nulls, and in this case our code is largely designed to convert nulls to booleans, so to me it contributes to clarity. But this kind of thing too, to me, is in the eye of the beholder.

Some people argue that keeping the If...End If construct . . .
Code:
If someExpression = True then 
    someVariable = True
End If
. . . is also more clear than . . .
Code:
someVariable = someExpression
. . . and that's a fair point too. These are somewhat religious arguments, or issues of style--not meaning, so I would not defend my own preference as anything other than a preference.

To me, the ugliest thing here . . .
Code:
przod = rst![kolory p] & "" <> ""
. . . is the space in the fieldname and the square brackets.

Inside a With block it could look like this . . .
Code:
przod = Nz(!koloryP, "") <> ""
przod = !koloryP & "" <> ""
[COLOR="Green"]'or even[/COLOR]
przod = Len(!koloryP & "")
. . . but, at a glance, which one checks for Null? Right, all of them.
I also commented about the space in the name :)
Something should be avoided at all cases.

I rarely use Nz() as it will only catch Null strings and not an empty "" ones :(
I mostly consider spaces only fields as empty too so ill mostly use
Trim(MyField & "") = ""
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:22
Joined
Jan 20, 2009
Messages
12,852
Some people argue that keeping the If...End If construct . . .
Code:
If someExpression = True then 
    someVariable = True
End If
. . . is also more clear than . . .
Code:
someVariable = someExpression

Except the two expressions are not equivalent.

The verbose version of the single line expression is:

Code:
If someExpression = True Then 
   someVariable = True
Else
   somevariable = False
End If
 

MarkK

bit cruncher
Local time
Today, 11:22
Joined
Mar 17, 2004
Messages
8,181
Yes, this is more accurately what I was talking about, If...Else...End If . . .
Code:
If someExpression = True Then 
   someVariable = True
Else
   somevariable = False
End If
I prefer this in code . . .
Code:
someVariable = someExpression
. . . but I have heard people argue against the latter on the grounds that it is confusing, or not clear enough.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:22
Joined
Jan 20, 2009
Messages
12,852
I prefer this in code . . .
Code:
someVariable = someExpression
. . . but I have heard people argue against the latter on the grounds that it is confusing, or not clear enough.

I much prefer it too. There is no question of clarity to anyone who understands logical expressions.

Concise code is readable code.

Using an elaborate structure is suggestive that there is something more complex that is going to be coded and, as such, it is misleading.
 

smig

Registered User.
Local time
Today, 21:22
Joined
Nov 25, 2009
Messages
2,209
As someone never used the single line expression (Never knew it's possible before :) ) I do agree it's a bit confusing.
An equal sign in the someExpression part can give the impression that both first Vars will get the value of the 3rd. one.
I think I understands logical expressions, but I can't see the question part here .. Is it ? ...
 

radek225

Registered User.
Local time
Today, 11:22
Joined
Apr 4, 2013
Messages
307
Neglecting the issue with the Nz, the code doesn't show the last record but rather it will end with the variable as True if any of the records are Not Null because there is nothing to return them to False.
Right

THX Guys. I just want to say, that first version code of pr2-eugin working good. Thanks for insight!
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:22
Joined
Jan 20, 2009
Messages
12,852
An equal sign in the someExpression part can give the impression that both first Vars will get the value of the 3rd. one.
.

You clearly don't understand how to read logical expressions.
 

smig

Registered User.
Local time
Today, 21:22
Joined
Nov 25, 2009
Messages
2,209
You clearly don't understand how to read logical expressions.
In math and for most people writing:
A = B = 3
Is the same as:
A = B and B = 3, result A and B = 3

for you it's only make sense it's like saying:
If B = 3 then A is True Else A is False


So maybe I don't understand how to read logical expressions, but I'm happy to be on the normal side of the globe :D
 

smig

Registered User.
Local time
Today, 21:22
Joined
Nov 25, 2009
Messages
2,209
Only if you read it backwards.

You are in the computers business for too many years, so you forgot a basic math :D
I'm Hebrew speaker so I normally read backward :p
 

Users who are viewing this thread

Top Bottom