Solved How to convert "" to null in MS Access VBA (2 Viewers)

nector

Member
Local time
Today, 15:23
Joined
Jan 21, 2020
Messages
368
The field below is a date release now sometimes the date field is empty , now in the Json dictionary it keeps return "" instead of null as result I'm always getting an error.

Code:
Company.Add "stockRlsDt", rs!stockreleasing.value


I want this field to return null if empty and actual date if not null
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:23
Joined
May 7, 2009
Messages
19,245
maybe:

Company.Add "stockRlsDt", IIF(IsNull(rs!stockreleasing), Null, rs!stockreleasing)
 

cheekybuddha

AWF VIP
Local time
Today, 13:23
Joined
Jul 21, 2014
Messages
2,280
I would have thought:
Code:
Company.Add "stockRlsDt", IIF(Len(rs!stockreleasing & "") = 0, Null, rs!stockreleasing)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:23
Joined
May 7, 2009
Messages
19,245
Company.Add "stockRlsDt", IIF(Len(rs!stockreleasing & "") = 0, Null, rs!stockreleasing)
it same as #2, since you cannot get the Length of a Null, so you add it to a NullString.
 

cheekybuddha

AWF VIP
Local time
Today, 13:23
Joined
Jul 21, 2014
Messages
2,280
date field is empty , now in the Json dictionary it keeps return "" instead of null

Yes, but #2 does not add Null for empty string.
 

Josef P.

Well-known member
Local time
Today, 14:23
Joined
Feb 2, 2023
Messages
827
Company.Add "stockRlsDt", IIF(Len(rs!stockreleasing) = 0, Null, rs!stockreleasing)' <-- equal to #3
or (better readable):
Company.Add "stockRlsDt", NullIf(rs!stockreleasing, vbnullstring)
with:
Code:
Public Function NullIf(Value As Variant, expression As Variant) As Variant
   If Value = expression Then
      NullIf = Null
   Else
      NullIf = Value
   End If
End Function
 

cheekybuddha

AWF VIP
Local time
Today, 13:23
Joined
Jul 21, 2014
Messages
2,280
Company.Add "stockRlsDt", IIF(Len(rs!stockreleasing) = 0, Null, rs!stockreleasing)' <-- equal to #3
Not quite - if rs!stockreleasing is actually Null then you will get an error.

Therefore safe to cover both possibilities like in #3.

NullIf() is nice addition too. (y)
 

Josef P.

Well-known member
Local time
Today, 14:23
Joined
Feb 2, 2023
Messages
827
Not quite - if rs!stockreleasing is actually Null then you will get an error.
I'll bet against it ;)
VBA.len(null) => null
 

cheekybuddha

AWF VIP
Local time
Today, 13:23
Joined
Jul 21, 2014
Messages
2,280
I guess (without NullIf()) you can tidy even more to:
Company.Add "stockRlsDt", IIF(Len(rs!stockreleasing), rs!stockreleasing, Null)
 

Josef P.

Well-known member
Local time
Today, 14:23
Joined
Feb 2, 2023
Messages
827
Null equates to False in the IIf()
I'm being picky here: Null is neither true nor false, but it leads into the Else part of IIF.
=>
Company.Add "stockRlsDt", IIF(Len(rs!stockreleasing) = 0, Null, rs!stockreleasing)
Then rs!stockreleasing is inserted, which is Null in this case.
 

cheekybuddha

AWF VIP
Local time
Today, 13:23
Joined
Jul 21, 2014
Messages
2,280
I'm being picky here: Null is neither true nor false, but it leads into the Else part of IIF.
=>
Company.Add "stockRlsDt", IIF(Len(rs!stockreleasing) = 0, Null, rs!stockreleasing)
Then rs!stockreleasing is inserted, which is Null in this case.
Yes, my phone did something weird and replied before I had finished editing, already fixed
 

nector

Member
Local time
Today, 15:23
Joined
Jan 21, 2020
Messages
368
I'm still getting the same "" instead of null by using the provided function:

Code:
Company.Add "stockRlsDt", NullIf(rs!stockreleasing.Value, Null)
 

cheekybuddha

AWF VIP
Local time
Today, 13:23
Joined
Jul 21, 2014
Messages
2,280
I'm still getting the same "" instead of null by using the provided function:

Code:
Company.Add "stockRlsDt", NullIf(rs!stockreleasing.Value, Null)
Check Josef's suggestion more carefully
 

nector

Member
Local time
Today, 15:23
Joined
Jan 21, 2020
Messages
368
Okay thanks , this one has worked

Code:
IIF(Len(rs!stockreleasing), rs!stockreleasing, Null)


This was given by Josef P.

Many thanks him and all the contributors.

Regards

Chris
 

cheekybuddha

AWF VIP
Local time
Today, 13:23
Joined
Jul 21, 2014
Messages
2,280
With the NullIf(), you used a different value for the second argument than what Josef had suggested.

(y)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:23
Joined
May 7, 2009
Messages
19,245
But if it is Null then it will return Null, which is what is wanted, no?
yes, i tested it you are Really a Genius for giving that function, don't you think?
 

Users who are viewing this thread

Top Bottom