Number of empty values. (1 Viewer)

shamal

Registered User.
Local time
Today, 02:44
Joined
Sep 28, 2013
Messages
77
Welcome..
How to calculate empty fields using the Visual Basic function.

I used this function in a query, but I want it in a function using Visual Basic
D_Null: IIf(IsNull([sn]);1;0)+IIf(IsNull([an]);1;0)+IIf(IsNull([en]);1;0)+IIf(IsNull([tn]);1;0)+IIf(IsNull([mn]);1;0)+IIf(IsNull([kn]);1;0)
 

Attachments

  • Null deg.png
    Null deg.png
    16.6 KB · Views: 29

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:44
Joined
May 7, 2009
Messages
19,243
put in a Module:
Code:
Public Function fnCountEmpty(ParamArray pArray() As Variant) As Integer
    Dim ret As Integer
    Dim v As Variant
    For Each v In pArray
        ret = ret + IsNull(v)
    Next
    fnCountEmpty = ret * -1
End Function

change your Query to:
Code:
D_Null: fnCountEmpty([sn], [an], [en], [tn], [mn], [kn])
 

shamal

Registered User.
Local time
Today, 02:44
Joined
Sep 28, 2013
Messages
77
put in a Module:
Code:
Public Function fnCountEmpty(ParamArray pArray() As Variant) As Integer
    Dim ret As Integer
    Dim v As Variant
    For Each v In pArray
        ret = ret + IsNull(v)
    Next
    fnCountEmpty = ret * -1
End Function

change your Query to:
Code:
D_Null: fnCountEmpty([sn], [an], [en], [tn], [mn], [kn])
Thank you
Very excellent function
 

ebs17

Well-known member
Local time
Today, 11:44
Joined
Feb 7, 2020
Messages
1,946
But you already realize that both variants are workarounds because the starting point is a crosstab and therefore an unnormalized table.
With a previous unpivot you would get a normalized table and could start a simple query.
SQL:
SELECT
   AttributID,
   n_Field,
   COUNT(*) - COUNT(n_Field) AS EmptyFields
FROM
   TableX
WHERE
   n_Field IN("sn", "an", "en", "tn", "mn", "kn")
GROUP BY
   AttributID,
   n_Field
 

shamal

Registered User.
Local time
Today, 02:44
Joined
Sep 28, 2013
Messages
77
But you already realize that both variants are workarounds because the starting point is a crosstab and therefore an unnormalized table.
With a previous unpivot you would get a normalized table and could start a simple query.
SQL:
SELECT
   AttributID,
   n_Field,
   COUNT(*) - COUNT(n_Field) AS EmptyFields
FROM
   TableX
WHERE
   n_Field IN("sn", "an", "en", "tn", "mn", "kn")
GROUP BY
   AttributID,
   n_Field
I don't understand your method.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:44
Joined
Feb 19, 2002
Messages
43,275
@ebs17 has assumed that the image you posted is not from a table but from a query made with a crosstab. The schema looks unnormalized so that is an obvious conclusion. If ebs is correct and the data is actually normalized, evaluating the data directly rather than crosstabing first is a better solution. However, I don't like the suggested solution since it hardcodes the In() clause. A better solution would be to use a table to control the "IN", especially since in a normalized schema, you shouldn't have empty rows anyway so the hardcoded In() wouldn't work since the empty rows would not exist.
 

ebs17

Well-known member
Local time
Today, 11:44
Joined
Feb 7, 2020
Messages
1,946
since it hardcodes the In() clause
This hardcoding is also available in the above two variants, so it is just a simple comparison.
You can also design it differently, but you should know the environment.
 

Users who are viewing this thread

Top Bottom