1

I apologize if this question already exists - I've tried looking for a solution and have not found one yet.

I have a database template that is used to set up individual databases to process client data that is not always in the same format or sometimes has additional fields. I am trying to build a query that will test the results of a cumulative table and ensure that the analyst who set up the new database did not miss anything. Ultimately I just need a way to show if there are any fields in the cumulative table that are NULL for every record and tell me the field names.

I've tried setting up a query using SWITCH and IIF but there are too many fields involved so invariably I get the Expression Too Complex error.

This is an example of the query, though my database will ultimately have 70 fields that need to be tested for null values after setup.

SELECT SWITCH(
TBL_Cumulative.[ScreeningDate] IS NULL, "ScreeningDate Missing",
TBL_Cumulative.[ScreeningLocation] IS NULL, "ScreeningLocation Missing",
TBL_Cumulative.[Source] IS NULL, "Source Missing",
TBL_Cumulative.[FirstName] IS NULL, "FirstName Missing",
TBL_Cumulative.[LastName] IS NULL, "LastName Missing",
TBL_Cumulative.[DOB] IS NULL, "DOB Missing",
TBL_Cumulative.[Female] IS NULL, "Female Missing",
TBL_Cumulative.[Male] IS NULL, "Male Missing",
TBL_Cumulative.[Height_FT] IS NULL, "Height_FT Missing",
TBL_Cumulative.[Height_IN] IS NULL, "Height_IN Missing",
TBL_Cumulative.[Weight] IS NULL, "Weight Missing",
TBL_Cumulative.[Fasting_Y] IS NULL, "Fasting_Y Missing",
TBL_Cumulative.[Fasting_N] IS NULL, "Fasting_N Missing",
TBL_Cumulative.[Pregnant_Y] IS NULL, "Pregnant_Y Missing",
TBL_Cumulative.[Pregnant_N] IS NULL, "Pregnant_N Missing",
TBL_Cumulative.[Tobacco_Y] IS NULL, "Tobacco_Y Missing",
TBL_Cumulative.[Tobacco_N] IS NULL, "Tobacco_N Missing",
TBL_Cumulative.[hbA1c] IS NULL, "hbA1c Missing",
TBL_Cumulative.[Cotinine] IS NULL, "Cotinine Missing",
TBL_Cumulative.[TSH] IS NULL, "TSH Missing",
TBL_Cumulative.[PSA] IS NULL, "PSA Missing") AS Error FROM TBL_Cumulative

3 Answers 3

1

I think you're looking for a CASE statement.

SELECT CASE WHEN TBL_Cumulative.[ScreeningDate] IS NULL THEN 'ScreeningDate Missing' 
            WHEN TBL_Cumulative.[ScreeningLocation] IS NULL THEN 'ScreeningLocation Missing'
            WHEN TBL_Cumulative.[Source] IS NULL THEN 'Source Missing',
            WHEN TBL_Cumulative.[FirstName] IS NULL THEN 'FirstName Missing',
            WHEN TBL_Cumulative.[LastName] IS NULL THEN 'LastName Missing',
            WHEN TBL_Cumulative.[DOB] IS NULL THEN 'DOB Missin',
            WHEN TBL_Cumulative.[Female] IS NULL THEN 'Female Missing',
            WHEN TBL_Cumulative.[Male] IS NULL THEN 'Male Missing',
            WHEN TBL_Cumulative.[Height_FT] IS NULL THEN 'Height_FT Missing',
            WHEN TBL_Cumulative.[Height_IN] IS NULL THEN 'Height_IN Missing',
            WHEN TBL_Cumulative.[Weight] IS NULL THEN 'Weight Missing',
            WHEN TBL_Cumulative.[Fasting_Y] IS NULL THEN 'Fasting_Y Missing',
            WHEN TBL_Cumulative.[Fasting_N] IS NULL THEN 'Fasting_N Missing',
            WHEN TBL_Cumulative.[Pregnant_Y] IS NULL THEN 'Pregnant_Y Missing',
            WHEN TBL_Cumulative.[Pregnant_N] IS NULL THEN 'Pregnant_N Missing',
            WHEN TBL_Cumulative.[Tobacco_Y] IS NULL THEN 'Tobacco_Y Missing',
            WHEN TBL_Cumulative.[Tobacco_N] IS NULL THEN 'Tobacco_N Missing',
            WHEN TBL_Cumulative.[hbA1c] IS NULL THEN 'hbA1c Missing',
            WHEN TBL_Cumulative.[Cotinine] IS NULL THEN 'Cotinine Missing',
            WHEN TBL_Cumulative.[TSH] IS NULL THEN 'TSH Missing' 
        END AS ERROR
FROM TBL_Cumulative
Sign up to request clarification or add additional context in comments.

1 Comment

Pretty sure CASE isn't part of Access SQL.
1

This should create the query for you. It will throw an error if the query already exists.

Public Sub Test()

    CreateQuery "TBL_Cumulative"

End Sub

Public Sub CreateQuery(TableName As String)

    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim rst As DAO.Recordset
    Dim fld As DAO.Field
    Dim SQL_Select As String
    Dim SQL_Where As String
    Dim SQL As String

    Set db = CurrentDb
    Set rst = db.OpenRecordset(TableName)

    SQL_Select = "SELECT "
    SQL_Where = "WHERE "
    For Each fld In rst.Fields
        SQL_Select = SQL_Select & "[" & fld.Name & "], "
        SQL_Where = SQL_Where & "[" & fld.Name & "] Is Null OR "
    Next fld

    SQL_Select = Left(SQL_Select, Len(SQL_Select) - 2) & " FROM [" & TableName & "]"
    SQL_Where = Left(SQL_Where, Len(SQL_Where) - 4)

    SQL = SQL_Select & SQL_Where

    Set qdf = db.CreateQueryDef("SQL_" & TableName, SQL)

    rst.Close
    Set rst = Nothing
    Set qdf = Nothing

End Sub

Comments

0

So what I did was to use a little bit of excel magic.

  1. Select one row from the table TBL_Cumulative.
  2. Copy that row, including headers into Excel.
  3. Copy the row again in Excel, paste with Transpose to a new sheet.

Now you have a list of all the fields in column A.

  1. In column B, create a formula:

    =CONCAT("SELECT '";$A1;"', COUNT(*) FROM TBL_Cumulative WHERE ";$A1;" IS NOT NULL UNION ALL")

  2. Drag down (from the lower rt corner of the cell) across all rows (with the column names) to copy the formula.

  3. Copy the resulting text.

  4. Paste into a query window in Access.

  5. Remove the last " UNION ALL" from the last row/text line.

  6. Run the query.

The result will be a list containing the names of all columns, and how many times they have non-null values. So you can just pick out the ones with 0 (zero) counts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.